Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Page 404 on form submission


Go to End


9 Posts   2733 Views

Avatar
ak1ra

Community Member, 5 Posts

10 March 2016 at 3:31am

Edited: 10/03/2016 3:31am

Hey, this is my first post on this forum so please indulge my noobyness. ;)

My issue:
I created a simple contact form based on Uncle Cheese's tutorial (http://www.silverstripe.org/learn/lessons/introduction-to-frontend-forms#tabpane-tutorial) with the only difference being, that i want to send an email upon submission and not save a comment. The form in general looks fine, but on submission I get a "Page not found".

Some more details:
- I'm using SilverStripe 3.2.1
- The contact-form is accessible (on /contact) and shows all fields as expected.
- Upon submission the form redirects to /contact/ContactForm –as I would expect– but the 404 page is shown
- nested_urls in the config.yml is set to true
- further code is attached below
- I tried to rewrite everything once, to make sure i did not screw up on any typos, but with the same result

My code:

Contact.php

class Contact extends Page {
  static $description = 'Default contact form';
  static $db = array('SubmitText' => 'HTMLText');

  public function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab('Root.Main', new HtmlEditorField('SubmitText', 'Thank you text'));
    return $fields;
  }
}

class Contact_Controller extends Page_Controller {
  private static $allowed_actions = array(
    'ContactForm'
  );

  public function init() {
    parent::init();
  }

  public function ContactForm() {
    $fields = FieldList::create(
      TextField::create('Name', 'Your name'),
      EmailField::create('Email', 'Your eMail-address'),
      DropdownField::create('Subject', 'Subject', array(
        '' => 'Select one option',
        'Question about an order',
        'Foo',
        'Bar',
        'Just say "Hi"'
      )),
      TextareaField::create('Message', 'Your message')
    );

    $actions = FieldList::create(
      FormAction::create('submitContact', 'Submit')
        ->setUseButtonTag(TRUE)
        ->addExtraClass('button')
    );

    $validator = RequiredFields::create('Name', 'Email', 'Message');

    return Form::create($this, __FUNCTION__, $fields, $actions, $validator);
  }

  public function submitContact($data, $form) {
    $config = SiteConfig::current_site_config();
    $email = new Email();

    $email->setTo($config->ContactMail);
    $email->setFrom($data['Email']);
    $email->setSubject("xxxxxxxx :: Contact from {$data["Name"]}");

    $messageBody = sprintf(
      "<p><strong>Name:</strong> %s</p><p><strong>Message:</strong><br>%s</p>",
      $data['Name'],
      $data['Message']
    );
    $email->setBody($messageBody);
    $email->send();

    return array(
      'Content' => $this->SubmitText,
      'ContactForm' => ''
    );
  }
}

routes.yml (not sure it this affects the issue)

---
Name: myroutes
After: framework/routes#coreroutes
---
Director:
  rules:
    'shorturl': 'Page_Controller'
    'item//$Action/$ID': 'Item_Controller'
    'editorial//$Action/$ID': 'Editorial_Controller'

I hope that covers all the needed infos, and i am really thankful for any hints or suggestions that can get me on the right track.

Avatar
fraxier95

Community Member, 8 Posts

10 March 2016 at 10:44pm

Edited: 10/03/2016 10:44pm

I could be wrong but I can't see a redirect after you process the form, in the SubmitContact function you return an array not sure what for but without telling SS where to go may produce the 404 error.

Try adding
return $this->redirect('/some/success/url');

at the end of that function and it should be ok given the page it redirects to exists.

Avatar
ak1ra

Community Member, 5 Posts

10 March 2016 at 11:15pm

Edited: 10/03/2016 11:16pm

Hi fraxier95,

thanks for your support. I followed up on your advice and adjusted the return to use the redirect, but i still got the 404 page (did a rebuild and flush, just to be sure).
However it looks like the submitContact-function is not even called – what i don't quiet understand…

Avatar
fraxier95

Community Member, 8 Posts

10 March 2016 at 11:29pm

What is the url of the page with 404 error now?

Avatar
ak1ra

Community Member, 5 Posts

10 March 2016 at 11:47pm

same as before, the form still redirects to /contact/ContactForm.
also i added a console log to see if the function is called at all, but there was it was not (no logged message).

So I would guess the main issue is that the handler-function submitContact() is not called.

Avatar
fraxier95

Community Member, 8 Posts

11 March 2016 at 12:10am

Edited: 11/03/2016 12:12am

Ok pretty weird, is your environment set to display errors? iirc putting it in development mode does that. It might be why it shows a 404 page instead of an error stack trace.

Otherwise can you inspect the button and find out where it goes? You should see the function name 'submitContact' used somewhere.

Avatar
ak1ra

Community Member, 5 Posts

12 March 2016 at 12:28am

yep, pretty wired.
The page is set to dev-mode and the button has action_submitContact for the name-attribute. So that should work out, i guess.

I can't shake the feeling that there is some general setting i screwed up, but i have no idea what to look for.

Avatar
martimiz

Forum Moderator, 1391 Posts

12 March 2016 at 5:51am

In situations like this, what I usually do is start with the most basic of forms on a fresh SilverStripe install, test if that works, and build it up from there in steps. Sometimes that just works a lot faster. Could be simple but weird things, like 'item' not being alowed as an url segment or whatever :)

Go to Top