我陷入了 cake php 的问题。我想仅在前端显示两个字段时才在模型内添加验证规则。基本上它们是隐藏的。更改选择框时会显示它们,并且如果它们可见,我希望它们是必需的。 让我向您展示我的代码
<?php
echo $this->JqueryValidation->input('website',array(
'type' => 'text',
'label' => 'Website',
'div' => true,
'class' => 'form-control',
'id' => 'InputWebsite',
'placeholder' => 'Enter your website'
));
?>
<?php
echo $this->JqueryValidation->input('phone',array(
'type' => 'text',
'label' => 'Phone',
'div' => true,
'class' => 'form-control',
'id' => 'InputPhone',
'placeholder' => 'Enter your contact Number'
));
?>
<script>
$(document).ready(function() {
$('#InputPhone').parent('div').hide();
$('#InputWebsite').parent('div').hide();
$('#purpose').on('change', function(e) {
var optionVal = $(this).val();
if (optionVal == 'Schedule a call') {
$('#InputPhone').parent('div').show();
$('#InputWebsite').parent('div').show();
$("#InputMessage").hide();
$("#InputMessage").val('');
$(".textarea").hide();
} else {
$('#InputPhone').parent('div').hide();
$('#InputPhone').val('');
$('#InputWebsite').parent('div').hide();
$('#InputWebsite').val('');
$("#InputMessage").show();
$(".textarea").show();
}
});
});
</script>
可以吗?
最佳答案
在模型内的 beforeValidate
函数中,检查选择框值是否是验证其他字段所需的值。那么如果是这样,您需要将验证规则添加到您的 $validate
数组中(使用 $this->validate += array(...new Rule...);
).
关于javascript - 在cakePHP 2.*中动态添加验证规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36907740/