I have a form, it has a date field called starts, which is optional:
...
<input type="date" name="starts">
...
Then I created a Request in Laravel that checks, among other things, if starts is a date, so the rules() function in the Request is somthing like:
public function rules()
{
return [
... (more validation rules)
'starts' => 'date',
... (more validation rules)
];
}
So, when I submit the form and there's no starts, the validation rules kicks in, it says it's not a date, and returns to my form.
How do I tell Laravel that starts is a date, only if it is present?
I'm looking something like the validation in files, which only take place if there's a file present
You can add "nullable" for your laravel validation rule like so:
'starts' => 'date|nullable'
Try 'starts' => 'nullable|date' as validation rule. There is a section about this in the documentation.
Related
I need to validate that a customer is using a valid subdomain name as well as making sure some other required fields are present. I have created the following rule in my model class:
public function rules()
{
return array(
array(',c_customerSubdomain, c_custAdminUser,c_adminPassword,c_adminContact', 'required', 'message' => "{attribute} is TEST required."),
array('c_customerSubdomain', 'match', 'pattern' => '/^[a-z0-9_]*$/', 'message' => "Customer Subdomain should contain only alphanumeric and underscore.", "allowEmpty" => false),
);
}
I am testing whether or not the rule is firing by changing the message for required values to: {attribute} is TEST required.
When I submit the form with all of the field blank, the result is as expected.
However, when I populate the field for subdomain with an illegal value, such as "ASD# a", I am expecting to get a validation error. However, instead, the rules bypass all of the other validation errors and attempt to save the model.
I have the following validation code in a controller. The problem is that laravel is sending the column name in the validation error , which is most of the time abbreviations. In this case the column name is "ans". Is there a way to show a different name to the user ?
Validation :
$this->validate($request, [
'ans' => 'required|max:5000|min:10'
]);
Error :
The ans field is required.
If you want to customize the attribute name you can do that; you don't have to customize the message to do this. The validate method takes an array of "custom attribute names" to use (4th argument), just like it takes an array of custom messages (3rd argument).
$this->validate(
$request,
['ans' => 'required|max:5000|min:10'], // rules
[], // custom messages
['ans' => 'Answer'] // custom attribute names
);
You should always have the option to specify custom messages and attributes with the validation methods that are available. These custom attribute names get replaced in the messages where :attribute is used.
If you don't do it this way you would have to create 3 custom messages, one for each rule you have defined to use for your field 'ans', since any of those rules could fail and will include the attribute name.
Yes, you can achieve with the following code.
$this->validate($request, [
'ans' => 'required|max:5000|min:10'
], [
'ans.required' => 'The answer field is required.'
]);
Take a look at the laravel documentation to know more.
You can send custom validation message,define all
your validation message in a variable like this
$messages = [
'ans.required' => 'The answer field is required.'
]
$this->validate($request, [
'ans' => 'required|max:5000|min:10'
], $messages);
Thanks
Hy! how can i use the not equal in validation
i'm using laravel 5.8. i want to do when i put the (name) something like Admin in input field at the submission of the form it show me error the You can't Use the Name Admin This is all for guest use. when the user is login the he/she can add this name
Laravel offers notIn validation rule for this:
Validator::make($data, [
'name' => [
'required',
Rule::notIn(['Admin']), // You can add more values to the array to black list it.
],
]);
Read more about this here.
I have a problem with Request validation. Everything works but one pattern. And its if I dont want the field to be required but once it is filled I want it to match a regex.
But it throws regex error when I leave the field empty.
Any tips on how I should handle it?
BTW: I made a custom Request class where I take care of the validation so if the solution could be also in the Request and not directly in Controller that would be great.
return [
'dic' => 'max:12|regex:/^[a-zA-Z]{2}[0-9]{8}[0-9]*$/',
];
return [
'dic' => 'nullable|max:12|regex:/^[a-zA-Z]{2}[0-9]{8}[0-9]*$/',
];
nullable won't check the other rules when the field is empty.
In my Laravel 5.2 app, I have the following validation rules to check a field:
$rules = array(
'file' => 'mimes:jpg,png,pdf,doc,docx',
'file' => 'validate_file'
);
Now, the validate_file is a custom validation rule. I want to run it only only if the first mimes validation passes.
I know I could simply validate/redirect with errors - in two batches, first the mimes, then the validate_file, but it seems like an overkill.
Any way I can do it the smart way in Laravel 5.2?
Use "bail" attribute. It will stop validation after first failure.
$this->validate($request, [
'title' => 'bail|mimes:jpg,png,pdf,doc,docx|validate_file',
]);
https://laravel.com/docs/5.2/validation#validation-quickstart search for "bail"