在蛋糕中动态添加验证规则PHP 2.*


Dynamically Adding validation rule in cakePHP 2.*

我陷入了蛋糕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>

可以做到吗?

在 Model beforeValidate函数中,检查选择框值是否是验证其他字段所需的值。如果是这样,则需要将验证规则添加到$validate数组中(使用 $this->validate += array(...new rule...); )。