Remote validation for a field which depends on others

淺唱寂寞╮ 提交于 2019-12-11 02:49:35

问题


How can I get parsley to include more fields in its AJAX-Calls, so that I can test dependencies between these fields?

Example:

<form method="post" action="/myscript.php" data-parsley-validate>
   <input type="text" name="i1" 
          data-parsley-group="g1" 
          data-parsley-remote="/myvalidator.php"
   />
   <input type="text" name="i2" 
          data-parsley-group="g1" 
          data-parsley-remote="/myvalidator.php"
   />

   <button type="submit">Send</button>
</form>

Currently the $_POST-data only contains the field being validated, but I'd need to know the input of other fields in the same group. BTW, this setup triggers other questions, like the sequence of validation (i1 cannot be evaluated before i2 is filled, too - but I'd be happy to only validate i2 and pass the value of i1 in that call).


回答1:


I have managed to send additional parameters by setting them as the default for jQuery ajax. For example, at the bottom of the page where my validator is used:

<script type="text/javascript">
    $.ajaxSetup({
            beforeSend: function(xhr, settings) {
                settings.url += "&id=" + $('#host').val();
            }}
    );
</script>

The jQuery documentation strongly advises against using the setup method in this manner, since it will affect any other ajax requests on the page. If your page is not too busy though, it may be a reasonable work around until something better becomes available in the Parsley library. Read more about it here, http://api.jquery.com/jQuery.ajaxSetup/



来源:https://stackoverflow.com/questions/23675442/remote-validation-for-a-field-which-depends-on-others

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!