[英]Laravel, jQuery - return to bracket notation after validation in Laravel (dot to bracket notation in jQuery)
Using Laravel 5.1.使用 Laravel 5.1。
I have request in which I do form input validation.我有一个请求,我在其中进行表单输入验证。 I call the .ajax
method and receive the JSON output.我调用.ajax
方法并接收 JSON 输出。
Something like:就像是:
{
"category.subcategory_one" : ["This field must be at least 3 characters long"],
"category.subcategory_two" : ["This field must be at least 5 characters long"],
...
}
However the inputs which are validated look like this然而,经过验证的输入看起来像这样
<input name="category[subcategory_one]" >
<input name="category[subcategory_two]" >
But now I can't add a CSS class to the inputs that weren't filled correctly because in my jQuery I have dot notation.但是现在我无法向未正确填充的输入添加 CSS 类,因为在我的 jQuery 中我有点符号。
Is there a simple way to convert category.subcategory_one
to category[subcategory_one]
?有没有一种简单的方法可以将category.subcategory_one
转换为category[subcategory_one]
?
Will need to parse those into name
format 将需要将其解析为name
格式
var errors ={
"category.subcategory_one" : ["This field must be at least 3 characters long"],
"category.subcategory_two" : ["This field must be at least 5 characters long"],
...
}
$.each(errors, function key, value){
var name = key;
if(key.indexOf('.') >-1){
name = key.split('.').join('[') +']';
}
$('[name='+name+']').append( $('<span>',{class:'error', text: value.join('<br>') })
});
You need to escape special characters in the selector that contains bracket notation.您需要对包含括号表示法的选择器中的特殊字符进行转义。
if (key.indexOf('.') !== -1){
key = key.split('.').join('\\[') +'\\]';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.