duykhoa
duykhoa

Reputation: 2302

Write rules in jquery-validation for Rails form

I have a form in HTML code

<form>
  <input placeholder="Your name" class="string required" type="text" name="service_order[your_name]" id="service_order_your_name" aria-required="true">
  <input placeholder="Your email" class="string email optional" type="email" name="service_order[email]" id="service_order_email">
</form>

the code is generate by Simpleform in Rails

= simple_form_for(@service_order) do |f|
  = f.input :your_name
  = f.input :email

And I use jquery validation to check user input. To define the rules in javascript, we simple write something similar to:

$("#new_service_order").validate(
    rules:
      your_name:
        minlength: 2
  )

My problem is the field is service_order[st], not just st like the example. How can I deal with it?

Upvotes: 0

Views: 1022

Answers (1)

akshay_kashain
akshay_kashain

Reputation: 700

As mentioned in the reference docs, you need to quote the names as in

$("#new_service_order").validate({
  rules: {
    'service_order[your_name]': {
      minlength: 20
     },
    'service_order[email]': {
      email: true
    }
  }
});

Also, please note that you need to fix the object literal passed to the validate function.

Upvotes: 2

Related Questions