4

All of my form fields have brackets in their names in order to group them by parent and to identify them as recurring elements, example: fieldName_01[0][0]. Of course, this makes regular use of Laravel's Validator class impossible, as it throws an error referencing that it is not expecting an array. This naming convention is common practice, so this can't be a rare issue.

I've see a couple other similar questions (HERE and HERE), but I can't seem to make sense of them (Laravel noob), or I just don't know how/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?

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>

Example of my rule:

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

Example of how I'm calling the validator:

$input = Input::all();
$validator = Validator::make($input, $this->rules);
5
  • Do you mind posting your code? Commented Apr 23, 2014 at 5:28
  • Please show more data, and what you have tried. Commented Apr 23, 2014 at 5:52
  • @ElliotFehr, I'm not exactly sure what you're looking for, but I added some sample code. Let me know if you want to see something else. Commented Apr 24, 2014 at 2:21
  • @majidarif, I really haven't tried much other than the new examples I just posted. Like I said in my OP, I don't really know where to begin. I will try you answer. Commented Apr 24, 2014 at 2:22
  • @jreed121 how did it go? I updated my answer. Commented Apr 24, 2014 at 2:43

2 Answers 2

8

As per the documentation:

When working on forms with "array" inputs, you may use dot notation to access the arrays:

(warning untested code)

If you have something like user[first_name] then:

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

You can handle any errors with:

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

So with your problem, you can validate fieldName_01[0][0] like:

'fieldName_01.0.0' => 'required|alpha_num|between:2,255'
Sign up to request clarification or add additional context in comments.

2 Comments

It didn't work. The php error is no longer being thrown, but the validator is saying the element wasn't there - it's probably not matching the rule name 'fieldName_01' with the field name 'fieldName_01.0.0'. When I use dd(Input::all()) to see what the imput looks like, the dots are replace by underscores. The fields are dynamic and sometimes their parents are dynamic as well, so there can be 1-99 of them (ie 'fieldName_01.99.99'), this is why I'm handling them in arrays. So Im trying to get away from specifying my rule like 'fieldName_01.0.0', but some how like 'fieldName_01'. Thanks BTW
@jreed121 Aaah, I see.
1
public function rules()
{
  $rules = [
'name' => 'required|max:255',
];

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

return $rules;

} this code is use for this

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.