我正在研究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
)
)
)
答案 0 :(得分:0)
您的验证对我来说似乎不错但是如果我们不在某个时间设置模型中的数据会导致此类问题。
在验证前添加以下行。
$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/');
}