January 12, 2010, 7:10pm
I have added a public property to my active record model class that doesn’t map to a real table column but i have that property/attribute in a form and i want to validate it with the current model. I have tried to add a validation rule to that public property but nothing happens on it. How can i achieve this?
January 12, 2010, 7:28pm
When you add a public attribute to an AR, (at least in Yii 1.0) you must have to set the attribute in the controller like this:
$model->attributes = $_POST[‘yourModel’];
$model->newAttribute = $_POST[‘yourModel’][‘newAttribute’];
May be this was your problem, without this you never set the attribute value.
January 12, 2010, 7:30pm
How about showing the snippets of what you tried.
January 13, 2010, 3:07pm
add it to your rule as safe to be able to set it via $model->attributes =$_POST[‘yourModel’];
public $newAttribute=int; // or whatever type
public function rules()
this must work, afterwards you should also be able to set a specific rule for your type to have validation.
you can also add something like this for customized validation of your attribute
protected function beforeValidate()
$error=Yii::t('yourmodel','You must fill newAttribute');
January 13, 2010, 3:22pm
Yes, please do because what you describe should be working. Have a look at the LoginForm generated by
yiic webapp, which does the same and works fine.
January 14, 2010, 9:48pm
I have the same problem here
January 14, 2010, 11:27pm
Well, your problem is also not going to be resolved without showing us any code.