1

There are two similar questions 1 and 2, but they submit the form through ajax, in my Rails app, I don't want to use ajax loading and write the following code:

<% content_for :javascript do %>
$("#new_answer_on_<%= _activity_parent_id %>").validate({
    onsubmit: function(){
        CKEditorUpdate();
        return true;
    },  
    errorClass: "my-error-class",
    rules: {
        "answer[content]": {required: true, minlength: 20}
    }
});

function CKEditorUpdate(){
for(instance in CKEDITOR.instances)
   CKEDITOR.instances[instance].updateElement();
}
<% end %>

The above code can alert error message when value of the textarea is empty, but when there is the available text: When I click submit button first time I also get alert error message, until I click it second time it will submit the content to the server. I don't know why? What should I do for reducing the additional click? Thanks in advance!

Community
  • 1
  • 1
abelard2008
  • 1,984
  • 1
  • 20
  • 35
  • JavaScript does not care about your framework or how your HTML was rendered. When asking about JavaScript/jQuery, please show the rendered HTML and JavaScript as seen by the browser. – Sparky May 18 '15 at 14:38

1 Answers1

0

It's likely you've broken the normal plugin behavior by setting the onsubmit option to a function. The onsubmit option in the .validate() method can only accept a boolean false as a parameter, not a custom function. By setting it to false, you will disable the validation trigger when the submit button is clicked. As per the documentation, "a boolean true is not a valid value" (already default behavior), nor can you set it to a custom function.

Review the documentation to select the appropriate callback option for your CK Editor Update function.

Sparky
  • 98,165
  • 25
  • 199
  • 285