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.

Template Questions /

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

Displaying page CMS field within a rendered SS form


Go to End


2 Posts   1715 Views

Avatar
rp55139

Community Member, 3 Posts

28 July 2016 at 9:38am

I have a requirement to add CMS managed copy (HTML) to a form (specifically some help text) - is there a way I can access the page's data when calling / rendering the form?

The form is being rendered in the page template as $NominationForm (calling a function in the page controller):
............
$form = new Form($this, 'NominationForm', $fields, $actions, $validator);
$form->setTemplate('Forms/NominationForm');

I need to be able to inject NominationIntructionCopy from the NominatePage class into the form somehow.

private static $db = array(
'TitleCopy' => 'Text',
'BodyCopy' => 'Text',
'NominationsCloseCopy' => 'Text',
'NominationIntructionCopy' => 'HTMLText',
);

Thanks in advance.

Avatar
ZarockNZ

Community Member, 17 Posts

21 September 2016 at 4:14pm

Edited: 21/09/2016 4:16pm

Hi, yes this is possible.

I think the LiteralField type could do what you need: http://api.silverstripe.org/master/class-SilverStripe.Forms.LiteralField.html

This field lets you put an arbitrary piece of HTML into your forms.

You can use it like below to include any html. In your case you probably want to set the content of the field to the $this->owner->NominationIntructionCopy


public function HelloForm() {
        $fields = new FieldList(
            TextField::create('Name', 'Your Name')
            // Literal field for any html to go at this place in the form.
            LiteralField::create('Instructions', '<p>My HTML Is here <strong>Bold text</strong> any html</p>')
        );

        $actions = new FieldList(
            FormAction::create("doSayHello")->setTitle("Say hello")
        );

        $required = new RequiredFields('Name');
        $form = new Form($this, 'HelloForm', $fields, $actions, $required);

        return $form;
    }

    public function doSayHello($data, Form $form) {
        $form->sessionMessage('Hello '. $data['Name'], 'success');
        return $this->redirectBack();
    }

If you would like instructions for a number of fields, and the instructions are short, you might want to consider using the 'right title' feature which outputs some text with the field like so...


 $fields = new FieldList(
    TextField::create('Name', 'Your Name')
        ->setRightTitle('Please enter your first name only'),    // Shorter instructions with right title.
);

Cheers,
DouG.