How to validate array inputs using proengsoft/laravel-jsvalidation and Laravel 5

Hi I'm trying to validate an array input and select like this:

<td width="23%">                                            
{!!Form::select('mmscod_id[]',['' => '- Seleccione un material -'] +$mat,null,['class' => 'form-control', 'id'=>'mmscod_id'])!!}    
</td>

<td width="17%">
<input type="text" class="form-control" id="cantidad" name="vtcanp[]"/> 

</td>
<td width="17%">
<input type="text" class="form-control" id="precio" name="vtprep[]"/>
</td>

I'm using the proengsoft/laravel-jsvalidation for client-side validation. For the back-end I use Laravel's Form request.

I also use the method of this site: How To: Validate an array of form fields with Laravel but it doesn't work and throws errors:

error1

error2

Edit: I forgot to mention that these elements are created dynamically Please help me

Upvotes: 1

Views: 1529

Answers (1)

Nidhin
Nidhin

Reputation: 1838

Laravel supports validating array inputs. You need to use this convention to validate array item.

$rules = [
    'vtcanp.*' => 'required',
];

For example:

This is my custom request class

class InvoiceRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules =[
            'client' => 'required',
            'date' => 'required',
            'total' => 'required',
            'discount' => 'required',
            'item.*' => 'required',
            'rate.*' => 'required',
            'quantity.*' => 'required',


        ];


        return $rules;
    }
}

And in the view added

{!! JsValidator::formRequest('App\Http\Requests\InvoiceRequest') !!}

These validate and show the error message with position of input array that I dynamically added to the view.

Upvotes: 2

Related Questions