Passing parameters from a form to a model that aren't part of the model

Hi, I’m trying to pass POST variables from my form to my model for processing and error checking however as the value is not part of my model I get an ‘undefined’ error. So:

  1. How can I pass form variables that are not part of my model to the controller

  2. How do I validate these variable using rules() in my model?


ad 1)

simply place in your form controls, but do not use $form->activeXXX, but something like this:

echo CHtml::textField( 'name', $value );

you may access them in action simply: $_POST[‘name’], or Yii::app()->request->postParam( ‘name’, ‘’ );

ad 2)

if those params aren’t part of any model - you must validate them on your own. Rules apply only to model attributes. You may however create another FormModel for this specific form, use its validator and copy parameter to some other model (activerecord for example) after validation succeeds.

Alternatively, you can add the variable to your model:

class User extends CActiveRecord


	public $extra_field;

	public function rules() 


		return array(

                    [other rules],