【问题标题】:Cakephp custom validation on dependent fieldsCakephp对依赖字段的自定义验证
【发布时间】:2011-12-26 11:58:53
【问题描述】:

我需要在我的模型中进行自定义验证,其中字段 1 不能重复,字段 2 将区分它,例如复合键。如下所示。

    public $validate = array(
    'field1' => array(
        'required' => array(
            'rule' => 'notEmpty',
            'message' => 'Can\'t left blank.'
        ),
        'unique' => array(
            'rule' => array('validateNumberType'),
            'message' => 'Already exist'
        ),
        )
    );
    function validationRule() {
    $validation_var = $this->find('first', array('conditions' => array('Model.field1' => 'field1', 'Model.field2' => 'field2')));
    return $validation_var;
}

我试过了,还是不行,是不是函数有问题?

【问题讨论】:

  • 永远不要忘记你的规则中的 last=>true!否则你的错误信息就会混乱

标签: php validation cakephp cakephp-1.3


【解决方案1】:

将您的函数名称更改为 validateNumberType,就像您在规则中定义的那样,例如:

公共 $ 验证 = 数组( 'field1' => 数组( '必需' => 数组( '规则' => 'notEmpty', '消息' => '不能留空。' ), '独特' => 数组( '规则' => 数组('validateNumberType'), '消息' => '已经存在' ), ) ); 函数 validateNumberType() { $validation_var = $this->find('first', array('conditions' => array('Model.field1' => 'field1', 'Model.field2' => 'field2'))); 返回 $validation_var; }

请参见此处,例如:Validation for Dependent Type

【讨论】:

    【解决方案2】:
    var $validate = array(
    'slug' => array(
    'rule' => 'validationRule',
    'message' => 'error message'
    )
    );
    function validationRule($check) {
    //validation
    //$check is value of the field
    }
    

    【讨论】:

      【解决方案3】:
      var $validate = array(
         'slug' => array(
              'rule' => 'validationRule',
              'message' => 'error message'
          )
      );
      function validationRule($check) {
          //validation
          //$check is value of the field
          //$this->data is value of whole submited form
      
          // return true if validate, false either
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-09-07
        • 2016-05-27
        • 2011-04-27
        • 1970-01-01
        • 1970-01-01
        • 2011-01-01
        • 1970-01-01
        相关资源
        最近更新 更多