I need help checking out my form with Laravel 5.4.
My form:
{{Form::bsText('general[firstname]')}}
{{Form::bsText('general[lastname]')}}
Then I have RequestObject for validation with the following rules:
'general[firstname]' => 'required|string:max:255',
'general[lastname]' => 'required|string:max:255',
Thus, it generates a “required” error, if not empty, as expected. Although, when I fill in the line, it still gives the required error message.
I also tried the following from 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 do not give errors at all.
Upon request, here is my complete Request object:
public function authorize()
{
$user = Auth::user();
return ($user && $user->isProjectManager()) ||
($user && $user->isAdmin());
}
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 [];
}
}
The proof that this 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 checks how you should include what you expect. Although, I like things clean and separate, and I want an array in which all the common fields are stored.
So how can I check it so that it is an array?