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?
Related
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
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 have a set of validation rules in laravel. there are two date fields that one of them have to be greater than other. but when I use gt operator, an error apears:
$validation = Validator::make($request->all(), [
'description' => 'required|string',
'started_at' => 'required|date',
'finished_at' => 'required|date|gt:started_at',
]);
error is: Method Illuminate\Validation\Validator::validateGt does not exist.
gt:started_at is wrong gt rule does not exist in laravel use after
$validation = Validator::make($request->all(), [
'description' => 'required|string',
'started_at' => 'required|date',
'finished_at' => 'required|date|after:started_at',
]);
How i set custom the attributte names in laravel 5.2 I already try this code, but doesn't work:
$attNames = array(
'code' => 'Número',
'contributor' => 'Nº Contribuinte',
'create_date' => 'Data criação',
'address' => 'Morada',
'zip_code' => 'Cod. Postal',
'city' => 'Localidade',
'email' => 'E-mail',
'phone_number' => 'Telefone',
'note' => 'Observações',
);
$validator = Validator::make($client, $this->rules,[],$attNames);
$validator->setAttributeNames($attNames);
if ($validator->fails()) {
// send back to the page with the input data and errors
$errors = $validator->messages();
return Redirect::to('/client/create')->withInput()->withErrors($errors);
}
You have passed wrong arguments to Validator::make.
You can pass only three arguments.
As per Documentation,
If needed, you may use custom error messages for validation instead of
the defaults. There are several ways to specify custom messages.
First, you may pass the custom messages as the third argument to the
Validator::make method.
$messages = [
'required' => 'The :attribute field is required.',
];
$validator = Validator::make($input, $rules, $messages);
I figure out.
controller:
use Validator;
(...)
$attName=array(
'code' => trans('validation.code'),
'contributor' => trans('validation.contributor'),
'create_date' => trans('validation.create_date'),
'address' => trans('validation.address'),
'zip_code' => trans('validation.zip_code'),
'city' => trans('validation.city'),
'email' => trans('validation.email'),
'phone_number' => trans('validation.phone_number'),
'note' => trans('validation.note'),
);
$validator = Validator::make($client, $this->rules, [], $attNames);
validation.php:
'attributes' => [
'code' => 'número',
'contributor' => 'nº contribuinte',
'create_date' => 'data criação',
'address' => 'morada',
'zip_code' => 'cod. postal',
'city' => 'localidade',
'email' => 'e-mail',
'phone_number' => 'telefone',
'note' => 'observações',
],