Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
cakephp模型数据验证中的问题_Php_Validation_Cakephp_Cakephp 2.3 - Fatal编程技术网

cakephp模型数据验证中的问题

cakephp模型数据验证中的问题,php,validation,cakephp,cakephp-2.3,Php,Validation,Cakephp,Cakephp 2.3,我正在开发cakephp v2.3。当我通过$this->User->validates验证数据时,即使存在值,它也将始终返回false 这是我的验证规则 public $validate = array( 'first_name' => array( 'notEmpty' => array( 'rule' => 'notEmpty', 'message' => 'Th

我正在开发
cakephp v2.3
。当我通过
$this->User->validates验证数据时,即使存在值,它也将始终返回false

这是我的验证规则

public $validate = array(
        'first_name' => array(
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'This field cannot be left blank.',
                'last' => true,
                'required' => true,
            ),
            'size' => array(
                'rule' => array('maxLength', 50),
                'message' => 'This field must be no larger than 50 characters long.'
            ),
        ),
        'last_name' => array(
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'This field cannot be left blank.',
                'last' => true,
                'required' => true,
            ),
            'size' => array(
                'rule' => array('maxLength', 50),
                'message' => 'This field must be no larger than 50 characters long.',
                'allowEmpty' => true,
            ),
        ),

        'email' => array(
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'This field cannot be left blank.',
                'last' => true,
                 'required' => 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,
            ),
        ),
        'password' => array(
            'rule1' => array(
                'rule' => array('minLength', 6),
                'message' => 'Passwords must be at least 6 characters long.',
                 'required' => true,
            ),

        ),
        'verify_password' => array(
            'rule' => 'validIdentical',
        ),
        'current_pass' => array(
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'This field cannot be left blank.',
                'last' => true,
            ),
            'match' => array(
                'rule' => 'validICurrent',
            )
        ),
        'gender' => array(
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'This field cannot be left blank.',
                'last' => true,
            ),
        ),
        'mobile' => array(
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'This field cannot be left blank.',
                'last' => true,
                 'required' => true,
            ),
            'numeric' => array(
                'rule' => 'numeric',
                'message' => 'Please enter valid phone numbers.',
            )
        ),
        'image' => array(
            'SizeLimit' => array(
                'rule' => array('isUnderPhpSizeLimit', false),
                'message' => 'File exceeds upload filesize limit',
            ),
            'FormSizeLimit' => array(
                'rule' => array('isUnderFormSizeLimit', false),
                'allowEmpty' => true,
                'message' => 'File exceeds form upload filesize limit'
            ),
            'CompletedUpload' => array(
                'rule' => array('isCompletedUpload', false),
                'message' => 'File was not successfully uploaded'
            ),
            'ValidExtension' => array(
                'allowEmpty' => true,
                'rule' => array('isValidExtension', array('gif', 'png', 'jpg', 'jpeg'), false),
                'message' => 'File does not have a gif, png, ,jpg and  jpeg extension'
            )
        ),
    );
这就是我验证的方式

    if ($this->request->is("post")) {
        if (!empty($this->request->data)) {

            if (!$this->User->validates($this->request->data)) {
                $this->Session->setFlash(__('The Information could not be saved. Please, try again.'), 'message', array('class' => 'danger'));
                $this->redirect(Router::url('/', true) . 'sign-up/');
            }
        }
当我尝试

die(debug($this->User->invalidFields()));
它会回来的

array(
    'password' => '*****',
    'last_name' => array(
        (int) 0 => 'This field cannot be left blank.',
        (int) 1 => 'This field cannot be left blank.'
    ),
    'email' => array(
        (int) 0 => 'This field cannot be left blank.',
        (int) 1 => 'This field cannot be left blank.'
    ),
    'mobile' => array(
        (int) 0 => 'This field cannot be left blank.',
        (int) 1 => 'This field cannot be left blank.'
    )
)
这是我正在传递的数据

Array
(
    [User] => Array
        (
            [role_id] => 2
            [first_name] => kapil
            [last_name] => sharma
            [email] => kapiltest@mailinator.com
            [password] => abc@123
            [verify_password] => abc@123
            [mobile] => 1234567890
            [gender] => Male
            [image] => Array
                (
                    [name] => Screenshot_61.png
                    [type] => image/png
                    [tmp_name] => /tmp/phppYBJQF
                    [error] => 0
                    [size] => 50611
                )
        )
)

您的验证对我来说似乎很好,但是如果我们不在模型中设置数据的话,就会导致这种问题

在验证前添加以下行

$this->User->set($this->request->data);
所以代码应该是这样的

$this->User->set($this->request->data); //set data in model

if (!$this->User->validates()) {
    $this->Session->setFlash(__('The Information could not be saved. Please, try again.'), 'message', array('class' => 'danger'));
    $this->redirect(Router::url('/', true) . 'sign-up/');
}