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.

Form Questions /

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

[Solved] Custom forms 2.4.2


Go to End


4 Posts   1455 Views

Avatar
Mo

Community Member, 541 Posts

29 October 2010 at 1:02am

Hi All,

Has anyone had problems creating custom forms in 2.4.2? I have a very simple form that is created in a method in my page controller. For some reason thought it doesn't seem to be calling the method specified in the action. It just seems to re-direct to the form's url with the forms method name tacked on the end?

Any ideas?

Mo

Avatar
Martijn

Community Member, 271 Posts

29 October 2010 at 4:00pm

Have you added the action method to the allowed actions array?

Avatar
Mo

Community Member, 541 Posts

29 October 2010 at 10:18pm

Yep.

I have actually tried downgrading to 2.4.1 and am getting the same problem. I don't think there is anything wrong with my code :s.

Here is my Page_Controller:

class Page_Controller extends ContentController {
    public static $allowed_actions = array(
        'guessForm'
    );

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

    public function guessForm() {
            $fields = new FieldSet(
                new TextField('Content','Any idea yet? Have a guess'),
                new HiddenField('OwnerID', 'ID', $this->getPlayer()->ID)
            );

            $actions = new FieldSet(new FormAction("doSubmitGuess","Submit"));

            $validator = new RequiredFields();
            $validator->set_javascript_validation_handler('none');

            return new Form($this,"guessForm",$fields,$actions,$validator);
    }

    public function doSubmitGuess($data,$form) {
        $guess = new Guess();
        $form->saveInto($guess);
        $guess->write();

        Director::redirectBack();
        return;
    }
}

I had already got the allowed actions entry. I have also written a unit test to sets saving of $guess and its associations, which pass. It just seems the form does not process the submit action :(.

I have also tried altering "Director::redirectBack();" with no luck.

Any ideas? This has me stumped. I don't think I am making some NOOB mistake, but I might be...

Mo

Avatar
Mo

Community Member, 541 Posts

31 October 2010 at 2:45am

Ok, this was just being a Noob!!

I had a method in the controller that was re-directing if the incorrect action was supplied. Obviously this re-direct happens before the POST data is processed.

All fixed now.

Mo