Validation in Laravel 5 in array-type fields?

2

Through collaborators of the site I was able to ask a question in the question Do a custom validation in laravel 5 validation validation in> ComboBox is checked.

Example:

  

input : text , required_if

And now another question has come up, I have several name fields, which is anotherfield value input , there in the case above it does not accept in the validation, it considers existing because of the existence of the array

Is there a way to do this?

    
asked by anonymous 12.10.2016 / 23:24

2 answers

1

Your FormRequest needs to know that the name field is a array and the already has a form of validate the field only needs to have the following nomenclature:

>
  

nome_do_campo + .*

Since your fields are related to both, you need to follow the nomenclature nome.* and liberar.* in the part of the validation, already in the part of the form ( <form> ) follows example below: p>

<p>
    <input type="checkbox" value="true" name="liberar[]">
    <input type="text" value="" name="nome[]">
</p>
<p>
    <input type="checkbox" value="true" name="liberar[]">
    <input type="text" value="" name="nome[]">
</p>

Also see an example of FormRequest :

class ExemploRequest extends FormRequest
{

    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'nome.*' => 'required_if:liberar.*,true'
        ];
    }

    public function messages()
    {
        return [                
            'nome.required_if' => 'nome é para ser digitado'
        ];
    }
}

Recommendation: You should also control the validation with Javascript to send the data practically correct, and these fields are sequences be very careful about this. >

14.10.2016 / 01:42
0

You can use an asterisk to symbolize any array field (name *.), I did not understand exactly what you want, so I advise you to take a look at the Laravel documentation in the array validation part, it should help you :

link

    
13.10.2016 / 16:52