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!"

             }
});
0

1 Answer 1

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!"
  }
});
Sign up to request clarification or add additional context in comments.

4 Comments

i id remove the [] an did as yours, it wont throw any error message
are you sure that "field" is the right name? shouldn't it be "images"?
no its not, i just copied it . i did use images name .. ill edit it
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.