Yii Framework Forum: Using custom fields on a CActiveRecord which aren't on the DB table - Yii Framework Forum

Jump to content

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

Using custom fields on a CActiveRecord which aren't on the DB table Rate Topic: -----

#1 User is offline   petruza 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 12-July 10

Posted 28 July 2010 - 02:11 PM

Hi, I have a model with a date field, but in the form I collect the date through 3 separate dropdowns.
Is it possible to have some automation so I can build the date field from the 3 separate values in beforeSave() or beforeValidate() and if the validation fails, have the selected options be selected again on the form?
Thanks.
0

#2 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,526
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 28 July 2010 - 02:28 PM

The algorithm is:
1) Add 3 properties to the model.
2) In beforeValidate() merge them or do what you need and put the result into the "date" property.
3) Add a validation rule for the "date" property.
4) In afterValidate() method you can check if there are any validation errors for this property (CModel.getError() method). If yes, then you can add 3 more errors for your separate fields (to highlight them in a form).
1

#3 User is offline   petruza 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 12-July 10

Posted 28 July 2010 - 05:09 PM

View Postandy_s, on 28 July 2010 - 02:28 PM, said:

The algorithm is:
[...]

Ok, but how do I do keep the dropdown selections selected when reloading the form with the error messages?
If my dropdowns were table fields, they would get selected because of the ActiveRecord features.

Currently I do this manually:
echo CHtml::activeDropDownList( $userProfileModel, 'BirthDay', $days,
                	array( 'options'=> array ( 
                			$_POST['UserProfile']['BirthDay'] => 
                				array( 'selected' => true ) ) ) );


And also how do I get those custom attributes automatically populated from the form submission so I can use them to build the date in beforeValidate().
The attributes are named the same as the form fields.
0

#4 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 28 July 2010 - 05:43 PM

View Postpetruza, on 28 July 2010 - 05:09 PM, said:

...
And also how do I get those custom attributes automatically populated from the form submission so I can use them to build the date in beforeValidate().
The attributes are named the same as the form fields.


Just add them as public properties in the model. I necessary, populate them in afterFind().

/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#5 User is offline   petruza 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 12-July 10

Posted 28 July 2010 - 06:01 PM

Oh sorry, they weren't being populated because they weren't declared as safe nor they had any rules.
Thanks!
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