自定义欧芹验证不起作用

时间:2014-07-05 03:50:53

标签: parsley.js

验证不适用于以下自定义验证(所有其他标准验证html属性都有效)。我已删除了表格中不适用的输入。

 <form id="regForm" role="form" method="post" action="" accept-charset="UTF-8">
<div class="form-group">
        <label for="wards">Your Ward</label>



<select name="fields[ward]" class="form-control">
<option value="" selected="selected">SELECT WARD</option>
<option value="item">item 1</option>
</select>
        <p class="help-block">It is important that you select the ward your current membership records reside in!</p>
      </div>

      <div class="form-group">
        <label for="wardOther">Other Ward or Not in Australia?</label>
        <textarea class="form-control" id="wardOther" name="fields[wardOther]" rows="4"  data-parsley-conditionalrequired='[name=\"fields[ward]\"] option:selected' data-parsley-validate-if-empty data-parsley-success-class="" data-parsley-conditionalrequired-message="This value is required since you did not select an australian ward!"></textarea>
            <p class="help-block">Please tell us the Country, Stake, Ward, and Bishops Name you are attending from</p>
      </div>

<script>
$(document).ready(function() {

  $('#regForm').parsley({
    validators: {
      conditionalrequired: {
        fn: function (value, requirements) {
          console.log('fdsafds');
      if ($(requirements).val() == '' && '' == value)
      return false; 


          return true;
        },
        priority: 32
      }
    }
  });
});

</script>

console.log未触发。

2 个答案:

答案 0 :(得分:1)

客户验证工具不适用于您的情况,因为您没有指定表单名称值和&#34; data-parsley-validate&#34;到表格定义。

<form id="regForm" role="form" method="post" action="" accept-charset="UTF-8">

只需添加name =&#34; regForm&#34;它应该正常工作。

<form id="regForm" name="regForm" role="form" method="post" action="" accept-charset="UTF-8" data-parsley-validate>

答案 1 :(得分:0)

我使用了一点different approach来创建自定义验证器。您可以在此处查看工作示例http://jsfiddle.net/Hmhjy/

window.ParsleyValidator.addValidator('conditionalrequired', 
function (value, requirement) {
    console.log('fdsafds');
    if ($(requirements).val() == '' && '' == value) {
        return false; 
    }

    return false;
}, 32)
.addMessage('en', 'conditionalrequired', 'This value is required since you did not select an australian ward!');

$('#regForm').parsley();