PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, March 1, 2022

[FIXED] Laravel, jQuery - return to bracket notation after validation in Laravel (dot to bracket notation in jQuery)

 March 01, 2022     javascript, jquery, laravel, laravel-5.1, php     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing