I have one model extending AR class with specific rules. But now i need to insert row into this table, but with other rules. Is i need to create other model with new rules, or it is possible to define orther rules?
Asked
Active
Viewed 918 times
1
3 Answers
5
You can set validation scenario. For example:
$model = new Post();
$model->scenario = 'new_line';
$model->attributes = $_GET['data'];
if ($model->validate()){
$model->save(false);
}
in your model:
public function rules()
{
return array(
array('username, text', 'required','on' => 'new_line')
);
}
In model rules all array lines must have key "on", else this rules will not apply.
Read more here.

iurii_n
- 1,330
- 10
- 17
-
To make it shorter, you can even do $model = new Post('new_line'); – MrSoundless Dec 28 '12 at 15:29
1
If you are extending your class (active records) then you can actually just override your rules() function i.e.:
class User extends ActiveRecord(){
function rules(){
return array(array(
// Nomrally a rule
))
}
}
And then make your next class:
class User_extended extends ActiveRecord(){
function rules(){
return array(array(
// Nomrally a rule
))
}
}
And that should be it. You can then call the User_extended
class and your rules will apply to the parent User
class since Yii grabs the rules in a $this
context and $this
will be your child class.
But you can also use scenarios here, but it might get dirty especially if you need to override other methods.

Sammaye
- 43,242
- 7
- 104
- 146
0
thx. Now i'm trying to use this
/**
* @param string $attribute fields names wich should be validated
* @param array $params additional params for validation
*/
public function ValidatorName($attribute,$params) { … }

user1279525
- 539
- 1
- 6
- 12
-
If your not using scenarios You'll need to bind it still, if on `beforeValidate` you bind it you should be fine. – Sammaye Dec 24 '12 at 09:50