Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Options

Issue with custom Form submission

I defined the following form in a view php file for a plug-in I am preparing:

<form action="<?php echo Url('/profile/mems');?>" method="GET">

When the form submits, it does not gets to the method I defined as following in my code:

public function ProfileController_Mems_Create($Sender) { }

instead it goes to vanilla/index.php?memsoption=xbus1&Submit+Button=Submit

But if I type in the browser: http://mydomain/vanilla/index.php?p=/profile/mems, it goes to my method above.

Sure I am doing something wrong, but I could not find out what exactly is wrong by looking at documentation and the community.
Does anyone have any experience of defining custom forms that can submit data to a controller in code?
Appreciate if you can share your experience on this or if you have any reference to documentation/discussion related to this.

Comments

  • Options
    hgtonighthgtonight ∞ · New Moderator

    Don't bother writing out manual form markup. Use the Gdn_Form object and just open it in your view:

    echo $this->Form->Open();
    echo $this->Form->Errors();
    
    /* Spit out your inputs */
    
    echo $this->Form->Close();
    

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • Options
    R_JR_J Ex-Fanboy Munich Admin

    You shouldn't do it like that. Your view should use the form class of Vanilla look here for an implementation example:
    https://github.com/vanilla/addons/blob/master/plugins/AuthorSelector/views/changeauthor.php
    https://github.com/vanilla/addons/blob/master/plugins/AuthorSelector/class.authorselector.plugin.php#L42-82

    The view only implements a submit button and the "action" is defined in the controller.

    You have to create a controller for the form:

    public  function PluginController_YourPlugin_Create ($Sender) {
        // add a form to your controller
        $Sender->Form = new Gdn_Form();
    
        if (!$Sender->Form->AuthenticatedPostBack()) {
            // form hasn't yet submitted. Stuff values into formfields for example or pass info to your form
           $Sender->SetData('SomePlaceholder', 'Data you'll need in your view');
        } else {
            // form has been submitted and you can now work with the submitted values
            // here they are!
            $FormPostValues = $Sender->Form->FormValues();
            // you can validate them, alter them or whatever yoou like and then do something like that
            $this->Mems($FormPostValues);
    }
    
Sign In or Register to comment.