I am using Laravel 6.13.1.
I have the following validation
$validator = Validator::make($request->all(), [
'name' => 'required|max:100',
'email' => 'required|email',
'mobile_number' => 'required',
'date_of_birth' => 'required',
'address' => 'required',
'category' => 'required',
'other_category' => 'required_if:category,==,Others',
'sub_caste' => 'required',
'photo' => 'required',
'status' => 'required|integer',
'father_name' => 'required',
'father_occupation' => 'required',
]);
if ($validator->fails()) {
return back()->withErrors($validator)->withInput();
}
It has 12 rules and it works. If I add one more rule then the validator stops working.
{{$errors}} gives an empty array in the view file.
Edit 1: The validation with 12 rules shows all error messages, but if I add one more validation like
$validator = Validator::make($request->all(), [
'name' => 'required|max:100',
'email' => 'required|email',
'mobile_number' => 'required',
'date_of_birth' => 'required',
'address' => 'required',
'category' => 'required',
'other_category' => 'required_if:category,==,Others',
'sub_caste' => 'required',
'photo' => 'required',
'status' => 'required|integer',
'father_name' => 'required',
'father_occupation' => 'required',
'mother_name' => 'required',
]);
then no error messages. {{$errors}} is an empty array.
In my view, I am using the following code to list errors
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
Edit 2: I tried the same validation on Laravel 5.5 and it works well as indented.
I was facing the same problem, and found somewhere that maybe this is caused by the size of error messages. I've changed the SESSION_DRIVER env option from "cookie" to "file" and it worked!
I don't think that it has a limit, However you can create a request and put all your validation on it like that:
php artisan make:request UserRequest
You can check out this link Form Request Validation
Related
When I try to update a post if I dont change value of slug , laravel return this error:
The slug has already been taken.
Here is my validation:
$this->validate($request, [
'name' => 'required',
'content' => 'required',
'excerpt' => 'required',
'type' => 'required',
'slug' => 'required | unique:providers'
]);
when you're updating records and there is unique validation is added. You should pass the id of which record you're inserting.
While Update
'slug' => 'required | unique:providers,slug,'.$providerId
While insert
'slug' => 'required | unique:providers'
You need to add an except rule to your unique case to exclude the current ID from check:
$this->validate($request, [
'name' => 'required',
'content' => 'required',
'excerpt' => 'required',
'type' => 'required',
'slug' => 'required|unique:providers,id,' . $provider->id
]);
Make the column and the id that you pass to use your table's data. I am just giving you an idea on what you need to add.
You need to tell the validator to do an exception for the current entity you're updating.
$this->validate($request, [
'name' => 'required',
'content' => 'required',
'excerpt' => 'required',
'type' => 'required',
'slug' => 'required|unique:providers,slug,'.$providerId
]);
When adding:
use Illuminate\Support\Str;
.
.
$slug = Str::slug($request->name, '-');
$exists = Provider::where('slug', $slug)->first();
if($exists) {
$slug = $slug.'-'.rand(1000,9999);
}
When updating:
$request->validate([
'slug' => 'required|unique:providers,slug,'.$provider_id.',id',
]);
if i have more than 20 fields,can we validate this with less code???
i do not want to write required for all input .
$request->validate([
'first_name' => 'required',
'last_name' => 'required',
'gender' => 'required',
'date_of_birth' => 'required',
'place_of_birth' => 'required',
'nationality' => 'required',
'mobile_number' => 'required',
'email' => 'required|email|unique:informations',
'home_region' => 'required',
'digital_address' => 'required',
'school_name' => 'required',
'school_region' => 'required',
'school_digital_address' => 'required',
'school_level' => 'required',
'school_program_of_study' => 'required',
'patron_first_name' => 'required',
'patron_last_name' => 'required',
'patron_gender' => 'required',
'patron_mobile_number' => 'required'
]);
Sure! here's how, iterate through all request data (including ones you didn't intend to have sent which makes this a bad idea) and validate them to be required except for ones that requires more rules like 'email' (those can be validated by themselves)
$data = array_except($request->all(), ['_token', 'email']);
foreach ($data as $key => $value) {
$request->validate([$key => 'required']);
}
$request->validate(['email' => 'required|email|unique:informations']);
That's the minimal code laravel needs to works the validation.
If you dont enter each field's name, how is it supposed to know wich missing entry from the request is required ? laravel needs each and every input name marked down.
Actually yes there is a way.
This is your code
$request->validate([
'first_name' => 'required',
'last_name' => 'required',
'gender' => 'required',
'date_of_birth' => 'required',
'place_of_birth' => 'required',
'nationality' => 'required',
'mobile_number' => 'required',
'email' => 'required|email|unique:informations',
'home_region' => 'required',
'digital_address' => 'required',
'school_name' => 'required',
'school_region' => 'required',
'school_digital_address' => 'required',
'school_level' => 'required',
'school_program_of_study' => 'required',
'patron_first_name' => 'required',
'patron_last_name' => 'required',
'patron_gender' => 'required',
'patron_mobile_number' => 'required'
]);
Now in your form html for all these fields you need to make an array for it like this
<form>
<input type="text" name="person[first_name]">
<input type="text" name="person[last_name]">
<input type="email" name="email">
</form>
and in your Controller or Request you can make this array required like this
$request->validate([
'person' => 'required|array',
'person.*' => 'required'
'email' => 'required|email|unique:informations',
]);
This way you can have less index in your array ;), Hope it will help
I am currently trying to setup a Laravel project on IIS and so far everything works as it should. The only problem is that when I try to validate my form that is being submitted it crashes with the error Serialization of 'Serialization of 'Symfony\Component\HttpFoundation\File\UploadedFile' is not allowed'
This happens on the following section of code:
$rules = array(
'title' => 'required',
'short_title' => 'required|between:1,15',
//'alias' => 'required',
'description' => 'required',
'province' => 'required',
'start_datetime' => 'required|date',
'url' => 'required',
'max_entries' => 'required|numeric',
'fee_sub' => 'numeric',
'fee_class_batch' => 'numeric',
'fee_class_batch_unpaid' => 'numeric',
'fee_class_batch_t' => 'numeric',
'fee_class_batch_t_unpaid' => 'numeric',
'fee_team_remove' => 'numeric'
);
$validator = Validator::make(Input::all(), $rules);
If I comment out the last line the form submit works without error. So it has something to do with that Input::all(), $rules.
Does someone know why this is happening?
I want validate name and email (do required) only if user is guest. How I can do it correctly?
I tried paste the if condition. But I don't think this is correct:
if (Auth::check()) {
$request->validate([
'name' => 'required|string',
'email' => 'required|string',
'body' => 'required|string',
'type' => 'required|integer|in:1,2,3'
]);
} else {
$request->validate([
'body' => 'required|string',
'type' => 'required|integer|in:1,2,3'
]);
}
I can't think of a validation rule that'll do this, but you can at least get the code a little cleaner by skipping the else condition, for instance like so:
$rules = [
'body' => 'required|string',
'type' => 'required|integer|in:1,2,3',
];
if (Auth::guest()) {
$rules['name'] = 'required|string';
$rules['email'] = 'required|string';
}
$request->validate($rules);
Another option would be to simply check the requirement within the array and just skip the field in the validation for authorised users. It's shorter, but not quite as clean:
$request->validate([
'body' => 'required|string',
'type' => 'required|integer|in:1,2,3',
'name' => Auth::guest() ? 'required|string' : '',
'email' => Auth::guest() ? 'required|string' : '',
]);
By the way, I would recommend validating the email address as an email address: https://laravel.com/docs/5.7/validation#rule-email
I don't know how to get the query string array to validate in Laravel Validation.
here is my code
public function addUserByAdmin(Request $request){
$this->validate($request,[
'name' => 'required',
'fullname' => 'required',
'password' => 'required',
'position' => 'required',
'phone' => 'required'
]);
$uesrAction = new UserAction($this->repository);
$user = $uesrAction->addUser($request->input('phone'),$request->input('password'),$request->input('name'),$request->input('fullname'),$request->input('position'));
if($user){
return response()->json(['success' => 'success','user'=>$user]);
}
return response()->json(['success' => 'fail']);
}
but validation doesn't work.Then I tried Validation::make() method and also didn't work.
My URL be like
http://localhost:8000/admin/users/add-user?phone=abcdefg&password=erer&name=ere&fullname=rere&position=343
If you are using laravel 5.5, change your code as follows:
public function addUserByAdmin(Request $request){
$validatedData = $request->validate([
'name' => 'required',
'fullname' => 'required',
'password' => 'required',
'position' => 'required',
'phone' => 'required'
]);
// ...
}
One of the features that was added in this update is that other way to use the validate() method:
More info:
https://laravel.com/docs/5.5/validation#quick-writing-the-validation-logic
https://laravel.com/docs/5.5/releases