Validating Laravel Form Array

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:

/**
 * 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 [];
    }
}

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?

+4
5

, . .

$errors->get('general[voornaam]')

:

$errors->get('general.voornaam')

! !

0

,

'general.firstname' => 'required|alpha|max:255'

,

'general[firstname]' => 'required|string:max:255',

, https://laravel.com/docs/5.4/validation#rule-alpha

+1

'general.firstname' => 'required|alpha|max:255',
'general.lastname' => 'required|alpha|max:255',
0

?

 'general[voornaam]' => 'required|alpha:max:255',
  'general[achternaam]' => 'required|string:max:255',

, : -

'general[voornaam]' => 'required|alpha|max:255',
'general[achternaam]' => 'required|alpha|max:255',
0

, , 5.4, , 5.1, :

    $data = Input::all();

    $rules = [
            'fname' => ['required', 'min:3'],
            'lname' => ['required', 'min:3'],   
            'email' => ['required', 'email', 'unique:users', 'min:3'],
            'password' => ['required', 'min:4'],
            'type' => ['required']
            ];
    $validator = $this->validate($request, $rules);

, fname lname , .. , , .

, .

0

Source: https://habr.com/ru/post/1686793/


All Articles