简体   繁体   English

防止在ng-invalid上提交rails表单

[英]Prevent rails form submission on ng-invalid

I have a form, generated through rails helpers, that has some AngularJS on it. 我有一个通过Rails助手生成的表单,上面有一些AngularJS。

 = form_for some_models do |f|
      %fieldset.form-horizontal
          %div.some-class
     #And so on and so forth
      =f.submit

And so on. 等等。 Within this form, I have various angular directives and controllers nesting within each other. 在这种形式下,我将各种角度指令和控制器相互嵌套。 One of the directives is listening on a field and comparing its value to do some custom angular validation like so: 指令之一是侦听字段并比较其值以执行一些自定义角度验证,如下所示:

    ctrl.$setValidity("decisionRuleCodeActivated", scope.valid).

This sets ng-invalid/ng-valid on the form and the relevant fields correctly, however the forms submission can still occur with no problems. 这样可以正确设置表单和相关字段上的ng-invalid / ng-valid,但是仍然可以毫无问题地进行表单提交。

While I understand that the "correct" way to block this behaviour is to introduce an ngSubmit, which does the validation, and then reaches out to an Angular Service which serializes to JSON and Posts to the relevant endpoint, this would require rewriting a truly obscene amount of code. 虽然我知道阻止此行为的“正确”方法是引入ngSubmit进行验证,然后提供给Angular Service(将其序列化为JSON并将其发布到相关端点),但这将需要重写一个真正的淫秽行为代码量。

Thus my question: 因此,我的问题是:

How can I prevent the form from submitting, without doing so? 如何避免不提交表单?

Thanks! 谢谢!

My solution: 我的解决方案:

  if scope.valid is true
    angular.element("form.ng-valid").find("input:submit").click(->
      true
    )
  else
    angular.element("form.ng-invalid.ng-dirty").find("input:submit").click(->
      false
    )

The code above in a method triggered by the a watch, just after the $setValidity in a directive 上面的代码由watch触发,位于指令中$ setValidity之后

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM