CakePHP - Using custom form tags without losing form auto fill magic

The CakePHP FormHelper is one of the biggest time savers in the whole framework. It not only takes care of generating common elements as well as performs auto-filling of pretty much all input fields. The only problem I find with it is the difficulty in generating forms using $form->create(...) that submit to the current page, ie a form with action="". This is because when the URL in $form->create(...) is set as '' or null, the default action URL of /model/add takes over.

The solution I end up with is often writing my own <form> and </form> tags instead of using $form->create() and $form->end(). The issue with using custom form tags is that CakePHP will no longer auto fill input fields meaning that a lot of code has to be implemented to detect and specify input values. Overcoming this is quite simple and simply requires the use of $form->end() with a custom form tag, such that your form code looks like:

<form action="" method="POST">
<?= $form->input("") ?>
<?= $form->input("") ?>
<?= $form->end() ?>


variaas said...

Check this out:

echo $form->create('Post',array('type'=>'get', 'controller'=>'posts', 'action'=>'search'));

Matt said...

It's incredibly bad practice to submit a form to '' in any case, you should always specify your action, even if it IS to the same url that the form is located at ;o)

Post a Comment