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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

All other Modules /

Discuss all other Modules here.

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

Userforms - add default fields to form


Go to End


4 Posts   3067 Views

Avatar
b0bro

Community Member, 38 Posts

30 September 2009 at 2:29pm

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!

Avatar
Willr

Forum Moderator, 5523 Posts

30 September 2009 at 3:33pm

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();
}

Avatar
b0bro

Community Member, 38 Posts

30 September 2009 at 4:11pm

Edited: 30/09/2009 8:11pm

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!

Avatar
Double-A-Ron

Community Member, 607 Posts

22 October 2009 at 2:01pm

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