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.

Customising the CMS /

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

[Solved] Contact Form action not activating


Go to End


2 Posts   1229 Views

Avatar
LewisMcMahon

Community Member, 2 Posts

18 June 2015 at 8:29pm

I have built a contact form but i cant get it when submitted to direct to the submit action it just directs to "/contact/?Message=dsdfdas&Name=lewis&Email=
&Company=&Phone=&SecurityID=ecff9d8ebe33324ad9f827dac885246778747413&action_submitContactForm=Submit" when it should be directing to /contact/submitContactForm/

Any help would be greatly appreciated

Bellow is the contact page controller .

class ContactPage_Controller extends Page_Controller
{
    private static $allowed_actions = array('Form','submitContactForm');

    public function ContactForm() {
        $fields = new FieldList(
            TextareaField::create('Message', '')
                ->addExtraClass('span8 search-query right')
                ->setTemplate('BootstrapTextareaField')
                ->setAttribute('placeholder', 'MESSAGE*')
                ->setRows(8),
            TextField::create('Name', '')
                ->addExtraClass('span4 search-query')
                ->setTemplate('BootstrapTextField')
                ->setAttribute('placeholder', 'NAME*'),
            TextField::create('Email', '')
                ->addExtraClass('span4 search-query')
                ->setTemplate('BootstrapTextField')
                ->setAttribute('placeholder', 'EMAIL*'),
            TextField::create('Company', '')
                ->addExtraClass('span4 search-query')
                ->setTemplate('BootstrapTextField')
                ->setAttribute('placeholder', 'COMPANY*'),
            TextField::create('Phone', '')
                ->addExtraClass('span4 search-query')
                ->setTemplate('BootstrapTextField')
                ->setAttribute('placeholder', 'PHONE*')
        );
        $actions = new FieldList(
            FormAction::create('submitContactForm', 'Submit')
                ->setUseButtonTag(true)
                ->addExtraClass('btn red-btn pull-right')
        );
        $required = RequiredFields::create('Name', 'Email', 'Message');

        $form = new Form($this, 'ContactForm', $fields, $actions, $required);
        $form = $form->setTemplate('BootstrapFormTemplate')
            ->addExtraClass('contact-form');
//            ->enableSpamProtection(array(
//                'protector' => 'RecaptchaProtector',
//                'name' => 'Captcha'
//            ));

        return $form;
    }

    public function submitContactForm($data,$form)
    {

        // Save the form data in the Session so that the user can put in invalid
        Session::set("FormData.{$form->getName()}.data", $data);

        $errors = $this->validateForm($data);

        if ($errors !== false) {
            // Load the errors so that they can be displayed on the Form in the view
            foreach ($errors as $error) {
                if (!empty($error['field'])) {
                    // errorMessage is distinct form sessionMessage in that it displays directly next/under a field
                    $form->addErrorMessage($error['field'], $error['error'], 'validation error');
                } else {
                    // sessionMessage displays at top of form
                    $form->sessionMessage($error['error'], 'alert alert-danger');
                }
            }

            return $this->redirectBack();
        }

        $email = new Email($data);
        $config = SiteConfig::current_site_config();
        $email->setTo($config->Email);
        $email->setFrom($data['Email']);
        $email->setSubject("Contact Message from {$data["Name"]}");

        $messageBody = "
            <p><strong>Name:</strong> {$data['Name']}</p>
            <p><strong>Phone Number:</strong> {$data['Phone']}</p>
            <p><strong>Company:</strong> {$data['Company']}</p>
            <p><strong>Message:</strong> {$data['Message']}</p>
        ";
        $email->setBody($messageBody);

        if($email->send()) {
            // Clear the stored Session data if it successful
            Session::clear("FormData.{$form->getName()}.data");
            $form->sessionMessage('Thank you for your enquiry. We will try to get back to you as soon as possible.', 'alert alert-info');
        } else {
            $form->sessionMessage('There was a problem with submitting your enquiry. Please try again.', 'alert alert-danger');
        }
        return $this->redirectBack();

    }
...}

The form is called in with $ContactForm and the html it generates is

<form class="form-search">
            <div class="row">
                
    


    <fieldset>
        
        
            <div id="Message" class="field textarea span8 search-query right nolabel">
	
	<div class="middleColumn">
		<textarea name="Message" id="Form_ContactForm_Message" rows="8" class="form-control textarea span8 search-query right nolabel" placeholder="MESSAGE*" required=""></textarea>
	</div>
	
	
	
</div>

        
            <div id="Name" class="field text span4 search-query nolabel">
	
	<div class="middleColumn">
		<input type="text" name="Name" id="Form_ContactForm_Name" class="form-control text span4 search-query nolabel" placeholder="NAME*" required="">

	</div>
	
	
	
</div>

        
            <div id="Email" class="field text span4 search-query nolabel">
	
	<div class="middleColumn">
		<input type="text" name="Email" id="Form_ContactForm_Email" class="form-control text span4 search-query nolabel" placeholder="EMAIL*" required="">

	</div>
	
	
	
</div>

        
            <div id="Company" class="field text span4 search-query nolabel">
	
	<div class="middleColumn">
		<input type="text" name="Company" id="Form_ContactForm_Company" class="form-control text span4 search-query nolabel" placeholder="COMPANY*">

	</div>
	
	
	
</div>

        
            <div id="Phone" class="field text span4 search-query nolabel">
	
	<div class="middleColumn">
		<input type="text" name="Phone" id="Form_ContactForm_Phone" class="form-control text span4 search-query nolabel" placeholder="PHONE*">

	</div>
	
	
	
</div>

        
            <input type="hidden" name="SecurityID" value="ecff9d8ebe33324ad9f827dac885246778747413" class="hidden" id="Form_ContactForm_SecurityID">

        
        <div class="clear"><!-- --></div>
    </fieldset>


        <p id="Form_ContactForm_error" class="message " style="display: none"></p>



        <div class="Actions">
            
                
	<button type="submit" name="action_submitContactForm" value="Submit" class="action btn red-btn pull-right" id="Form_ContactForm_action_submitContactForm">
		Submit
	</button>


            
        </div>


    


                <div class="alert alert-success hidden" id="contactSuccess">
                    <strong>Success!</strong> Your message has been sent to us.
                </div>

                <div class="alert alert-danger hidden" id="contactError">
                    <strong>Error!</strong> There was an error sending your message.
                </div>
            </div></form>

Avatar
LewisMcMahon

Community Member, 2 Posts

18 June 2015 at 8:54pm

I solved it turns out if you have <form> tags with no action around your form silver stripe wont make its own and will just use the ones its a child of.