Validating a Laravel 4 form with input values ​​that are arrays

All fields are my forms have their names in parentheses to group them by their parents and to identify them as duplicate items, such as: fieldName_01[0][0]. Of course, this makes it impossible to use the Laravel Validator class, as it raises an error, referring to the fact that it does not expect an array. This naming convention is common practice, so this cannot be a rare problem.

I see a couple of other similar questions ( HERE and HERE ), but I can "It seems they understand them (Laravel noob), or I just don’t know how and where to implement the solutions. In this answer , where would I create this extended class? How do I tell Laravel to include it in my project?

An example of my elements:

<div class="form-group col-sm-4'}}">
    {{ Form::label('fieldName_01[0][0]', 'My Element', array('class'=>'col-sm-3'))}}
    <div class="col-sm-7 col-md-6 recurringField">
    {{ Form::text('fieldName_01[0][0]', null, array(
        'class'=>'form-control input-md',
        'placeholder'=>'My Element',
        'data-element-code' => '',
        'data-recur-group' => 'fieldName_01',
        'id'=>'fieldName_01[0][0]',
        'data-fkey' => '0',
        'data-pkey' => '0'
    )) }}
    </div>
</div>

An example of my rule:

'fieldName_01'=>'required|alpha_num|between:2,255'

An example of how I call the validator:

$input = Input::all();
$validator = Validator::make($input, $this->rules);
+4
source share
2 answers

According to the documentation :

When working with forms with array inputs, you can use dot notation to access arrays:

(warning unverified code)

If you have something like user[first_name], then:

'user.first_name' => 'required|between:2,28'

:

$errors->first('user.first_name');

, fieldName_01[0][0] :

'fieldName_01.0.0' => 'required|alpha_num|between:2,255'
+8
public function rules()
{
  $rules = [
'name' => 'required|max:255',
];

foreach($this->request->get('items') as $key => $val)
{
 $rules['items.'.$key] = 'required|max:10';
}

return $rules;

}  

+1

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


All Articles