Cakephp:默认验证

Cakephp:默认验证,cakephp,Cakephp,我正在向用户的控制器发送请求。这两种方法都返回相同的结果: $this->User->validates(); 或 是因为cakephp正在对请求数据执行默认验证吗 如何执行自定义验证?是否需要设置新的数据值?只有在模型中定义验证时,cakephp才会验证 下面是一个如何在模型中设置验证的示例 /** * Validation * * @var array * @access public */ public $valida

我正在向用户的控制器发送请求。这两种方法都返回相同的结果:

$this->User->validates();

是因为
cakephp
正在对请求数据执行默认验证吗


如何执行自定义验证?是否需要设置新的数据值?

只有在模型中定义验证时,cakephp才会验证

下面是一个如何在模型中设置验证的示例

/**
     * Validation
     *
     * @var array
     * @access public
     */
    public $validate = array(
        'first_name' => array(
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'This field cannot be left blank.',
                'last' => true,
            ),
            'size' => array(
                'rule' => array('maxLength', 50),
                'message' => 'This field must be no larger than 50 characters long.'
            ),
        ),
        'last_name' => array(
            'size' => array(
                'rule' => array('maxLength', 50),
                'message' => 'This field must be no larger than 50 characters long.',
                'allowEmpty' => true,
            ),
        ),
        'username' => array(
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'This field cannot be left blank.',
                'last' => true,
            ),
            'size' => array(
                'rule' => array('maxLength', 255),
                'message' => 'This field must be no larger than 255 characters long.'
            ),
            'isUnique' => array(
                'rule' => 'isUnique',
                'message' => 'The username has already been taken.',
                'last' => true,
            ),
        ),
);
如果您想要自己的验证,您可以这样做

 public $validate = array(
          'email' => array(
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'This field cannot be left blank.',
                'last' => true,
            ),

            'checkMail' => array(
                'rule' => 'checkMail',
                'message' => 'Please provide a valid email address.',
                'last' => true,
            ),
            'isUnique' => array(
                'rule' => 'isUnique',
                'message' => 'Email address already in use.',
                'last' => true,
            ),
        ),
    );

      /**
         * check email format
         */
        function checkMail() {
            $email = $this->data['User']['email'];
            $dotInString = substr_count(strstr($email, '@'), '.');

            if ($dotInString > 2)
                return false;

            if ($dotInString > 1)
                $preg_match_string = "/^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+(\.[a-zA-Z]{2,2})+(\.[a-zA-Z]{2,6})+$/";
            else
                $preg_match_string = "/^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+(\.[a-zA-Z]{2,6})+$/";

            if (preg_match($preg_match_string, $email) > 0) {
                return true;
            } else {
                return false;
            }
        }

有关更多信息,请参阅检查Cakephp文档我已经设置了验证规则,但我想知道Cakephp在默认情况下是验证请求数据还是其他数据,基本上,请求->数据在默认情况下是否成为模型活动数据。如果您仅尝试保存任何信息,则将对要保存的数据进行验证。如果您仍有疑问,请在问题中提供更多信息。根据您的问题,您似乎在询问如何启动验证以及如何进行自定义验证。
 public $validate = array(
          'email' => array(
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'This field cannot be left blank.',
                'last' => true,
            ),

            'checkMail' => array(
                'rule' => 'checkMail',
                'message' => 'Please provide a valid email address.',
                'last' => true,
            ),
            'isUnique' => array(
                'rule' => 'isUnique',
                'message' => 'Email address already in use.',
                'last' => true,
            ),
        ),
    );

      /**
         * check email format
         */
        function checkMail() {
            $email = $this->data['User']['email'];
            $dotInString = substr_count(strstr($email, '@'), '.');

            if ($dotInString > 2)
                return false;

            if ($dotInString > 1)
                $preg_match_string = "/^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+(\.[a-zA-Z]{2,2})+(\.[a-zA-Z]{2,6})+$/";
            else
                $preg_match_string = "/^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+(\.[a-zA-Z]{2,6})+$/";

            if (preg_match($preg_match_string, $email) > 0) {
                return true;
            } else {
                return false;
            }
        }