简体   繁体   English

jQuery使用Coffeescript进行验证

[英]Jquery Validate with Coffeescript

SO I can make the following code work in javascript 所以我可以使以下代码在javascript中工作

validateSearchForm:=>
    $(@el).find("#form").validate({
      rules:{
        startDateInputBox:{
          dateISO:true,
          endDateInputBox:{lessThan : "#licenseStart"}
        },
        endDateInputBox:{
          dateISO:true
        }
        searchPurposeBox:"required"


      }



      });

  $.validator.addMethod "lessThan",(value,element)-> 
    //do some stuff
  ,"some comment"

But the lessThan function keeps being undefined in coffee. 但是lessThan函数在咖啡中始终未定义。 Pretty sure this is some syntax error - can anyone help? 可以肯定这是一些语法错误-有人可以帮忙吗?

I'm not sure it will fix your problem, but try to define your objects without {} : 我不确定它是否可以解决您的问题,但是请尝试在不使用{}情况下定义对象:

rules:
    startDateInputBox:
        dateISO: true,
        endDateInputBox:
             lessThan: "#licenseStart"
    endDateInputBox:
         dateISO: true
    searchPurposeBox: "required"

Your code structure is like this: 您的代码结构如下:

validateSearchForm: =>
    $(@el).find('#form').validate({ ... })
    $.validator.addMethod "lessThan", (value, element) -> 
        #...
    , "some comment"

so you're calling $.validator.addMethod after you try to validate the form. 所以你调用$.validator.addMethod尝试验证的形式。 You should add all your extra validation methods before you try to use them and you should probably add them outside your methods that will be validating forms. 您应先添加所有其他验证方法, 然后再尝试使用它们,并且可能应将它们添加到将要验证表单的方法之外。 Something more like this: 像这样:

# Somewhere in your global application set up...
$.validator.addMethod "lessThan", (value, element) -> 
    #...
, "some comment"

# And then elsewhere...
class Whatever
    #...
    validateSearchForm: =>
        $(@el).find('#form').validate({ ... })

You have to pay very close attention to your indentation in CoffeeScript, the indentation supplies half of your code's structure so it has to be right and it should be consistent. 您必须非常注意CoffeeScript中的缩进,缩进提供了代码结构的一半,因此它必须正确且应保持一致。

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

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