Yii Framework Forum: How can i validate a non model attribute? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How can i validate a non model attribute? Rate Topic: -----

#1 User is offline   skyblaze 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 106
  • Joined: 07-January 09

Posted 12 January 2010 - 02:10 PM

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?
0

#2 User is offline   ngleich 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 16-April 09

Posted 12 January 2010 - 02:28 PM

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:

if(isset($_POST['yourModel']) {
$model->attributes = $_POST['yourModel'];
$model->newAttribute = $_POST['yourModel']['newAttribute'];
...
}
May be this was your problem, without this you never set the attribute value.
0

#3 User is offline   jayrulez 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 348
  • Joined: 29-July 09

Posted 12 January 2010 - 02:30 PM

How about showing the snippets of what you tried.
php:
foreach(array('cat', 'dog', 'cow') as $animal) echo $animal."\n";

python:
[(animal, print(animal)) for animal in ['cat', 'dog', 'cow']]

ruby:
['cat', 'dog', 'cow'].each {|animal| puts animal}


You say Tomato, I say Tomato.
0

#4 User is offline   horizons 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 123
  • Joined: 18-December 08

Posted 13 January 2010 - 10:07 AM

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() 
{
...
array('newAttribute','safe'),
}


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()
{
          
                if(!($this->newAttribute))
                {
                    $error=Yii::t('yourmodel','You must fill newAttribute');
                    $this->addError('newAttribute',$error);
                }
           
        return parent::beforeValidate();
}

0

#5 User is offline   Sander 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 169
  • Joined: 03-November 09
  • Location:Amsterdam, Netherlands

Posted 13 January 2010 - 10:22 AM

View Postjayrulez, on 12 January 2010 - 02:30 PM, said:

How about showing the snippets of what you tried.

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.
0

#6 User is offline   _bogus_ 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 14-January 10

Posted 14 January 2010 - 04:48 PM

I have the same problem here :/
0

#7 User is offline   Sander 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 169
  • Joined: 03-November 09
  • Location:Amsterdam, Netherlands

Posted 14 January 2010 - 06:27 PM

Well, your problem is also not going to be resolved without showing us any code.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users