External form with own controller
I've mad an external form for my contact form. The styled HTML form is in the folder "external_form/forms/" and the controller with the same filename in the folder "external_form/forms/controllers". The correct function is called in my controller class when i push the send button and an email was send to me...so far, so good.
Now I've got treeproblems:
1.After that I would like to redirect the user to an other page with the command "$this->redirect('/tanks');" but that dosent work corectly. I have to put the URL with my '/tanks' together, but I can't
2. Why is the standard function "view()" or "on_start()" not working? How could I save the actually URL in a hidden input on the form when the form is showing?
3. In my URL is set the language e.x. "mysite.ch/en/company/contact" so i know the acctually language and this information has to be in the mail to. But when the form is filled out with errors and after that correctly I loose the language info in my URL, because it looks like "mysite.ch/?index.php?cID=530&bID=533&arHandle=Newsletter&ccm_token=1313742449:56fc94d929c59e8b24c9b012540d2f67&btask=passthru&method=contact_send"
Thanks for help
PS: Maybe it't completely the wrong direction i chose :S
So I don't have a solution to your exact problems here, but I am attaching a block that you could use as a starting point as a replacement for the external_form. (Note that this goes into your site's top-level "blocks" directory, not in "packages").
Hope it helps.
EDIT: I've since updated this code and the latest version is available here:https://github.com/jordanlev/c5_custom_contact_form...
it works fine with a block.
I've got one bug in it, when adding the block and fill out tanks you mesage and notify mail and click (insert/update), the two errors 'You must enter your name.' and 'You must enter your email address.' appears.
Is it possible that it calls the function "validate($data)"? how can I avoid that?
But if you just want to correct that error in your own file, change the name of the "validate" function in controller.php to something else, like "validate_form". Then also change the code that calls that function so it's using the new name as well.
Another bug was in the view.php file, where you need to change this:
<?php if ($showThanks); ?>
<?php if ($showThanks): ?>
(they're almost exactly the same, but that should be a colon at the end, not a semicolon).
One more question. I used two checkboxes within the form, created by the form helper. But the sate i can't read out in the controllers function action_submit_form()....any ideas?
$data->getInfo = $this->post('infos');
<?php echo $fh->checkbox('infos', $data->infos, true); ?>
$data->getInfos = empty($this->post('getInfos')) ? false : true;
I should probably update that code I have on github to include an example of these since it's such a tricky thing (took me a while to figure it out when I first started).
I'm scratching my head over your instructions for the Custom Form Block you created. I've copied and pasted the html from the old form and pasted it into view.php. What was already in view.php is this:
My question is do I have to go through old table based HTML form layout and convert things like:
<table width="480" border="0" cellpadding="2" cellspacing="0" class="rating"> <tr class="repeat"> <td colspan="4" align="left" valign="middle"><h2>Preferred Method of Contact</h2></td> </tr> <tr class="repeat"> <td align="left" valign="middle"><input id="alumni_preferred_contact_home_phone" name="alumni[preferred_contact]" type="radio" value="Home Phone" /> Home Phone</td> <td align="left" valign="middle"><input id="alumni_preferred_contact_work_phone" name="alumni[preferred_contact]" type="radio" value="Work Phone" /> Work Phone</td> <td align="left" valign="middle"><input id="alumni_preferred_contact_home_email" name="alumni[preferred_contact]" type="radio" value="Home E-mail" /> Home E-mail</td> <td align="left" valign="middle"><input id="alumni_preferred_contact_work_email" name="alumni[preferred_contact]" type="radio" value="Work E-mail" /> Work E-mail</td> </tr> </table>
If so, how do I handle a complex scale style with multiple columns and rows?
Just any guidance would be helpful. Thanks.
The only problem you'll run into is if the user submits the form and there's an error -- you will want some PHP code in your form to re-display the user's last inputted data (otherwise they'll have to fill out the entire form again if they miss just 1 required field for example). This is functionality that the C5 form helpers do for you automatically, which is why I use them -- because it saves me from having to write this code to "repopulate" form fields.
Note that you can also use the form helpers along with your table structure -- the form helper code just outputs the form elements -- but you can put those snippets of code inside your html <table> if you want. There are form helpers for all kinds of fields, including radio buttons, so this should be very doable given your situation.
See this documentation page for some basic info about the form helpers (unfortunately it's not very good, but hopefully gets you on the right track):
How do you set what is required with using straight HTML for this block?
You must enter your name.
You must provide a valid email address
Where is the "required" fields being set?
Thanks for great tips and custom block Jordan! Everything works just fine except translating the error messages.
I've tried to wrap messages in controller.php's public function validate_form() function into t() variable like this:
$val->addRequired('name', t('You must enter your name'));
Then i updated messages.mo file with PoEdit, but the translated error messages don't show up. Any ideas how to translate the form messages?
Thanks in advance!