Kris MP
Kris MP

Reputation: 2415

how to validate ckeditor with jquery validate in rails?

my view code:

<%= f.cktext_area :description, class: 'form-control', id: 'description', rows: 8, required: true %>
<script>
  CKEDITOR.replace('description');
</script>

this will generate an html code like this:

<textarea class="form-control" id="description" rows="8" required="required" name="product[description]">
</textarea><script>
//<![CDATA[
(function() { if (typeof CKEDITOR != 'undefined') { if (CKEDITOR.instances['description'] == undefined) { CKEDITOR.replace('description'); } } else { setTimeout(arguments.callee, 50); } })();
//]]>
</script>

and here is my script to validate above code:

$(function(){
  $("#product_form").validate({
ignore: [],
    rules: {
      description: {
        required: function(){
          CKEDITOR.instances.description.updateElement();
        }
      }
    }
  });
})

I've been following this fiddle and customized with my need, but nothing happen. Can anybody help me? thanks

Upvotes: 1

Views: 717

Answers (1)

Sparky
Sparky

Reputation: 98728

You have two problems below...

rules: {
    description: {
        required: function(){
            CKEDITOR.instances.description.updateElement();
        }
    }
}
  1. You are only supposed to use the name of the field, not the id within the rules option. In this case, product[description].

  2. Having a function() as the parameter of the required rule makes no sense in this context. The field is either required or it isn't, so this rule can only be true or false.

Try this...

rules: {
    "product[description]": { // <-- name of the field
        required: true        // <-- this field is required
    }
}

Upvotes: 1

Related Questions