0

It just doesn't seem to work in my codes. I am trying to validate a form using JQuery form validation where one of my field has an array of Images (or list of file inputs). i fiddled around with a lot of different options but at no veil.

Here is what i want to do : Suppose I have a form and it has an input field which takes multiple images:

{!! Form::file('images[]', array('multiple'=>true,'id'=>'images', 'class' => 'form-control btn btn-default') ) !!}   

I tried the following:

$( "#myform" ).validate({
  rules: {
images[]: {
  required: true,
  accept: "image/*"
}
  },
 messages: 
             {
                 images[]: "Title field cannot be blank!"

             }
});
Jose
  • 155
  • 2
  • 8

1 Answers1

0

In PHP, a value submitted like field[] leads to a variable called $field containing an additional item.

$field = array();
$field[] = "A";
$field[] = "B"

is the same as

$field = array();
array_push($field, "A");
array_push($field, "B");

In JavaScript, however, this syntax is not known. You either have to wrap an array around your field[] item in the JSON-Object or remove the []:

$( "#myform" ).validate({
  rules: {
    field: {
      required: true,
      accept: "image/*"
    }
  },
  messages: 
  {
     field: "Title field cannot be blank!"
  }
});

In your case, I don't see why you call your variable field. I guess you want to call it images:

$( "#myform" ).validate({
  rules: {
    images: {
      required: true,
      accept: "image/*"
    }
  },
  messages: 
  {
     images: "Title field cannot be blank!"
  }
});
Psi
  • 6,387
  • 3
  • 16
  • 26
  • i id remove the [] an did as yours, it wont throw any error message – Jose Feb 25 '17 at 19:24
  • are you sure that "field" is the right name? shouldn't it be "images"? – Psi Feb 25 '17 at 19:34
  • no its not, i just copied it . i did use images name .. ill edit it – Jose Feb 25 '17 at 19:38
  • The plugin does not work like this. If you declare a rule on an object named `images`, then it's looking for a *single* field with `name="images"`, not an array. – Sparky Feb 25 '17 at 23:09