Yii Framework Forum: How to preset values for forms - Yii Framework Forum

Jump to content

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

How to preset values for forms Rate Topic: -----

#1 User is offline   Dale 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 06-July 10

Posted 14 July 2010 - 09:22 PM

I'm using the gii generated code but i would like to preset some of the form values.
At a guess I should be setting the value in the controller but my attempts at doing this have so far failed.

So I have in the staffcontroller
	
public function actionCreate()
{
	$model=new Staff;

	if(isset($_POST['Staff']))
	{
		$model->attributes=$_POST['Staff'];
		if($model->save())
			$this->redirect(array('view','id'=>$model->id));
	}
	$this->render('create',array(
		'model'=>$model,
	));
}


the view makes use of the widget CActiveForm

I'm trying to preset a required value - companyid , and hide it from the user as they dont need to know about it.
0

#2 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,353
  • Joined: 12-October 09
  • Location:Croatia

Posted 15 July 2010 - 01:35 AM

So... you can set that after the attribute assignement like:

                ...
                $model->attributes=$_POST['Staff'];
                $model->companyid=<your value>;
                if($model->save())
                        $this->redirect(array('view','id'=>$model->id));
                ...

Find more about me.... btw. Do you know your WAN IP?
0

#3 User is offline   Dale 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 06-July 10

Posted 15 July 2010 - 02:42 AM

well that was simple - Thank you it works

and to disable the CActiveForm Validation on the companyid field was to remove companyid form the required rules array in the model.
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