0

I am trying to validate array field using laravel validate functionality as follow

$this->validate($request,['prodActualQty' => 'required|numeric','actQty[]' => 'required'
],$messages);

my input file is: <input class='form-control' type='text' name='actQty[]'>

It gives error if fields are blank but it still gives error even we fill the fields.

4
  • Just remove [] 'actQty' => 'required' Commented Jun 22, 2016 at 8:10
  • then it doesn't produce any error it means form gets submitted. Commented Jun 22, 2016 at 8:16
  • What message do you get when you try to submit and there's a value in the required fields? Commented Jun 22, 2016 at 8:20
  • Laravel validator with a wildcard Commented Jun 16, 2017 at 2:57

1 Answer 1

1

In Laravel 5.2 you can validate Form array elements using wildcards keyword.

So as per your situation you can either remove [] like below

$this->validate($request->all(), [
    'prodActualQty'     => 'required',
    'actQty'            => 'required'
]);

Or use wildcard operator

$this->validate($request->all(), [
    'prodActualQty'     => 'required',
    'actQty.*'          => 'required'
]);
Sign up to request clarification or add additional context in comments.

3 Comments

thanks... i just get it.. but when write message for error it gives all numbers of error equals to input fields. $messages = ['actQty.*.required' => 'Please enter rawmaterial actual quantity'];
For custom message you need to write another argument in validate method. like this $this->validate($request->all(), [ 'prodActualQty' => 'required', 'actQty.*' => 'required' ], [ 'actQty.1' => 'some validation message for actQty.1', 'actQty.2' => 'some validation message for actQty.2', ]);
yeap.. but i dont know about number of array fields are there... so writing actQty.1, actQty.2 will give problems again.

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.