Question

In Yii, the same form is generally used for Create and Update. So, if I have fields like Email, Password, ...other_fields.... during Create but I do not want to display Email and Password field specifically during update, but all other remaining fields should be there in Update. How can this be done, without changing the _form.php

Also, there are more instances, like - date_entered, date_updated etc., which are never required to be displayed, but evaluated internally and stored into table. Hidden fields will not be useful as, they will still be visible in the source. The only way, I can think of is, by completely removing these fields from the _form.php

I have tried with rules() like 'safe' and also used scenarios, but I could not solve this problem.

Any help in this regard, will be highly appreciated.

Was it helpful?

Solution

For edit/update action, isNewRecord field is set true / 1 in model object.

Like

<?php
if($model->isNewRecord)
{
   $form->textField ......
}
?>

Just check in this field in _form.php and add email and password field if it is false / 0.

OTHER TIPS

What you are trying to do is really simple.

You can just create another form lets say, _form2 and copy paste the original data from the _form into it and just remove the data that you don't want to be filled.

So after that, lets say that you don't want email to be there when you update, so go into the controller and edit the action like so and render the other form :

public function actionUpdate($id){
$model = new WhateverModel();
// Lets say that you want to insert your own data it any field, do it like this :
$model->whateverattribute_column = $whateverData;
#some code is here... yada yada
$this->render('update',array('model'=>$model));

}

After that you can render the partial in the 'update' view like so :

<?php $this->renderPartial('_form2',array('model'=>$model)); ?>

Done!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top