I have about 8 fields (4 required and 4 optional) in my form and I have tested every field with "required" and other forms of validation rules.
Now I have removed the "required" rule in the optional ones but when I submit the form, the optional fields won't let me, the error I get on each is "The field is invalid".
Can someone please tell what needs to be done? All I want is 4 to be required and other 4 to be optional. Also, I did place other validation rules in the optional ones like "min, max and regex".
`$this->validate($request,[
'firstname' => 'required|regex:/^[a-zA-Z ]*$/|min:2|max:255',
'lastname' => 'regex:/^[a-zA-Z ]*$/|min:2|max:255',
'email' => 'required|email|max:255|unique:clients',
'telephone' => 'regex:^\+?([0-9]{2})-?([0-9]{3})-?([0-9]{6,7})*$^',
'job_title' => 'required|regex:/^[a-zA-Z. ]*$/|min:7|max:255',
'nationality' => 'regex:/^[a-zA-Z ]*$/|min:4|max:255',
'dob' => 'required|regex:/^[a-zA-Z0-9,. ]*$/|max:255',
'interests' => 'regex:/^[a-zA-Z,. ]*$/|min:10|max:255',
]);
`
As you can see, the ones not having required field are the ones not letting me submit the form.
Related
My model has two nullable fields, but at least one of them must be not-null.
How do I setup request validation for this ?
$request->validate([
'field_1' => ['nullable'],
'field_2' => ['nullable'],
...
]);
thanks
If you have only two filed comparisons then you can use required_without:foo,bar,...
$request->validate([
'field_1' => 'required_without:field_2',
'field_2' => 'required_without:field_1'
]);
if it has more than two fields then required_without_all:foo,bar,...
$request->validate([
'field_1' => 'required_without_all:field_2,field_3',
'field_2' => 'required_without_all:field_1,field_3',
'field_3' => 'required_without_all:field_1,field_2',
]);
As per Doc
required_without:foo,bar,...
The field under validation must be present and not empty only when any
of the other specified fields are empty or not present.
required_without_all:foo,bar,...
The field under validation must be present and not empty only when all
of the other specified fields are empty or not present.
Validation
So I have a Laravel form where I'm collecting data, I use same form for creating and updating the table. Problem is that I only want the input fields to be validated when they contain data, this is my validation logic.
$validated = $request->validate([
'site_name' => 'sometimes|string',
'site_email' => 'sometimes|email',
'site_keywords' => 'sometimes|string',
'site_description' => 'sometimes|string',
'site_acronym' => 'sometimes|string|max:7',
'site_favicon' => 'sometimes|mimes:jpg,png,jpeg,svg',
'site_logo' => 'sometimes||mimes:jpg,png,jpeg,svg',
'site_currency' => 'sometimes|string',
'site_currency_symbol' => 'sometimes|string',
]);
But when the form is submitted, laravel returns an error for empty input fields, e.g it returns an error like so "The site name must be a string." for the first validation rule.
What am I doing wrong please?
EDIT
So I took the advice from #roj-vroemen and modified my code by using the nullable validation rule and it works just fine now.
Here's the new code block
$validated = $request->validate([
'site_name' => 'string|nullable|sometimes',
'site_email' => 'email|nullable|sometimes',
'site_keywords' => 'string|nullable|sometimes',
'site_description' => 'string|nullable|sometimes',
'site_acronym' => 'string|nullable|sometimes|max:7',
'site_favicon' => 'mimes:jpg,png,jpeg,svg|nullable|sometimes',
'site_logo' => 'mimes:jpg,png,jpeg,svg|nullable|sometimes',
'site_currency' => 'string|nullable|sometimes',
'site_currency_symbol' => 'string|nullable|sometimes',
]);
You'll want to use nullable in this case. sometimes basically means it only runs the validation for that field when it's present in the data.
In short:
Use nullable when you want to allow null values.
https://laravel.com/docs/8.x/validation#rule-nullable
Use sometimes if your only want to validate the field when it's present in the given input array.
https://laravel.com/docs/8.x/validation#validating-when-present
Note: you might want to make sure to filter out the empty values as the fields containing null will still be there:
$validated = array_filter($validated);
Okay, so I've been searching for a while this question, but couldn't find an answer (or at least some direct one) that explains this to me.
I've been using CodeIgniter 3.x Form Validation library, so I have some data like this:
// Just example data
$input_data = [
'id' => 1,
'logged_in' => TRUE,
'username' => 'alejandroivan'
];
Then, when I want to validate it, I use:
$this->form_validation->set_data($input_data);
$this->form_validation->set_rules([
[
'field' => 'id',
'label' => 'The ID to work on',
'rules' => 'required|min_length[1]|is_natural_no_zero'
],
[
'field' => 'username',
'label' => 'The username',
'rules' => 'required|min_length[1]|alpha_numeric|strtolower'
],
[
'field' => 'logged_in',
'label' => 'The login status of the user',
'rules' => 'required|in_list[0,1]'
]
]);
if ( $this->form_validation->run() === FALSE ) { /* failed */ }
So I have some questions here:
Is the label key really necessary? I'm not using the Form Validation auto-generated error messages in any way, I just want to know if the data passed validation or not. Will something else fail if I just omit it? As this will be a JSON API, I don't really want to print the description of the field, just a static error that I have already defined.
In the username field of my example, will the required rule check length? In other words, is min_length optional in this case? The same question for alpha_numeric... is the empty string considered alpha numeric?
In the logged_in field (which is boolean), how do I check for TRUE or FALSE? Would in_list[0,1] be sufficient? Should I include required too? Is there something like is_boolean?
Thank you in advance.
The "label" key is necessary, but it can be empty.
The "required" rule does not check length, nor does the "alpha_numeric". It checks that a value is present, it does not check the length of said value. For that, there is min_length[] and max_length[].
If you're only passing a 0 or 1, then this is probably the easiest and shortest route.
How to set not require numeric validation for Laravel5.2? I just used this Code but when i don't send value or select box haven't selected item I have error the val field most be numeric... I need if request hasn't bed input leave bed alone. leave bed validate ...
$this->validate($request, [
'provinces_id' => 'required|numeric',
'type' => 'required',
'bed' => 'numeric',
]);
If I understood you correctly, you're looking for sometimes rule:
'bed' => 'sometimes|numeric',
In some situations, you may wish to run validation checks against a field only if that field is present in the input array. To quickly accomplish this, add the sometimes rule to your rule list
In Laravel 6 or 5.8, you should use nullable. But sometimes keyword doesn't work on that versions.
Use sometimes instead of required in validation rules. It checks if only there is a value. Otherwise it treats parameter as optional.
You may need nullable – sometimes and
present didn't work for me when combined with integer|min:0 on a standard text input type - the integer error was always triggered.
A Note on Optional Fields
By default, Laravel includes the TrimStrings and ConvertEmptyStringsToNull middleware in your application's global middleware stack. These middleware are listed in the stack by the App\Http\Kernel class. Because of this, you will often need to mark your "optional" request fields as nullable if you do not want the validator to consider null values as invalid.
Tested with Laravel 6.0-dev
Full list of available rules
In laravel 5.5 or versions after it, we begin to use nullable instead of sometimes.
according to laravel documentation 8 you must to set nullable rule
for example:
$validated = $request->validate([
'firstName' => ['required','max:255'],
'lastName' => ['required','max:255'],
'branches' => ['required'],
'services' => ['required' , 'json'],
'contract' => ['required' , 'max:255'],
'FixSalary' => ['nullable','numeric' , 'max:90000000'],
'Percent' => ['nullable','numeric' , 'max:100'],
]);
in your case :
$this->validate($request, [
'provinces_id' => 'required|numeric',
'type' => 'required',
'bed' => 'nullable|numeric',
]);
I'm building a form . there is 3 specific text filed that at least one of them should be filled by the user. how may i implement it with Laravel validation rules?
//dd($request)
array:6 [▼
"_token" => "o5td5RMv2EQ5mA7LqXpyMXCKIu7L78BfrRSEU1se"
"skill_id" => "6"
"plan_id" => "1"
//at least one of below fields should be filled
"context" => ""
"link" => ""
"desired_date" => ""
]
You can use:
required_without_all:foo,bar,...
In this the field under validation must be present only when all of the other specified fields are not present.
$rules = array(
'skill_id' => 'required_without_all:plan_id,context,link',
'plan_id' => 'required_without_all:skill_id,context,link',
);
OR
You can use the required_unless rule: https://laravel.com/docs/5.2/validation#rule-required-unless
required_unless:anotherfield,value,...
IN this case, the field under validation must be present unless the anotherfield field is equal to any value.
You can use required_without_all
$rules = array(
'context' => 'required_without_all:link,desired_date',
'link' => 'required_without_all:context,desired_date',
'desired_date' => 'required_without_all:context,link',
);