Have An Update Form On The View Page

Hello everybody,

I would like to give the possibility to a user to update his own information directly from his view page. Like we can find on "admin" page with the "advanced search" link, the user would have the possibility to click on link which would collapse an update form with the save button of course.

I speak about the search link cause I tried to use its implementation to do the same with update from on view. However I didn’t succeed… I tried many things, playing with controller… render… forms… _view etc… But, the best thing I get to do is to display the form… but when I tried to save it, nothings happen. does anybody have an idea how to manage it ?

thanks

Post your view and controller code please.

view :


<?php Yii::app()->clientScript->registerScript('Update', "

$('.update-button').click(function(){

	$('.update-form').toggle();

  alert($.fn.yiiGridView.update('update-form'));

});

$('.update-form form').submit(function(){

	

});

")

?>


<?php $this->widget('zii.widgets.CDetailView', array(...

?>


<?php echo CHtml::link('Change the information','#',array('class'=>'update-button')); ?>

<div class="update-form" style="display:none">

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

</div>

controller :


	public function actionView($id)

	{

		$this->render('view',array(

			'model'=>$this->loadModel($id),

		));

	}


public function actionUpdate($id)

	{

		$model=$this->loadModel($id);


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['NewInfo']))

		{

			$model->attributes=$_POST['NewInfo'];

			if($model->save())

				{

         

          Yii::app()->user->setFlash('success', '<p class="alert-text"><strong>Entry successfully updated.</strong></p>');

          if (Yii::app()->request->isAjaxRequest)

        {

          echo CJSON::encode(array(

            'status'=>'success', 

            'div'=>Yii::t('strings',"Entry successfully updated")

            ));

          Yii::app()->end(0);               

        }

      else

				$this->redirect(array('admin'));

      }

    }

    

    if (Yii::app()->request->isAjaxRequest)

    {

      echo CJSON::encode(array(

        'status'=>'failure', 

        'div'=>$this->renderPartial('_form', array('model'=>$model), true)));

      Yii::app()->end(0);               

    }

    else

      $this->render('update',array('model'=>$model,));

  }

I think the problem comes from the javascript… I don’t really know how to submit my form to the actionUpdate url…

Another Idea would be to create a button with a dialog box like I did on admin pages anyway… the update works well on its own page

The most simple solution is probably to just invoke your update code from the view action. Here is a rough idea of what I mean:


public function actionView($id) {

  if(Yii::app()->request->isPostRequest) $this->actionUpdate($id);

  $this->render('view',array( 'model'=>$this->loadModel($id)));

}

I haven’t tested this at all but I’ve used something similar before and it worked well for my situation.

(Also, if speed is a concern you’ll probably want to work around the fact that this will retrieve the record from the DB twice. Because of this, making a separate method for the form post check code may not be a bad idea.)

Thanks for your reply krowe, I decided to use a dialog box with a button to call it and display my form then save it. That was the most simple ;)