1

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried and doesn't validate the client side. I think the proengsoft/laravel-jsvalidation doesn't support this validation rule yet.
For me its working for dynamically created input fields.I am working on Laravel 5.2
With Laravel 5.5 and JsValidation 2.2.1, this answer works fine for me BUT it will fail if I provide an 'array' type validation, e.g. 'rate' => 'required|array|min:1'. This breaks JsValidation when there is only 1 element in the array (JsValidation see's it an a single value, not array in this case)

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.