2016-10-28 97 views
3

我在选择框上使用parsley.js和自定义验证器。parsley.js动态错误消息

我希望验证器上的错误消息使用选择框中的当前选定选项+一些更多文本。

不幸的是,似乎我无法动态地更改错误消息。

我的代码

window.Parsley 
.addValidator('attachedEmployee', { 
    requirementType: 'string', 
    validateString: function(value, arg1, arg2, arg3) { 
     var employeeID = $("#medarbejder_navn").val(); 
     //No employee is selected if ID is 1 
     if(employeeID == 1) 
     { 
      //Only shifts which can be made with no employees are "accepted" and "free" 
      if(value == "G" || value == "L" || value == "A") 
       return true; 
      else 
      { 
       return false 
      } 

     } 
     else 
      return true; 
    }, 
    messages: { 
     da: "%s" 
    } 
}); 

这似乎在验证只要它被连接增加了错误信息,这意味着它是正确的,从一开始走锁定。

任何人都知道如何解决这个问题?

回答

1

与这只是挣扎,发现multiple custom error message support per field by using parsley.js

答案要更改错误信息,现在我这样做:

window.Parsley 
    .addValidator('atLeast', { 
     validateString: function(value, requirement){ 
      window.Parsley.addMessage('en', 'atLeast','Fill at least ' + requirement + ' input'); 
      return this.validateAtLeast(Number(requirement)); // custom function 
     }, 
    });