我想要有条件的验证,在更新我的密码字段时隐藏并且密码验证是“6到15个”字符!所以密码以哈希格式存储在数据库中,因此不允许我更新。我的型号代码如下,
<?php
class User extends AppModel {
public $belongsTo = array(
'Group',
'City',
'Area',
);
public $validate = array(
'name' => array(
'rule' => 'alphaNumeric',
'required' => true,
'message' => 'Only alphabets and numbers are allowed!'
),
'email' => array(
//~ 'rule1' => array(
//~ 'rule' => 'isUnique',
//~ 'message' => 'Email address already exists!',
//~ 'last' => true
//~ ),
'rule2' => array(
'rule' => 'email',
'message' => 'Invalid Email!',
'last' => true
)
),
//~ 'password' => array(
//~ 'rule' => array('between', 6, 15),
//~ 'required' => true,
//~ 'message' => 'Password must be of 6 to 15 characters'
//~ ),
//~ 'confirm_password' => array(
//~ 'rule' => 'confirmPassword',
//~ 'message' => 'Confirm password do not match!'
//~ ),
//~ 'address' => array(
//~ 'allowEmpty' => false,
//~ 'required' => true,
//~ 'message' => 'Description is required'
//~ ),
//~ 'phone_number' => array(
//~ 'rule' => 'phone',
//~ 'allowEmpty' => false,
//~ 'required' => true,
//~ 'message' => 'Phone Number is required'
//~ )
);
public function beforeSave() {
if (isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
}
return true;
}
public function confirmPassword() {
debug('model');
if ($this->data[$this->alias]['confirm_password'] != '') {
debug('model in');
if(strcmp($this->data[$this->alias]['password'], $this->data[$this->alias]['confirm_password']) == 0) {
debug('model in in');
return true;
}
}
return false;
}
}
我也有确认密码验证。所以告诉我任何条件验证的解决方案!谢谢你的帮助!