Issue
Using Laravel 5.1.
I have request in which I do form input validation. I call the .ajax
method and receive the JSON output.
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.
Is there a simple way to convert category.subcategory_one
to category[subcategory_one]
?
Solution
Will need to parse those into name
format
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>') })
});
Answered By - charlietfl
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.