I need some help for validating my form with Laravel 5.4.
My form:
{{Form::bsText('general[firstname]')}}
{{Form::bsText('general[lastname]')}}
Then I have a RequestObject for the validation with the following rules:
'general[firstname]' => 'required|string:max:255',
'general[lastname]' => 'required|string:max:255',
This way it generates an error 'required' when not empty as expected. Though when i fill in a string, it still gives the required error message.
I've also tried the following as from the laravel docs:
'general.firstname' => 'required|string:max:255',
'general.lastname' => 'required|string:max:255',
and:
'general.*.firstname' => 'required|string:max:255',
'general.*.lastname' => 'required|string:max:255',
Both of the above don't give an error at all.
On request, here is my full Request object:
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
$user = Auth::user();
return ($user && $user->isProjectManager()) ||
($user && $user->isAdmin());
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
switch($this->method()){
case 'GET':
case 'DELETE':
return [];
case 'POST':
return [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'role' => 'in:2,3,4,5,6,7',
'password' => 'required|string|min:6|confirmed',
'project_manager_gegevens_photo' => 'required_if:role,2|mimes:png,jpeg,gif',
'general[voornaam]' => 'required|alpha:max:255',
'general[achternaam]' => 'required|string:max:255',
'general[date]' => 'required_if:role,3,4,5|date|after:today',
'general[telefoonnummer]' => 'required_if:role,3,4,5',
'general[interne_medewerker]' => 'boolean',
'general[geslacht]' => 'in:m,v,o',
];
case 'PUT':
case 'PATCH':
return [
'name' => 'required|string|max:255',
'password' => 'required|string|min:6|confirmed',
];
default:return [];
}
}
Proof that it is has something to do with array validation: When I change the name to:
{{Form::bsText('general_firstname')}}
and
'general_firstname' => 'required|string:max:255'
It validates like it should en what you'd expect. Though, i like things clean and seperate and want a array that hold all of the general fields.
So, how can I validate it so it is an array?
Use alpha insteadof string,
like,
'general.firstname' => 'required|alpha|max:255'
Replace this,
'general[firstname]' => 'required|string:max:255',
Refer Docs, https://laravel.com/docs/5.4/validation#rule-alpha
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