我在我的users_controller中有以下内容,因为执行登录需要进一步的逻辑,但是在CakePHP中完成默认验证会停止,因此我试图强制它验证用户名和密码在进行帐户检查之前,请先填写字段一切正常,但这。我已经尝试添加两个if语句(下面)来检查用户名/密码是否为空,但是只要页面加载它们为空并且验证框显示。CakePHP登录用户名/密码验证
我很难为如何实现这一目标。任何帮助将不胜感激。
function login() {
if($this->Auth->user()) {
$this->redirect(array('controller'=>'shows'));
}
if(empty($this->data['User']['username'])) {
$this->User->validationErrors['username'] = "Please enter a username";
}
if(empty($this->data['User']['password'])) {
$this->User->validationErrors['password'] = "Please enter a password";
}
if(!empty($this->data['User']['username'])) {
// unset unrequired validation rules
unset($this->User->validate['username']['unique']);
// validate form
$this->User->set($this->data);
if($this->User->validates()) {
// update Last Login date
$this->User->id = $this->User->_user['User']['id'];
$this->User->saveField('last_login',date("Y-m-d H:i:s"));
// save User to Session and redirect
$this->Session->write('User', $this->User->_user);
$this->Session->setFlash('You have successfully logged in.','default',array('class'=>'flash_green'));
//$this->redirect(array('controller'=>'shows', 'admin'=>FALSE));
} else {
$this->Session->setFlash('Incorrect username/password combination.','default',array('class'=>'flash_red'));
$this->redirect(array('controller'=>'users', 'action'=>'login', 'admin'=>FALSE));
}
}
}
users_controller beforeFilter()
function beforeFilter(){
parent::beforeFilter();
$this->Auth->allow('register');
}
app_controller beforeFilter和组件
var $components = array('Session', 'Auth' => array(
'loginAction' => array('controller'=>'users','action'=>'login', 'admin'=>false),
//'logoutRedirect' => array('controller'=>'users','action'=>'logout'),
'loginRedirect' => array('controller'=>'shows', 'action'=>'index'),
'autoRedirect' => false,
'authorize' => 'controller')
);
function beforeFilter() {
$this->Auth->allow('home');
$this->set('admin', $this->_isAdmin());
$this->set('logged_in', $this->_loggedIn());
$this->set('users_username', $this->_usersUsername());
}
我想我知道了,在登录功能结束后,我将它重定向回登录操作,因此重置了验证。 '$ this-> redirect(array('controller'=>'users','action'=>'login','admin'=> FALSE));'。只是有一个奇怪的问题与密码跳过空的验证和长度的警告,而不是... – medoix
修复了这一点,'模型验证'的'最后'=>真。这是cakephp对每个规则进行迭代的方式。 – medoix