3

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.

2
  • Why dont't you want to change _form? Commented Dec 24, 2013 at 15:34
  • What is the purpose of a strong framework. Write rules(), scenarios within model (i.e., where every attributes are) and this is a question of removing certain attributes (i.e., labels, fields everything...) Commented Dec 24, 2013 at 16:19

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, but I want to stick with CActiveForm and not with CHtml. As such, how do I use it as an argument. For instance, using CHtml, it can be like this CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); But how to apply it within $form->textField etc..., assuming $form is an instance of the CActiveForm
You can just copy paste the content of _form.php in to _update_form.php for example, and edit it's content as you wish. You only have to render thus new file in your controller update method.
As @veelen said, do as follows. If you don't want to edit _form.php, then just copy the _form.php and save it as _edit_form.php. Then update the code as you want. render this _edit_form.php in your update function in controller.
Yes, I got it that way. But I was thinking not to apply this conditional statement, within the whole block.
Yes. That's why we are using conditional block. :-)
|
3

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!

1 Comment

I think for what you are trying to achieve, this is the correct answer. And should be checked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.