JQuery验证规则不适用于输入字段中的文件阵列
问题描述:
这似乎并不适用于我的代码。我试图使用JQuery表单验证来验证表单,其中一个字段有一个图像数组(或文件输入列表)。我摆弄了很多不同的选择,但没有掩盖。JQuery验证规则不适用于输入字段中的文件阵列
这是我想做的事: 假设我有一种形式,它有一个输入字段,需要多张图片:
{!! Form::file('images[]', array('multiple'=>true,'id'=>'images', 'class' => 'form-control btn btn-default')) !!}
我试过如下:
$("#myform").validate({
rules: {
images[]: {
required: true,
accept: "image/*"
}
},
messages:
{
images[]: "Title field cannot be blank!"
}
});
答
在PHP ,提交的值如field[]
会导致名为$ field的变量包含附加项目。
$field = array();
$field[] = "A";
$field[] = "B"
相同
$field = array();
array_push($field, "A");
array_push($field, "B");
在JavaScript中,然而,该语法是未知的。你要么必须包装的阵列周围的field[]
项目的JSON-对象或删除[]
:
$("#myform").validate({
rules: {
field: {
required: true,
accept: "image/*"
}
},
messages:
{
field: "Title field cannot be blank!"
}
});
在你的情况,我不明白为什么你打电话给你的变量field
。我想你想叫它images
:
$("#myform").validate({
rules: {
images: {
required: true,
accept: "image/*"
}
},
messages:
{
images: "Title field cannot be blank!"
}
});
我的ID删除[]做一个像你,它不会引发任何错误消息 – Jose
你确定“场”是正确的名称?它不应该是“图像”吗? – Psi
不,我只是复制它。我确实使用了图像名称。ill编辑它 – Jose