Arvind K.
Arvind K.

Reputation: 1294

Cakephp - Having Issue when submitting form to plugin controller action

I am a 3+ years old in cakephp and facing a somewhat strange issue with submitting a form to plugin controller's action (i am using plugin first time). After trying different known things i am posting this one.

Going straight into the matter here is the form in my "forum" plugin's search_controller.php's "index" view:

echo $form->create("Search", array('url'=>array('controller' =>
'search', 'action' => 'index','plugin'=>'forum'),
'id'=>'searchFormMain'));
<input type="text" name="data[Search][keyword]" style="width:357px; margin-left:9px;"><p><span id="searchButton"><input
type="image" src="/img/button_search.jpg" style="height:40px;width:
136px;border:0;" class="handcursor"></span></p>
</form>

As i am submitting this form to "index" action of search controller of forum plugin, the following code does print nothing:

public function index($type='') {
    if(!empty($this->data)) {
        pr($this->data);
        die;
    }
}

While if i try the same code within beforeFilter of the same controller i.e. search_controller.php it works well and prints as follows:

Array
(
    [Search] => Array
        (
            [keyword] => Hello Forum
        )

)

And finally here is the beforeFilter code (of search_controller.php):

public function beforeFilter() {
    parent::beforeFilter();
    if(!empty($this->data)) {
        pr($this->data);
    }
}

Fyi, it does not matter if i comment out "parent::beforeFilter();" or even disable $uses of my controller (if they look doubtful to you) the result is same i.e. the control is not going in to "index" action in the case of form submit while is working fine in the case of page call. The url/action to page is http://localhost.rfdf.org/forum/search/index. If i call the url directly it loads the form fine but when i submit it, it never gets into the "index" action of the controller thus no view rendered.

If i try the same set of code out of "forum" plugin environment i.e. in normal application it works just fine

I have been trying to find a way out of this for last 3+ hours now but no success. I would appreciate any help in solving this puzzle.

Upvotes: 1

Views: 1861

Answers (3)

Arvind K.
Arvind K.

Reputation: 1294

I got it, finally!

It was Securty compontent dropping the request into the blackHole whenever it failed to find a security token with the form data. I learned that "Security" component "interferes" with $form->create() method and places a token as a hidden field with each $form->create() call. On the form submit, just after beforeFilter and right before getting into the controller "action" it checks for this token and simply dies everything on a validation failure. Unfortunately there is no error message or entry to cake log.

In my case i had been creating my own custom tag and not with the help of $form->create method so no token was being generated which lead to all the pain.

I resolved it by placing

$this->Security->validatePost = false; 

at the end of beforeFilter.

Thanks everyone!

Upvotes: 1

JohnP
JohnP

Reputation: 50019

Are you using ACL or any of the like? In the beforeFilter, do a pr of the request. See which action is being requested to make sure that the request is correct

Upvotes: 0

David Yell
David Yell

Reputation: 11855

Have you tried putting an else into that if(!empty($this->data)) and doing a pr() as it could be that your post is not empty.

Either that or the format of your url array is not correct.

From ln759, http://api.cakephp.org/view_source/router/#line-757

$defaults = $params = array('plugin' => null, 'controller' => null, 'action' => 'index');

So I guess you need plugin first?

Upvotes: 0

Related Questions