Jump to:

10931 Posts in 2614 Topics by 1810 members

All other Modules

SilverStripe Forums » All other Modules » Userforms - add default fields to form

Discuss all other Modules here.

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

Page: 1
Go to End
Author Topic: 1783 Views
  • b0bro
    Avatar
    Community Member
    38 Posts

    Userforms - add default fields to form Link to this post

    Hi,

    how do you add default fields to a page that is extending userdefinedforms.

    Wanted Scenario eg.
    Admin creates a new event page type and the form tab automatically has two form elements. Eg. a 'RSVP' checkbox and a 'Name' text field.

    Current Problem.
    Currently everytime admin creates a new Event Page type, he/she has to manually add the fields to the form (not so good when you have lots of events all the time)

    Im sure its simple, but cant find the answer. Iv looked at newsletter subscibeform that does this but i get errors trying to mimic it!

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: Userforms - add default fields to form Link to this post

    You would need to subclass userformpage.php as you can't do this out of the box. Then one way you could do this is on your subclass override the onAfterWrite() function to create the fields. I am pretty sure this will be called when you create the page

    // your userformpage subclass file
    function onAfterWrite() {
    if(!$this->Fields() || !$this->Fields()->exists()) {
    $textField = new EditableTextField();
    $textField->ParentID = $this->ID;
    // set any other options
    $textField->write();
    $this->Fields()->push($textField);

    }
    parent::onAfterWrite();
    }

  • b0bro
    Avatar
    Community Member
    38 Posts

    Re: Userforms - add default fields to form Link to this post

    thanks will, I appreciate your help but that went all over my head!

    could you please explain it in basic idiot proof terms...

    I have

    class EventPage extends UserDefinedForm{

    //Removed non-important code for this post

    // your userformpage subclass file
    function onAfterWrite() {
    if(!$this->Fields() || !$this->Fields()->exists()) {
    $textField = new EditableTextField();
    $textField->ParentID = $this->ID;
    // set any other options
    $textField->write();
    $this->Fields()->push($textField);

    }
    parent::onAfterWrite();
    }

    }

    But still i have to manually create the form when i click on the form tab for each new eventpage. Please help me ive spent all day on this and still stuck!

  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Re: Userforms - add default fields to form Link to this post

    Hi mate,

    I have just done this exact thing by simply creating a page type that extends userdefined forms. Will is right in that onAfterWrite will do this for you.

    My needs were to populate alot more defaults than what "new EditableTextField" would provide (not to mention other field types) so I added a few functions.

    Attached is my code for this default form. The setup for the fields is done from line 61 onwards.

    Hope it helps.

    Aaron

    Attached Files
    1783 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.