C5.7.5.8 - form submit errors from home page
Permalink
Hello,
I have a weird form behavior: when I have on any but home pages, it works fine, but when I put it on the home page, I have weird errors on submission which don't make sense. I'll explain with an example of the 'Name' field error checking.
Here's the submit function bit:
Here's the validate_form part:
So, the weird problem is that the IF checking seems to be totally ignored, the function returns false BUT the email gets sent AND the errors from the form_errors array are shown! How can that be? How can the function return false AND still send the email?
And as I said, it only happens on the home page. On any other page all works fine.
I have a weird form behavior: when I have on any but home pages, it works fine, but when I put it on the home page, I have weird errors on submission which don't make sense. I'll explain with an example of the 'Name' field error checking.
Here's the submit function bit:
public function action_submit_form() { if ($this->isPost() && $this->validate_form()) { ... @$mh->sendMail(); ... $this->set('success', $this->success); } else { $this->set('form_errors', $this->form_errors); } $_POST = array(); $this->view(); }
Here's the validate_form part:
public function validate_form() { $this->name = $txt->sanitize($_POST['name']); ... if ((mb_strlen($this->name, 'UTF-8') < 2) || (mb_strlen($this->name, 'UTF-8') > 60)) { array_push($this->form_errors, $this->error_name); } ... if (!$this->form_errors) { return true; } else { array_push($this->form_errors, sprintf($this->error_submit, $this->email_to)); return false; } }
So, the weird problem is that the IF checking seems to be totally ignored, the function returns false BUT the email gets sent AND the errors from the form_errors array are shown! How can that be? How can the function return false AND still send the email?
And as I said, it only happens on the home page. On any other page all works fine.
I can't help you, except to confirm that you're not going crazy. I developed a form-based block recently, and observed different behaviour when the block was placed on a home page. I traced it down to some code in the c5 core, which included some comment about checking something about a redirect (IIRC). Alas, I couldn't understand what it was doing, or why. But for sure my form's submit URL was being called differently when the form was on a home page.
So does it mean forms don't work on a home page?
In general, forms can be used on the home page. c5's built-in Form block seems to manage it. I just couldn't quite work out how!
IIRC, I got around the problem I was having by avoiding passing around bID in submit URLs. c5's URL redirection code seemed to interfere with it when dealing with the home page, but I couldn't understand the details.
My alternative approach is here:
https://www.concrete5.org/marketplace/addons/contact-form-no-links1...
IIRC, I got around the problem I was having by avoiding passing around bID in submit URLs. c5's URL redirection code seemed to interfere with it when dealing with the home page, but I couldn't understand the details.
My alternative approach is here:
https://www.concrete5.org/marketplace/addons/contact-form-no-links1...
I am having the same problem (in 5.6)... I thought I was going crazy as I have checked my code multiple times...
I have made a Pagetype Controller (home.php) and created a page type (home). I know that it is accessing the controller becuse if I remove it or alter the code it throws an error, but the controller isn't doing what it should and the same code works on any other page in the site where I implement the same process.
It must be something to do with the uniqueness of the homepage...
I have made a Pagetype Controller (home.php) and created a page type (home). I know that it is accessing the controller becuse if I remove it or alter the code it throws an error, but the controller isn't doing what it should and the same code works on any other page in the site where I implement the same process.
It must be something to do with the uniqueness of the homepage...
Good to know it's not me going nuts )))
But doesn't seem right one can't use forms on home page. Maybe there's a better fix than making home controllers?
I'll try the redirect addon and make another "home" page to redirect to. I'll see if the form's gonna work on that.
But doesn't seem right one can't use forms on home page. Maybe there's a better fix than making home controllers?
I'll try the redirect addon and make another "home" page to redirect to. I'll see if the form's gonna work on that.
I don't think that my problem is the form as I can also use a form block on the homepage.
My form is hardcoded and uses a controller to operate it. I have setup a Pagetype controller and the homepage has its own pagetype. I think the problem lies within the controller on the homepage. Whether there is another controller secretly attached to the homepage which is interfering with my controller I don't know...
I don't understand why my pagetype controller will work on any other page with the same pagetype, but not on the homepage...
My form is hardcoded and uses a controller to operate it. I have setup a Pagetype controller and the homepage has its own pagetype. I think the problem lies within the controller on the homepage. Whether there is another controller secretly attached to the homepage which is interfering with my controller I don't know...
I don't understand why my pagetype controller will work on any other page with the same pagetype, but not on the homepage...
I'm a bit (okay, a lot) out of my depth, but I'd be investigating your form submit method and action URL, and comparing with the actual URL that you're picking up in the controller. You might find that the latter URL is missing some parameters when being received on a home page.
This may not apply to you, depending on how you've structured things.
This may not apply to you, depending on how you've structured things.