arambalakjian / DataObject-as-Page

A SilverStripe module for displaying DataObjects as Pages
53 stars 27 forks source link

[question] possible to access DOAP values from form on show page? #37

Closed mierla closed 11 years ago

mierla commented 11 years ago

I apologize if this the wrong place for this question. Is it possible to populate a front-facing form on a DOAP listing page with the values being shown as the DOAP? How do you pass them to the form template?

arambalakjian commented 11 years ago

Hi Mierla,

Not quite sure what you mean?

Where is the form?

Aram

mierla commented 11 years ago

Thanks for the reply - let me see if I can be clearer:

I've got a DataObjectasPage called 'Representative.' I want users to be able to fill out a form to send an email to that Representative on each RepresentativeHolder/show/ page. To do that, I need to be able to pass the Representative's email address (and ideally some other fields) to the form, but I can't figure out how to get to them.

arambalakjian commented 11 years ago

Ahh ok, this is not really related to this module as it applies to any DO used in this way.

What you need to do is pass in the DO ID to the Form Constructor. To do this you need a function like this on your contoller:

    /*
     * Contact Form
     */
    function DOContactForm($ID = null)
    {
        $form = new DOContactForm($this, 'DOContactForm', $ID);
        $form->setHTMLID('DOContactForm_' . $ID);
        return $form;
    }

And then in your DO you need something like this:

    public function DOContactForm()
    {
        return Controller::curr()->DOContactForm($this->ID);
    }

And finally in your DOContactForm you need something like this:

    function __construct($controller, $name, $DOid) 
    {
        $fields = new FieldList();

        $fields->push(new HiddenField('DOID', 'Data Object ID', $DOid));

        // Create action
        $actions = new FieldSet(
            new FormAction('SendContactForm', 'Send')
        );  

        $validator = new DOContactFormValidator('Email', 'Comments');

        parent::__construct($controller, $name, $fields, $actions, $validator);
    }

Hope that helps!

Aram

mierla commented 11 years ago

Ohh... It definitely does. Thank you. While Silverstripe's not new to me, trying to do this level of stuff is definitely new, and forms have always confused the heck out of me.

One last question: I put the FormAction ('SendContactForm') in DOContactForm as well, right?

arambalakjian commented 11 years ago

No, in that example it would go in the DOContactForm class, which is an extension of Form.

You could also define the form within the controller and have the send function in there too, we just like to separate out forms into their own classes :)

Aram

mierla commented 11 years ago

That makes a lot of sense. So DOContactForm would look like:

class DOContactForm extends Form {
    public function __construct($controller, $name, $DOid) {
        $fields = new FieldList();

        $fields->push(new HiddenField('DOID', 'Data Object ID', $DOid));

        // create action
        $actions = new FieldSet(
            new FormAction('SendContactForm', 'Send')
            );

        $validator = new DOContactFormValidator('Email', 'Comments');

        parent::__construct($controller, $name, $fields, $actions, $validator);
    }

       public function SendContactForm($data, $form) {
               // The stuff it's supposed to do. 
               // Send the email
               // Set a session variable
              return $this->redirectBack();
       }
}

and it's all nicely and neatly packaged up. That's SO much neater than the ungainly mess I had in my controller. I can't thank you enough.

arambalakjian commented 11 years ago

No problem at all :)

Aram