0

Hi Im trying to validate an array inputs like this.

{!!Form::open()!!}
   <input type="text" name="value1[]">
   <input type="text" name="value1[]">
  {!! Form::submit('submit') !!}
{!! Form::close()!!}

This is my controller

   $rules = [
              'value1[]' => 'required|array',

         ];

   $data = $request->all()

   $validator = Validator::make($data, $rules);

        if ($validator->fails())
        {
            return redirect()->back()->withErrors($validator->errors());
        }

Unfortunately, it doesn't seem to work. How do you validate an input fields in laravel using laravel jsvalidation? This is the package I am using https://github.com/proengsoft/laravel-jsvalidation/issues/174

Thanks,

3
  • do just want to validate if value1 is array or not ? Or you want to validate individual elements inside the array value1 Commented Aug 10, 2016 at 9:45
  • Actually, Im trying to validate if value1 is not empty. I want to validate individual elements inside the array. Commented Aug 10, 2016 at 9:54
  • Please do not use the jQuery Validate tag when the question has nothing to do with this plugin. Edited. Thanks. Commented Aug 10, 2016 at 14:12

2 Answers 2

1

Try this:

$rules = [
              'value1.*' => 'required',

         ];

Reference, validating array inputs in laravel

Update:

$rules = [
              'value1' => 'array|required',

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

5 Comments

@Atashi if this solves your issue, you can select it as answer.
Unfortunately, this didn't work. If I tried to validate this, using $validator->fails(), it always say true. Whether there is value or null, it always fail.
do you know the structure of value1 array?
is it like this > value1[index][index] or value1[index][key], here index means number and key means any string
As you can see on my html, the name of the element is name="value[]", this is dynamically created,
0

You can create a Form Request Class and catch using

$this->request->get('items')

with that you can loop through your array inputs.

Here is a link that might Help you :

https://ericlbarnes.com/2015/04/04/laravel-array-validation/

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.