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.

Form Questions /

Subclassing Form


Reply


2 Posts   1169 Views

Avatar
zenmonkey

Community Member, 532 Posts

14 January 2010 at 4:46am

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

Avatar
Willr

Forum Moderator, 5513 Posts

14 January 2010 at 9:22am

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.