0
votes

About two months ago, I upgraded a SilverStripe website from 2.3.5 to 2.4.6. Since the upgrade, some of the forms on my site won't send all of the data to the submit function (the POST entry is empty), including "system" forms like the forgot password form for the admin section (and I think even the password field for the admin login form). What I find odd is that I have a nearly identical (but different code) form with the same field, which works just fine, but even copying the field over doesn't work.

Here's the working form and its submit code:

function ContactUsForm() {
        // Create fields
        $fields = new FieldSet(
                        new HeaderField('Send Us a Message:', '2'),
                        new TextField('NAME', 'Full Name:'),
                        new EmailField('EMAIL', 'E-mail Address:'),
                        new TextField('PHONE', 'Phone:'),
                        new TextField('COMPANYNAME', 'Company Name:'),
                        new TextareaField('MESSAGE', 'Message/Note:'),
                        new TextField('HUMAN', '1+1 =')
        );

        // Create actions
        $actions = new FieldSet(
                        new FormAction('doContactUs', 'Submit')
        );

        return new Form($this, 'ContactUsForm', $fields, $actions);
    }

    function doContactUs($data, $form) {

        $human = $data['HUMAN'];
        if ($human == '2') {
            $from = '[email protected]';
            $to = '[email protected]';
            $subject = 'General Contact  Submission';
            $body = $data['NAME'] . ' has submitted a General Inquiry, their info: ' . $data['NAME'] . ' | ' . $data['EMAIL'] . ' | ' . $data['PHONE'] . ' | ' . $data['COMPANYNAME'] . ' and they have included the following note (if they included a note): ' . $data['MESSAGE'];
            $email = new Email($from, $to, $subject, $body);
            $email->send();


            Director::redirect('thankyou/');
        } else {
            $form->addErrorMessage('Message', 'Incorrect answer to the human check.','error');
            return Director::redirectBack();
        }
    }

Here's the form that doesn't work, and its submit code:

function GenericContactForm() {

    global $contactmessage;

    // Create fields
    $fields = new FieldSet(
            new TextField('NAME', 'Full Name:'),
            new EmailField('EMAIL', 'Email:'),
            new TextField('TELEPHONE', 'Work Phone:'),
            new ListboxField(
                $name = "TYPEOFCONTACT",
                $title = "Office Type:",
                $source = array(
                "Single Person Office" => "Single Person Office",
                "Team Room" => "Team Room",
                "Open Plan" => "Open Plan"
                ),
                $value = 1
            ),
                    new TextField('HUMAN', '1+1 =')
    );

    // Create actions
    $actions = new FieldSet(
            new FormAction('doGenericContact', 'Submit')
    );

    return new Form($this, 'GenericContactForm', $fields, $actions);
}

function doGenericContact($data, $form) {
    $human = $data['HUMAN'];
    if ($human == '2') {
        $from = $data['EMAIL'];
        $to = '[email protected]';
        $subject = 'Contact Request';
        $body = 'The following individual has requested to be contacted: ' . $data['NAME'] . ' | ' . $data['EMAIL'] . ' | ' . $data['TELEPHONE'] . ' and they have made contact to inquire about the following: ' . $data['TYPEOFCONTACT'];
        $email = new Email($from, $to, $subject, $body);
        $email->send();

        Director::redirect('/thankyou/');
    } else {
        $form->addErrorMessage('Message', "Incorrect answer to the human check. (Your answer: $human)", 'error');
        return Director::redirectBack();
    }
}

I've tried removing the ListboxField, since that's about the only real difference I've found. I also tried testing it locally by removing everything inside the "true" portion of the if statement in the submit function, and it works locally, but not on the server, which leads me to believe it may be a conflict between something in SilverStripe and the server setup (I have limited access to the server). Additionally, these forms worked prior to the upgrade.

Any ideas on what is causing this, and what I can do to fix it?

Edit - Further troubleshooting has found that there is no data in the post array on the non-working forms.

1
You state that "some of the forms on my site won't send all of the data". Please clarify what the actual problem is. Are the forms not sending the email or are they sending the email but with missing field data? - ryanwachtl
@ryanwachtl - It's not even making it to the email code because it's not making it past the human check. The "human" POST variable is empty. - Shauna
I don't see anything in your code that would prevent the form from working. Try using Debug::show($data) inside doGenericContact() to get a look at what is being submitted from the form. Once you verify that $data['HUMAN'] has a value, make sure your email server will send emails as coming from a domain other than your own. I have a feeling the issue is related to the servers email setup and not anything SilverStripe related. - ryanwachtl
@ryanwachtl - There is no value in $data['HUMAN'], that's what I've been saying. Additionally, this code worked prior to the upgrade to 2.4.6 on this server, emailing and all. Also, there are forms that DO work just fine running on the same site and server. - Shauna

1 Answers

0
votes

It turned out to be an issue with the way SilverStripe 2.4 does modules vs how 2.3 does modules. I wasn't the one who solved it, so I don't have the details on the issue, though, but bringing the modules in line with what 2.4 expects has fixed it.