Error 400 Invalid request YII for deleting a record

dibs_ab picture dibs_ab · Aug 21, 2012 · Viewed 15.5k times · Source

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.

Answer

bool.dev picture bool.dev · Aug 21, 2012

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?'
  )
);