I have been reading Laravel validation documentation. I am not clear how to combine two rules.
For example:
<input type="checkbox" name="has_login" value="1">
<input type="text" name="pin" value="">
If has_login
checkbox is ticked then Input text pin
value is required.
If has_login
is not ticked then Input text pin
is not required.
Laravel Validation:
public function rules()
{
return [
'has_login' => 'accepted',
'pin' => 'required',
];
}
Use required_with or required_if
required_with:foo,bar
The field under validation must be present and not empty only if any of the other specified fields are present.
return [
'has_login' => 'sometimes',
'pin' => 'required_with:has_login,on',
];
--
required_if:anotherfield,value
The field under validation must be present and not empty if the anotherfield field is equal to any value.
return [
'has_login' => 'sometimes',
'pin' => 'required_if:has_login,on',
];
--
https://laravel.com/docs/5.2/validation
--
Also, if the checkbox has_login
is not checked, it will not send as part of the form submission
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With