5

I am trying to delete a record in Yii, which throws a Error 400. Please do not repeat this request again. It's checking for a post variable, my controller file has
if(Yii::app()->request->isPostRequest)
When I echo my post variable it's blank whereas $_GET has the id which I want to delete, my View file looks like,

echo CHtml::link(CHtml::encode('Delete image'), array('image/delete', 'id'=>$image->id), array('class' => 'delete','confirm'=>'This will remove the image. Are you sure?'));

The access rules has delete to authenticated users, which is right. Tried it with a * too. I also tried sending it as a hidden variable but no good.

Not able to figure out how should I post a form in Yii.

dibs_ab
  • 723
  • 2
  • 13
  • 24

4 Answers4

15

This is happening because it's not a post request. CHtml::link has a way by which you can use post method, instead of get. This requires you to use submit attribute of clientChange, in your htmlOptions.

Somewhat like this:

echo CHtml::link(CHtml::encode('Delete image'), array('image/delete', 'id'=>$image->id),
  array(
    'submit'=>array('image/delete', 'id'=>$image->id),
    'class' => 'delete','confirm'=>'This will remove the image. Are you sure?'
  )
);
bool.dev
  • 17,508
  • 5
  • 69
  • 93
  • Thanks.. It worked. :) So, all defaults are 'Get' in Yii, right? Wheneer, I need to post a form, i explicitly need to define it, the way you showed it? – dibs_ab Aug 21 '12 at 08:23
  • by default, links are supposed to be 'Get' always, be it yii or simple html. but yes when we need to post through a link, this is the way in yii, it attaches a small javascript line to the output, which submits the form. but if you are talking about full fledged forms, take a look at cactiveform. – bool.dev Aug 21 '12 at 08:43
  • I'm not able to implement this, can someone please check my question here: http://stackoverflow.com/questions/13014436/error-400-invalid-request-when-creating-custom-delete-button-in-cgridview – WebDevPT Oct 22 '12 at 15:21
10

the default with Curd will be like this

/**
 * @return array action filters
 */
public function filters()
{
    return array(
        'accessControl', // perform access control for CRUD operations
        'postOnly + delete', // we only allow deletion via POST request
    );
}

The delete action can only accessed by POST; You can check it.

So you need to delete this line to work with you 'postOnly + delete',

Ahmad Samilo
  • 271
  • 4
  • 18
1

According to my experience this caused by some javascript files loaded after jquery. If you have jQuery link at the end of the document please remove it.

Genc Hosting
  • 187
  • 2
  • 7
  • The build-in JQuery was indeed my problem. I have disabled it by following this: http://hatadu.wordpress.com/2011/04/15/how-to-use-your-own-jquery-library-in-yii/ – NovaLogic Aug 01 '14 at 20:07
0

I think your actionDelete() contains the code that throws this, and I guess it depends on AJAX call or not. Check your relevant actionDelete()

Boaz Rymland
  • 1,459
  • 11
  • 29