ben
ben

Reputation: 29787

How can I use customer error messages on form fields generated by Ruby on Rails when using the jQuery validation plugin?

I'm trying to use jQuery Validation plugin with customer error messages in a form on my site. The way to specify customer messages is as follows:

$('#user_new').validate({
    messages: {
        user_email: {
        required: "dd ",
        email: "Please enter a valid email address, example: [email protected]"
    }
    }
});

This will apply the custom error messages to the input with the name of user_email. This code works okay. The problem is that the fields have names such as user[email] because they are generated by Rails using form_for. But this won't work:

$('#user_new').validate({
    messages: {
        user[email]: {
        required: "dd ",
        email: "Please enter a valid email address, example: [email protected]"
    }
    }
});

The user[email] part causes a javascript error. Has anyone got custom messages working on Rails generated input fields with this plugin? Thanks for reading.

Upvotes: 1

Views: 314

Answers (1)

hybernaut
hybernaut

Reputation: 636

Have you tried just quoting the javascript array keys?

messages: {
    "user[email]": {
    required: "dd ",
    email: "Please enter a valid email address, example: [email protected]"
}

Upvotes: 1

Related Questions