October CMS - required checkbox - laravel

How could I validate if the checkbox is ticked?
public $rules = [
'name' => 'required',
'email' => 'required|email',
'phone' => 'required',
'terms' => 'required',
];
'terms' is the name of the checkbox. Now I get the alert about required field even if it is checked.
HTML:
<input type="checkbox" name="terms" value="1" id="terms"/>
Simple HTML5 attribute required is also not working.

Wouter Van Damme has it right. You simply need to use the trait listed in that documentation. like so:
public $rules = [
'name' => 'required',
'email' => 'required|email',
'phone' => 'required',
'terms' => 'required|accepted',
];
here is an october specific link although the answers are the same.http://octobercms.com/docs/services/validation#rule-accepted

Related

how to exit when I return laravel Validation?

i' new to laravel here I have created a registration API and private function for validation but not show validation.
that is my validation function
`private function validateInfo($request, $toValidate) {
$validator = Validator::make($request->all(), $toValidate);
if ($validator->fails()) {
$errors = $validator->errors()->toArray();
$values = array_values($errors);
$arr = [];
foreach ($values as $errorlst)
array_push($arr, $errorlst[0]);
$error = implode(',', $arr);
return response()->json(['error' => true, 'message' => $error], 400);
exit;
}
}`
call function under switch case :-
switch ($request->user_type) :
case "teacher":
$this->validateInfo($request, [
'first_name' => 'required',
'last_name' => 'required',
'telephone' => 'required',
'country' => 'required',
'date_of_birth' => 'required|date',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
]);
break;
case "student" :
$this->validateInfo($request, [
'first_name' => 'required',
'last_name' => 'required',
'telephone' => 'required',
'country' => 'required',
'date_of_birth' => 'required|date',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
]);
break;
endswitch;
it is not showing validation error. Any Mistake?
Thank You
instead of this you can create a form request to validate your request.
class UserRequest extends FormRequest
{
protected function failedValidation(Validator $validator) {
throw new HttpResponseException(
response()->json([
"message" => implode(',',$validator->errors()->all())
],422));
}
public function rules(){
return [
'first_name' => 'required',
'last_name' => 'required',
'telephone' => 'required',
'country' => 'required',
'date_of_birth' => 'required|date',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
]);
}
}
please check docs for more info https://laravel.com/docs/8.x/validation#form-request-validation
You Need to return For Display Like
switch ($request->user_type) :
case "teacher":
return $this->validateInfo($request, [
'first_name' => 'required',
'last_name' => 'required',
'telephone' => 'required',
'country' => 'required',
'date_of_birth' => 'required|date',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
]);
break;
case "student" :
return $this->validateInfo($request, [
'first_name' => 'required',
'last_name' => 'required',
'telephone' => 'required',
'country' => 'required',
'date_of_birth' => 'required|date',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
]);
break;
endswitch;

Why is the style different? something is missing? [duplicate]

This question already has answers here:
How do I style the HTML form validation error messages with CSS?
(4 answers)
Closed 3 years ago.
I modified the code like this: ArticleRequest.php
public function rules()
{
return [
'title' => 'required|min:2|max:255',
'slug' => 'unique:articles,slug,'.\Request::get('id'),
'content' => 'required|min:2',
'date' => 'required|date',
'status' => 'required',
'category_id' => 'required',
'description'=> 'required',
];
}
ArticleCrudController.php
$this->crud->addField([
'name' => 'description',
'label' => 'Description',
'type' => 'text',
'placeholder' => 'Description meta tag',
'attributes' => [
'required' => true,
],
]);
Why is the style different? something is missing?
thanks, you are very kind
Marco
public function rules()
{
return [
'title' => 'required|min:2|max:255',
// you should add validation rule for description attr
'description' => 'required',
'slug' => 'unique:articles,slug,'.\Request::get('id'),
'content' => 'required|min:2',
'date' => 'required|date',
'status' => 'required',
'category_id' => 'required',
'description'=> 'required',
];
}

dump($request); does not work in this code. Laravel 5.6

The code does not have any syntax error, but does not work. It seems the code has no interaction with database.
public function execute(Request $request) {
if($request->isMethod('post')) {
$messages = [
'required' => "Campo :attribute rellenado",
'email' => "Este :attribute campo debe rellenarse con su email"
];
$this->validate($request,[
'name' => 'required|max:255',
'email' => 'required|email',
'text' => 'required'
], $messages);
dump($request);
edit your rules
$messages = [
'name.required' => 'This Field Name is required ',
'name.max' => 'The name should be :max in length',
'email.required' => 'the email field is required',
'email.email' => 'the email should be valid email',
'text.required' => 'the the text is required',
];
$this->validate($request,[
'name' => 'required|max:255',
'email' => 'required|email',
'text' => 'required'
], $messages);
the dump($requset) will not work in this case because you are using request trait where $this->validate function automatically redirect back if validation fails
Try to change dump($request) to dd($request)
if($request->isMethod('post')) {
$messages = [
'required' => "Campo :attribute rellenado",
'email' => "Este :attribute campo debe rellenarse con su email"
];
$this->validate($request,[
'name' => 'required|max:255',
'email' => 'required|email',
'text' => 'required'
], $messages);
dd($request);

laravel how to validate as equal to a variable

in laravel validation (registering) i want to compare one of the fields with a php variable (it should be equal with that)
how can i do this?
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'phone' => 'required|min:10|max:11|unique:users',
'email' => 'required|email|max:255',
'password' => 'required',
'password_confirmation' => 'required',
'user_captcha' => 'required'
]);
}
You can do it for example for name field like this:
$variable = "something"
return Validator::make($data, [
'name' => [
'required',
Rule::in([$variable]),
],
'phone' => 'required|min:10|max:11|unique:users',
'email' => 'required|email|max:255',
'password' => 'required',
'password_confirmation' => 'required',
'user_captcha' => 'required'
]);
Remember to import Rule Class (use Illuminate\Validation\Rule;)
You can get more info in: https://laravel.com/docs/5.4/validation#rule-in
EDIT
As suggested by #patricus, you can also concatenate the variable
$variable = "something"
return Validator::make($data, [
'name' => 'required|in:'.$variable,
'phone' => 'required|min:10|max:11|unique:users',
'email' => 'required|email|max:255',
'password' => 'required',
'password_confirmation' => 'required',
'user_captcha' => 'required'
]);
EDIT2
If you have a variable that is an array:
$variable = ['one','two'];
return Validator::make($data, [
'name' => 'required|in:'.implode(",", $variable),
'phone' => 'required|min:10|max:11|unique:users',
'email' => 'required|email|max:255',
'password' => 'required',
'password_confirmation' => 'required',
'user_captcha' => 'required'
]);
Or
$variable = ['one','two']
return Validator::make($data, [
'name' => [
'required',
Rule::in($variable),
],
'phone' => 'required|min:10|max:11|unique:users',
'email' => 'required|email|max:255',
'password' => 'required',
'password_confirmation' => 'required',
'user_captcha' => 'required'
]);

Solving Method Not Allowed Http Exception in Laravel 5.2

I want some values in my table editable, so I created this simple custom form. But this will throw error of method not allowed http exception.Any help?
<form action="{{ url('/idx-test/update-this-student/'. $student->id)}}" class="" method="POST">//changing this to put, patch does not solve the error
Route
Route::post('/idx-test/update-this-student/{id}', 'StudentController#updateThisStudent'); //again changing this to patch,or put does not help
Controller
public function updateThisStudent(StudentRequest $request, $id)
{
$student = Student::findOrFail($id);
$student->update($request->all());
// return redirect('city');
echo "updated";
}
StudentRequest
public function rules()
{
return [
'firstname' => 'required|alpha|min:2|max:10',
'lastname' => 'required|alpha|min:2|max:10',
'bday' => 'required|date',
'address' => 'required|min:10',
'zip' => 'required|min:4|max:10',
'phone' => 'required|digits:7',
'mobile' => 'required|digits:11',
'email' => 'required|email',
'city_id' => 'required',
'yearlevel_id' => 'required',
'section_id' => 'required',
];
}
By adding this small piece of code
<input type="hidden" name="_method" value="PATCH">
My problem solve

Resources