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',
]);
Related
I'm trying to validate a unique entry in my laravel app
following is my validation array,
$website = $websiteModel->find($id);
$this->validate($request, [
'subDomainName' => ['required','regex:/^[A-Za-z0-9 ]+$/'],
'subDomainSuffix' => ['required'],
'packageType' => ['required'],
'themeid' => ['required'],
'lang' => ['required'],
'user' => ['required'],
'domain' => [
'required',
'string',
'min:2',
'max:255',
Rule::unique('apps')->ignore($website)
],
], $request->all());
My validation working properly BUT,
When i tried to enter a duplicate value for my domain field, It get validated properly but not showing the error message, saying sorry the name is already exists...
<input type="text" id="domain" class="form-control" name="domain" >
{!! $errors->first('domain', '<span class="help-block" role="alert">:message</span>') !!}
Here in this span it shows nothing but in the common error message area it shows sorry the form cannot be updated... So how can I validate the field properly and display the relevant error message
Do something like this:
On insert request use
'domain' => [
...
'unique:websites,domain'
]
On update request use
'domain' => [
...
"unique:websites,domain,{$this->website->id}"
]
Or
'domain' => [
...
Rule::unique('websites', 'domain')->ignore($this->website)
]
You passed $request->all() as validation messages.
Please Try:
$website = $websiteModel->find($id);
$request->validate([
'subDomainName' => ['required','regex:/^[A-Za-z0-9 ]+$/'],
'subDomainSuffix' => ['required'],
'packageType' => ['required'],
'themeid' => ['required'],
'lang' => ['required'],
'user' => ['required'],
'domain' => [
'required',
'string',
'min:2',
'max:255',
Rule::unique('apps')->ignore($website)
],
]);
don't you need to pass duplicate column in ignore Rule To instruct the validator to ignore the website domain, except for it self ? for example like
Rule::unique('apps')->ignore($website->id)
please try this one . it helps to solve your problem
use exception and validator in top of the file
use Exception;
use Validator;
$rules = [
'subDomainName' => 'required|unique:sub_domain_name',
];
$validator = Validator::make($request->all(), $rules, $message);
if ($validator->fails()) {
throw new Exception(implode('\n', $validator->errors()->all()));
}
sub_domain_name : this is database column name
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
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?
My validation fails with this:
$this->validate($request, [
'name' => ['required'],
'email' => ['required', 'email', 'unique:organisers',$organiser->id,'organisers_id'],
'organiser_logo' => ['mimes:jpeg,jpg,png', 'max:10000'],
]);
but it works with this:
$this->validate($request, [
'name' => ['required'],
'email' => ['required', 'email', 'unique:organisers'],
'organiser_logo' => ['mimes:jpeg,jpg,png', 'max:10000'],
]);
This:
'unique:organisers',$organiser->id,'organisers_id'
needs to be:
'unique:organisers,'.$organiser->id.',organisers_id'
or (note the double-quotes):
"unique:organisers,{$organiser->id},organisers_id"
The , means "new array element", the . means "add to this string".
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',
],