Jump to:

2012 Posts in 1438 Topics by 621 members

Form Questions

SilverStripe Forums » Form Questions » Subclassing Form

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

Page: 1
Go to End
Author Topic: 1123 Views
  • zenmonkey
    Avatar
    Community Member
    528 Posts

    Subclassing Form Link to this post

    I'm having trouble understanding the purpose of extending the Form object.

    I thought creating a form Subclass would allow me to call it from any PageType, I'm trying to create a form that I can use on it Originally Intended page type as well as a Virtual Page

    Below is the as it exists on my original PageType

    function UserProductReviewForm() {
    // Create fields
    $fields = new FieldSet(
           new TextField('Name','Name:'),
           new EmailField('Email','E-Mail Address:'),
           new TextField('Location','Location:'),
           new OptionsetField('Rating','Rating',array(
             '1' => '1',
             '2' => '2',
             '3' => '3',
             '4' => '4',
             '5' => '5',
           )),
           new TextField('Title','Review Title:'),
           new TextareaField('Review'),
           new HiddenField('ProductID', '', $this->ID),
           new HiddenField('ProductName','',$this->Title)
    );
    // Create actions
    $actions = new FieldSet(
    new FormAction('doReview', 'Submit')
    );

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

    And the New Custom Form

    class NewReviewForm extends Form {
       /**Create Review Form****
       ************************
       ************************/
       function __construct($controller, $name) {
    // Create fields
        //$recaptchaField = new RecaptchaField('MyCaptcha');
       
       
    $fields = new FieldSet(
          new TextField('Name','Name:'),
          new EmailField('Email','E-Mail Address:'),
          new TextField('Location','Location:'),
          new OptionsetField('Rating','Rating',array(
             '1' => '1',
             '2' => '2',
             '3' => '3',
             '4' => '4',
             '5' => '5',
          ),
          new TextField('Title','Review Title:'),
          new TextareaField('Review'),
          new HiddenField('ProductID', '', $this->ID),
          new HiddenField('ProductName','',$this->Title)
    );
    // Create actions
    $actions = new FieldSet(
    new FormAction('doReview', 'Submit')
    );
       
       parent::__construct($controller, $name, $fields, $actions);
       
    }

    How would I call this form in template?
    And where do I define the new Action?

    Thanks

  • Willr
    Avatar
    Forum Moderator
    5511 Posts

    Re: Subclassing Form Link to this post

    Well you would need to have a function on your page controller to load the form like

    function UserProductReviewForm() {
    return new NewReviewForm($this, 'UserProductReviewForm');

    And you can then include it in the template with $UserProductReviewForm.

    Your submission method would go on that same Page_Controller.

    1123 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.