如何在任何視圖中驗證更多操作,如創建用戶或編輯器。 想要在不同的行動中有更多不同的規則。CakePHP 2.0用戶模型:如何驗證多個操作
我用我的驗證在我的模型,像這樣的例子:
<?
App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel {
public $name = 'User';
public $primaryKey = 'id';
public $validate = array(
'username' => array(
'first'=>array(
'rule'=>'notEmpty',
'message'=>'Your username is empty!',
'last'=>true
),
'second'=>array(
'rule'=>'isUnique',
'message'=>'the username is already taken!'
)
),
'password' => array(
'rule' => 'notEmpty',
'message' => 'Your password is empty!'
)
);
public function beforeSave() {
if(isset($this->data[$this->alias]['password'])) {
$this->data['User']['password'] = AuthComponent::password($this->data[$this->alias]['password']);
}
return true;
}
}
?>
,但我有我想要的使用驗證其他方式就像不同的操作:
'username' => array(
'first'=>array(
'rule'=>'notEmpty',
'message'=>'Your username is empty!',
'last'=>true
),
'second'=>array(
'rule'=>array('minlength',5),
'message'=>'The Username is to short!'
)
),
'password' => array(
'rule' => 'notEmpty',
'message' => 'Your password is empty!'
)
);
在
相同的模型和相同的控制器是可能的。
你的問題沒有任何意義,也沒有細節。您目前使用哪些代碼,以及您嘗試執行哪些操作? – 2011-12-25 04:12:53
希望這將有助於更好的瞭解 – webfacer 2011-12-25 13:22:31