How to differentiate add and edit state in _form.php?


I have a file field in the _form.php used by both add.php and edit.php

<?php echo $form->labelEx($model,'ImageFileName'); ?>

<?php echo $form->fileField($model,'ImageFileName'); ?>

<?php echo $form->error($model,'ImageFileName'); ?>

In the add state, the form field appears nicely and I have no problem in the controller uploading the file.

But how do I make the edit page to show the uploaded file in the form of a link? (with a small delete icon beside it)

If I share the same _form.php as the add page, the edit page will show the input field minus the filename.

How to differentiate add and edit state in the _form.php?

Or should I create a separate _form_edit.php just to display the uploaded file as a link?

Thanks for helping.


You can pass the variable to _form.php. I.e. in add.php:

<?php $this->renderPartial('_form', array(



)); ?>

And in edit.php:

<?php $this->renderPartial('_form', array(



)); ?>

Then in _form.php you can check it:

<?php echo CHtml::submitButton($update ? Yii::t("Post", "Save") : Yii::t("Post", "Create"), array('name'=>'submitPost', 'class'=>'button1')); ?>

You can also check if the $model is new:

if ($model->isNewRecord)


   create stuffs




   update stuffs


Like is done for the submit button.

thks guys!