I have Registration model(which has fields login, password, email, mobile) and want to use some of the fields from Registration(like login and password) in a login action, and also validate it. How can i reuse the model fields, and also Validate the fields based on Scenario.
Asked
Active
Viewed 1,879 times
2 Answers
1
i think you can reuse form model and can write custom validation rules in the form and for each action. for example. hari you just give a try like this...
class RegistrationForm extends CFormModel {
public $login;
public $password;
public $email;
public $mobile;
public function rules() {
return array(
array('login,password','loginValidator','on'=>'login'),
array('login,password,email,mobile','registerValidator','on'=>'register')
);
}
public function loginValidator() {
dummyfunc($this->login,.....);
}
public function registerValidator() {
dummyfunc($this->login,.....);
}
}
Controller part:
$formModel = new RegistrationForm('login');
$formModel->attributes = $_POST['RegistrationForm'];
if($formModel->validate()) {
#..........;
} else {
#..........;
}
have a nice day!!!

David Newcomb
- 10,639
- 3
- 49
- 62

Jaison Justus
- 2,753
- 8
- 47
- 65
0
It is better to use a user module.
This is an example of the directory structure of a user module:
protected/modules/user/
controllers/
LoginController.php
LogoutController.php
RegisterController.php
...
models/
Login.php
Register.php
...
views/
user/
login.php
register.php
...
The directory structure of yii-user might be useful.

jamband
- 926
- 7
- 11