Where to put error reporting code

Hi All,

I am new to Yii and MVC style coding so I was wondering where I should put some code that reports an error back to the user.

I have a simple schema with a one to many relationship enforced by the db with a "RESTRICT" for ON DELETE

1 color -> many Collectors

I would like to display a nice popup error to the user if they try and delete a color that is being used in the foreign key by Collectors.

I tried putting some code in the BeforeDelete() method of Color’s model. But I could not work out a way to display a dialog from there and it really didnt seem to be the right place.


//   public function beforeDelete()

//   {

//      if(parent::BeforeDelete())

//      {

//         $c=$this->countCollectorsUsingColor();

//         if($c>0)

//         {

//            return false;

//         }

//      }

//      

//     return true;

//   }

I then tried creating a method in Colors model that returned count of references and called that from ColorsController actionDelete method - There the popup is kinda of shown but becomes a whole page and not a popup.


   public function countCollectorsUsingColor()

   {

      return count(Collector::model()->findAll("color_id=".$this->id));

   }


   public function actionDelete($id)

   {

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

      {

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

         $count=$model->countCollectorsUsingColor();

         if($count>0)

         {

            $this->beginWidget('zii.widgets.jui.CJuiDialog', array(

               'id'=>'mydialog',

               // additional javascript options for the dialog plugin

               'options'=>array(

                  'title'=>'Cannot delete color because referenced by collectors',

                  'autoOpen'=>true,

                  'modal'=>true,

                  'width'=>'auto',

                  'height'=>'auto',

               ),

            ));

            echo "Cannot delete this color because:";

            echo "There are $count collectors using this color.";

            echo "Please edit them first.";

            echo $this->renderPartial('_formDialog', array('model'=>$model));

            $this->endWidget('zii.widgets.jui.CJuiDialog');


            // the link that may open the dialog

//            echo CHtml::link('open dialog', '#', array( 'onclick'=>'$("#mydialog").dialog("open"); return false;',));

         }

         else

         {

            // we only allow deletion via POST request

            $this->loadModel($id)->delete();


            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

            if(!isset($_GET['ajax']))

               $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));

         }

      }

      else

         throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');

   }

So the real question is how and where should I do this properly. I would like to keep the "Confirm Delete" option from the menu in thee generated view code and then display an error saying that something like "This color is used X times"

Either a CJuiDialog or the JNotify solutions would be cool.

TIA

Adrian

You can try with:




    public function accessRules() {

        return array(

            array('allow', // allow authenticated users to access all actions

                'actions' => array('delete'),

                'expression' => '$this->countCollectorsUsingColor()>0'

            )

        );

    }



Ok but how do I get this to display a meaningful popup to the user on the reason why this failed - tying this I got a a Yii 403 error from just using the validation rules.

Ok, sorry, … rollback. First of all, why do you show a delete option if a user cant do it? You can show "in use" or "delete". My expression maybe is incorrect because you need a static method like:




                'expression' => 'YourClass::countCollectorsUsingColor()>0'

I think that it is a waste of resources to calculate whether the delete option should be show when 99% the user shall just be viewing the record/records.

I got the expression working - I think it provides a useless error message to the user. which comes out like




Error 403

You are not authorized to perform this action.



I really really want to display a popup or a JNotify message. That is my requirement for this question :slight_smile:

Ok, where do you need this confirmation?

In normal coding I’ll write

<a href=“return false;” onclick=“if(confirm(‘Do you really want to do this?’))document.location.href=‘path/to/delete’;”>delete</a>

Is it ok?

More info from me. I generated the Model and CRUD from gii - so its all standard. The 2 web locations that the delete button is available are from the "admin.php and "view.php" files.

In view.php gii generated something very similar to what you have - was this the place you were suggesting to put this code

array(‘label’=>‘Delete Color’, ‘url’=>’#’, ‘linkOptions’=>array(‘submit’=>array(‘delete’,‘id’=>$model->id),‘confirm’=>‘Are you sure you want to delete this item?’)),

I have no idea where the confirmation comes from when clicking on the delete button from the admin view (havent found it yet).

I was assuming that I could put something in either the controller or the model (so I dont have to duplicate) and that it would work in both admin.php and view.php. I am happy to edit them as well if something needs to be done to make them display the popup or the JNotify.

This goes to the general question of responsibility in a MVC applications. Who (Model, View, Controller) is supposed to be involved in reporting and error to the User.