JordanLev's Custom Contact Form - Validation

Permalink
Hi,

I'm using the custom contact form, which is working great, apart from validation. I can see the validation within the php (required etc) but I have included an automatic page change once the form has been submitted.

Here is the source code below:

<input type="submit" id="submit" value="Submit" class="btn btn-lg btn-primary" onclick="window.location.href='/thanks-for-requesting-a-callback/'" role="button" />



Is there a way I can validate the code before it gets sent as the contact form is being sent regardless at the moment.

Thanks

 
jordanlev replied on at Permalink Reply
jordanlev
Hey there,
The "onclick" code you have on that button is bypassing the actual form submission. (You're basically telling the browser "don't actually submit this form, but instead go to some other page").

What you'll want to do instead is allow the form submission to go through and THEN redirect the user to the other page. You'll want to do this in 2 places -- one is via javascript and one is via PHP (so that your form is accessible regardless of whether javascript is enabled or not).

For the javascript redirect, edit the "packages/custom_contact_form/blocks/custom_contact_form/view.php" file, and find the line that contains "} else { //validation error" (should be around line #43), and directly ABOVE that line, insert your redirect code:
window.location.href='/thanks-for-requesting-a-callback/';


For the php redirect (in case javascript is disabled), edit the "packages/custom_contact_form/blocks/custom_contact_form/controller.php" file, and scroll down to the bottom to find the "private function successRedirect() {" function. In that function, you'll see this line of code:
$redirect = Loader::helper('navigation')->getCollectionURL(Page::getCurrentPage());

Change that line to this instead:
$redirect = Loader::helper('navigation')->getCollectionURL(Page::getByPath('/thanks-for-requesting-a-callback'));


Best,
Jordan
drm92 replied on at Permalink Reply
Thanks for the reply Jordan,

but what happens when you have more than one form? How do you specify a certain thanks page to a particular contact form?


Thanks
jordanlev replied on at Permalink Reply
jordanlev
That's a bit more complicated, and would require altering the code of the addon in a more significant way. Ideally you'd want to add an editable field to the block itself, so the redirect location could be specified when adding/editing the block. Unfortunately there's no simple way to achieve that other than digging into the code.

If that's something beyond your area of expertise, and you have a budget for this project then my company could take on the task for you -- just email hello@webconcentrate.com and someone will be in touch with more questions and/or a quote for you.

Best,
Jordan