Take the 2-minute tour ×

I have an default layout (default.ctp) with an header. In this header is an searchform wich is displayed on each page with the following code:

echo $this->Form->create('null', array('url' => '/search/result'),array(
                        'inputDefaults' => array(
                            'label' => false,
                            'div' => false

                    echo $this->Form->input('search_query', array('label' => false, 'div' => false, 'type' => 'tel','class' => 'input-small', 'id' => 'appendedInputButton', 'placeholder' => 'Input search'));

                    echo $this->Form->button('Search', array('class' => 'btn'));
                    echo $this->Form->end()

In this default.ctp file I call twitterbootstrap input modal overlay with the following line. This element is used on every page.

<!-- Add order modal -->
        <?php echo $this->element('add_customer_order');?>

In this element I do an request action to my bestelling/modal_ext so that the form validation is used and the formfields are setup in the right way so that saving is possible with the bestelling controller. The following code is in the element:

$this->requestAction('Bestelling/modal_ext');//Include BestellingController modal_ext               
<!-- Add order modal -->
        <div id="Bestellingtoevoegen" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-header">
            <h3 id="myModalLabel">Add customer order</h3>
            <div class="modal-body">

                    <div class="row-fluid">
                        <div class="span8"> 
                            echo $this->Form->create('Bestelling');
                            //echo $this->Form->create('null', array('url' => '/bestelling/modal_ext'));
                            echo $this->Form->input('bestelnummer', array('type' => 'tel','class' => 'input-small'));
                            echo $this->Form->input('klantnummer', array('type' => 'tel','class' => 'input-small')); ?>
                        <div class="span4">
                            <?php echo $this->Form->checkbox('serial'); echo' <span class="label label-info">Serialcode</span> '; ?>
                            <?php echo $this->Form->checkbox('insured'); echo' <span class="label label-info">Insured</span>'; ?>
                            <?php echo $this->Form->checkbox('giftpaper'); echo' <span class="label label-info">Giftpaper</span>'; ?>

                    <div class="row-fluid">
                        <div class="span5"> 

                            echo $this->Form->input('titel', array('type' => 'tel', 'id' => 'discription', 'onkeyup' => 'lookupproductnamenew(this.value);', 'onblur' => 'fill();', 'after' => '<div class="suggestionsBox" id="suggestions" style="display: none;">
                                    <img src="images/upArrow.png" style="position: relative; top: -20px; left: 30px;" alt="upArrow" />
                                    <div class="suggestionList" id="autoSuggestionsList">

                        <div class="span7">
                            echo $this->Form->input('aantal', array(
                                'options' => array(
                                '1' => '1',
                                '2' => '2',
                                '3' => '3',
                                '4' => '4',
                                '5' => '5',
                                '6' => '6',
                                '7' => '7',
                                '8' => '8',
                                '9' => '9',
                                '10' => '10',
                                '11' => '11',
                                '12' => '12'),

                                'div' => 'input-prepend',
                                'label' => '&nbsp;',
                                'between' => '<span class="add-on">X</span>',
                                'class' => 'span8',
                                'id' => 'prependedInput',


                   <div class="row-fluid">  
                        <div class="span12">
                        echo $this->Form->input('opmerking', array('type' => 'textarea', 'class' => 'span10','rows' => '4', 'cols' => '90'));
                        echo $this->Form->hidden('levertijd', array('type' => 'tel', 'id' => 'levertijd', 'onblur' => 'fill();'));
                        echo $this->Form->hidden('prijs', array('type' => 'tel', 'id' => 'prijs', 'onblur' => 'fill();'));
                        echo $this->Form->hidden('artikelnummer', array('type' => 'tel', 'id' => 'artikelnummer', 'onblur' => 'fill();'));
                        echo $this->Form->hidden('ean', array('type' => 'tel', 'id' => 'ean', 'onblur' => 'fill();'));
                        echo $this->Form->hidden('overtime', array('type' => 'tel', 'id' => 'overtime', 'onblur' => 'fill();'));
                        echo $this->Form->hidden('productgroep', array('type' => 'tel', 'id' => 'productgroep', 'onblur' => 'fill();'));

            <div class="modal-footer">
            echo $this->Form->button('Save', array('class' => 'btn btn-primary'));echo'<br/>';
            echo $this->Form->end();

<!-- End Add order modal -->

When is submit the form in my header the form sends me to /search/result where I can see that data is submitted, but I also see an error that my bestellingcontroller is called. So when I submit the form in the header my other form is also submitted.

If I delete the request action in the element and replace the Form => create with:


It works fine, but then I lose the validation in this form and the $request->data changes because the value isn't an array like [Bestelling][fieldname]. The change of the structure of the array isn't a big problem, but I want to try to do it the right way. Can somebody help me in the right direction to solve this problem. I'm very new to cakephp, but I think that the way of calling the actions isn't done in de right way by my program.

share|improve this question
Dennis, please make sure all your code is in English. It makes it easier to understand for non-dutch people what you code is all about. It's a good habit to program in English, always. Because codesharing will become less of a pain. And welcome to SO. Make sure you code the name of the model in your FormHelper::create method. Since then you can exclude any errors made by Cake itself where it will assume you are calling it from the current controller. Read the FormHelper::create() docs if you will :) book.cakephp.org/2.0/en/core-libraries/helpers/form.html –  Jelmer May 12 '13 at 22:13

2 Answers 2

Both of your forms are submitting because you're esentially giving them the same "name". They do have different actions set but Cake's FormHelper:

echo $this->Form->create('null', ...

in this thingy you've written, the string 'null' is not the NULL language value.

FormHelper's book explanation says:

The form element is also returned with a DOM ID. The ID is generated using the name of the model, and the name of the controller action, CamelCased. So in your case I suppose that this is something like 'NullAddForm' (if it is the add action of the controller).

In your case you can pass FALSE for the model variable like this:

echo $this->Form->create(false, $otherStuff);

which will:

You can also pass false for $model. This will place your form data into the array: $this->request->data (instead of in the sub-array: $this->request->data['Model']). This can be handy for short forms that may not represent anything in your database.

So if you want both forms to POST to your controller directly use this cheme with the false value as the Model variable for the FormHelper::create() function's first parameter and be careful since this will change the structure of $this->request->data in the Controller.

Your other option, at least for the search, is to do it via AJAX. If you ask me this is better for such functionality as you will not be requesting a full re-render of the page. Even better if you do both forms via AJAX.

@Jelmer is very right about the variables in a different language than english since this will be hell for any other non-native (Dutch for example) speaker. I've personally dealth with a lot of such projects (yes exactly in Dutch) and it was not pretty.

share|improve this answer
@Jelmer I understand the point of the language, the problem is that I'm making the program on an existing database, but renaming the field for a question posted here makes it better to read. –  Dennis van der Graaf May 13 '13 at 21:25

Al the dutch naming has been replaced with english for better reading

I found the problem and it was actually a very smal error wich makes this error (I think). The error was caused by the naming of the database table order. Cakephp assumed that the databasetable is called orders with the extra S but this wasn't the case so I was setting the database with


I've changed the name to orders now and removed the following line in add_customer_order

$this->requestAction('Order/modal_ext');//Include OrderController modal_ext

I still use the following line in default.ctp


but this isn't giving any problems now. This controller searches in the table orders wich is set by setSource in the controller. If i change null to Search it gives an error because the database table searches doesn't exist. This form should point the controller order if I would do it the right way I think so that null can be replaced. Both forms are submitting separately now as expected. So I think the whole problem was that I wasn't following the naming convention from cakephp. Probable I will run into this namingconvention again and following it will be more easy (changing names of tables).

share|improve this answer

Your Answer


