0

So this is what i want to do and this is what my fields table looks like :

|-----------------------------|
|id   |   field  | is_required|
|-----------------------------|
|1    |   email  |    yes     |
|-----------------------------|
|2    |   phone  |    no      |
|-----------------------------|

now i have created a view for this table using gii, which has an option to delete field value, now what i want to do is in my actionDelete() i want to check if the value that the user is trying to delete is required or not (if the table's is_required field is yes or no, if it is yes then the field is required, if it is no then the field is not required), if the field is required i want to show an alert that "this field can't be deleted as it is a required field" else delete. And this is what i have done so far in my controller:

public function actionDelete($id)
    {
            $check=Fields::model()->findByAttributes(array('id'=>$id));
                $required=$check->is_required;
                if($required =='no'){
                    $this->loadModel($id)->delete();

                }elseif($required =='yes'){
                    echo "<script>alert('This field cannot be deleted.!');</script>";
                }


        if(!isset($_GET['ajax']))
            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
    }

but this is not working can any of you point me in the right direction..? Thanks in advance.

Criesto
  • 1,985
  • 4
  • 32
  • 41
  • What do you mean not working? What is being returned? – topher Jan 07 '14 at 07:28
  • it doesnot shows the popup, however it is not being deleted – Criesto Jan 07 '14 at 07:29
  • 1
    It seems that you are trying to delete something that is neither yes nor no e.g null. Also your condition should be an `if-else` not an `if-elseif` if `is_required` can only have 2 states. – topher Jan 07 '14 at 07:34
  • no i have checked the db there is no null value, but you said the right thing i should not use else if, thanks for pointing that out. – Criesto Jan 07 '14 at 07:36
  • Something else, any manipulation of the database should be done using POST requests for security purposes. – topher Jan 07 '14 at 07:43
  • 1
    can you show var_dump($required)? – Let me see Jan 07 '14 at 07:47
  • It is being deleted if i am using `$check->delete();` for `is_required=no` but for `is_required=yes` condition no `alert` is being shown...is there any other way to do this...? – Criesto Jan 07 '14 at 08:56

2 Answers2

2

Here's a better way of doing this.

First, put the condition for deletion in your model instead of the controller. You can use beforeDelete to do this.

public function beforeDelete(){
    if($this->is_required=='no'){
        return true;
    }
    return false;
}

Next update your action to a) use POST instead of GET and b) check whether the record exists

public function actionDelete($id){
    if($this->request->isPostRequest){ //enforce use of POST
        $model=$this->loadModel($id); // loadModel throws an exception if the record doesn't exist
        if($model->delete()){ 
            // do your redirects here
        }else{
            // do something else
        }
    }else{
        throw new CHttpException(400,'Invalid Request');
    }
}
topher
  • 14,790
  • 7
  • 54
  • 70
  • how do i enforce POST from model... i mean from the model i am sending the id through GET how do i change it to post.. and thanks for your answer, this is exactly what i was looking for. – Criesto Jan 08 '14 at 04:35
  • and is there any way to change the default pop up message for delete like if `is_required=yes` then the popup would show `"This field cannot be deleted"` instead of `"Do you really want to delete?"` – Criesto Jan 08 '14 at 04:44
  • @Criesto you can just hide the delete button using the `visible` attribute whenever `required=='yes'` instead as here http://stackoverflow.com/a/9433926/428543. For using POST just look at any gii generated delete button. – topher Jan 08 '14 at 10:04
0

So, I found a way to do this, here it is:

public function actionDelete($id)
{
   $check    = Fields::model()->findByAttributes(array('id'=>$id));
   $required = $check->is_required;
   if($required == 'no') {
     $check->delete();
   } elseif($required == 'yes') {
     throw new CHttpException(400, "This is a required field and cannot be deleted");
   }

   if(!isset($_GET['ajax']))
      $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}

There might be better ways of doing this, please let me know if you guys find any..thanks a lot stackoverflow. :)

Criesto
  • 1,985
  • 4
  • 32
  • 41