我有一个视图,其中有 2 个表单,一个用于登录,一个用于注册,如下所示:signup.ctp //my view
<div>
<?php
echo $this->Form->create("Tbluser");
echo $this->Form->hidden('formsent', array('value' => 'signup'));
echo $this->Form->input('username' ,array('label'=>'Username'));
echo $this->Form->input('password' ,array('label'=>'Password','type' => 'password'));
echo $this->Form->input('email' ,array('label'=>'Email'));
echo $this->Form->end('Register');
?>
</div>
<div>
<?php
echo $this->Form->create("Tbluser"); ?>
echo $this->Form->hidden('formsent', array('value' => 'login'));
echo $this->Form->input('username' ,array('label'=>"Username :"));
echo $this->Form->input('password' ,array('label'=>"Password :",'type' => 'password'));
echo $this->Form->end('Login');
?>
<div>
我用于两种形式的模型如下:
<?php
class Tbluser extends AppModel{
public $validate = array(
'username'=>array(
array(
'rule'=>'alphaNumeric',
'allowEmpty'=>false,
'message'=>'Invalide Username!'
),
array(
'rule' => array('minLength', '4'),
'message' => 'Username has to be more than 3 chars'
),
array(
'rule'=>'isUnique',
'message'=>'Username already taken!'
)
),
'password' => array(
array(
'rule' => 'alphaNumeric',
'allowEmpty'=>false,
'message' => 'Password must be AlphaNumeric!'
),
array(
'rule' => array('minLength', '4'),
'message' => 'Username has to be more that 3 chars'
)
),
'email'=>array(
array(
'rule'=>array('email',true),
'required'=>true,
'allowEmpty'=>false,
'message'=>'Invalide email adress!'
),
array(
'rule'=>'isUnique',
'message'=>'Mail adress already taken!'
)
)
);
}
?>
我使用的控制器如下:
<?php
class TblusersController extends AppController
{
public $uses = array(
'Tbluser'
);
public function signup()
{
if ($this->request->is('post')) {
if ('signup' === $this->request->data['Tbluser']['formsent']) {
// Registration part.
}else if('login' === $this->request->data['Tbluser']['formsent']){
//Login part
}
}
}
?>
我的 AppController 看起来像:
<?php
class AppController extends Controller {
public $helpers = array('Form', 'Html');
public $components = array('Session','Cookie','Auth'=>array(
'authenticate'=>array(
'Form' => array(
'userModel' => 'Tblforumuser',
'fields' => array(
'username' => 'username',
'password' => 'password'
)
)
)
));
}
?>
现在,如果我在注册表单中填写错误的数据并提交它,则会发生验证,但也会在登录表单字段中进行,所以我如何将验证设置为仅适用于该注册表单而不适用于两个表单?谢谢。