[英]Passing a PHP array of rules to jQuery validator using PHP json_encode
Having checked the other posts on this topic I am still unclear on how this should work.. I am generating a PHP array of rules (as form fields are dynamic) to apply the required rule of jQuery validator. 在检查了有关该主题的其他文章之后,我仍然不清楚它应该如何工作。我正在生成一个PHP规则数组(因为表单字段是动态的)以应用所需的jQuery验证程序规则。 Code I am using.. 我正在使用的代码
$( "#the-form" ).validate({
<?php echo json_encode($rules);?>
});
The $rules
array looks like this: $rules
数组如下所示:
Array
(
[rules] => Array
(
[uName] => Array
(
[required] => 1
)
[uEmail] => Array
(
[required] => 1
[email] => 1
)
[uPassword] => Array
(
[minlength] => 5
)
[uPasswordConfirm] => Array
(
[minlength] => 5
[equalTo] => #uPassword
)
)
)
The console is chucking this error: 控制台查错此错误:
SyntaxError : invalid property id SyntaxError :无效的属性ID
Referring to the first item in the json which is printing out like this: 引用json中的第一项,它像这样打印出来:
{"rules":{"uName":{"required":true},"uEmail":{"required":true,"email":true},"uPassword":{"minlength":5},"uPasswordConfirm":{"minlength":5,"equalTo":"#uPassword"}}}
The issue is with your syntax. 问题出在您的语法上。 You have {}
around the <?php ?>
tags, which is going to end up with your Javascript getting {{"rules":{ .. }}}
, where you're wanting {rules:{ .. }}.
您在{}
<?php ?>
标记周围有{}
,最终将使您的Javascript获得{{"rules":{ .. }}}
{rules:{ .. }}.
$( "#the-form" ).validate({
<?php echo json_encode($rules);?>
});
should be 应该
$( "#the-form" ).validate(
<?php echo json_encode($rules);?>
);
in order to work properly. 为了正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.