Jump to content

trying to use _each


jimfog

Recommended Posts

Take a look at this code....

 _.each(user.validationError, function (error) {                $('.fillname').prepend(error + '<br />');                    });

it uses _each,an underscore method to display error messages in a form(if the user has filled in the appropriate input fields.)

 

user.validationError is an object which contains that error messages and the function just iterates over them and puts them in an element with the class you see.

 

The problem with this code is that the error messages are NOT displayed under each form element as I want it.

Given the fact that user.validationError is an object with 3 elements...or less if the user has filled correctly one or two input elements

How I could iterate this object and assign its values to 3 different elements....each associated with a different input form?

 

It must be a loop...but I need a little help.

Link to comment
Share on other sites

Map the element the error message should go to and make it dynamic instead of hardcoding it. This could be part of your validation object.

Link to comment
Share on other sites

Map the element the error message should go to and make it dynamic instead of hardcoding it. This could be part of your validation object.

How am I going to do the mapping...I have heard the term before...but I am not sure what to do.

Link to comment
Share on other sites

var object={name:"you have to put a name",staff:"you have to put some staff",service:"you have to put a service"} 
Link to comment
Share on other sites

If "name", "staff", and "service" are the IDs of fields then you can use those to figure out where the put the messages. That would be one mapping. It would probably be more flexible to have an array of objects where one property of the object is the element ID to put the message, and the other property is the message.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...