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.

Form Questions /

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

Multilingual Custom Form Template


Go to End


2 Posts   1492 Views

Avatar
FloLech

Community Member, 3 Posts

9 June 2015 at 10:11am

I am building a page with two languages (english and german). For this i am using the translatable module offered on the silverstirpe page (http://addons.silverstripe.org/add-ons/silverstripe/translatable). On regular pages it works just like a charm. The contact form gets created in a controller. By $form->setTemplate i load a custom template for the form, which is located in the template's include subdirectory. In the class file I defined the different TextFields (model part), which I want to use in the form template. Here is the controller:

class MietenPage extends Page {
    private static $db = array (


        //Form
        'ChoosPakage' => 'Varchar',
        'FormEventHeadline' => 'Varchar',
        'FormNoteHeadline' => 'Varchar',
        'FormContactHeadline' => 'Varchar',
        'FormAdditionalHeadline' => 'Varchar',
        'FormCheckOverlay' => 'Varchar',
        'FormCheckCaseDesign' => 'Varchar',
        'FormCheckAGB' => 'Varchar',
        'FormSubmit' => 'Varchar',
        'AGBAlert' => 'Varchar'

    );

    public function getCMSFields() {
        $fields = parent::getCMSFields();

        //...

        return $fields;
    }
}

class MietenPage_Controller extends Page_Controller {
    private static $allowed_actions = array('ContactForm');
    public function ContactForm() { 
        $fields = new FieldList( 
            //...
        ); 
        $actions = new FieldList( 
            new FormAction('submit', 'Anfrage Senden!') 
        );

        $form = new Form($this, 'ContactForm', $fields, $actions); 
        $form->setTemplate('ContactFormTemplate');

        return $form;

    }
    public function submit($data, $ContactForm) { 
            $email = new Email(); 

                //...
        }
}

n my template i am calling the form using $ContactForm. This works totally fine, including all functionalities.

My Problem is, that I can not access the variables (like headline or submit button text eg. $FormEventHeadline), which hold the text in my custom template - just emptyness is returned. I think it has to do something with scope, but i can not solve this problem.

Thanks in advance ;)

Avatar
Pyromanik

Community Member, 419 Posts

9 June 2015 at 11:10am

I think you are confusing database fields with form fields.

private static $db defines the fields available on that model object (ie, database fields).

The fieldlist in your form definition should contain FormField derivitives. You can use _t to translate the field names here.
This may not be what you're trying to achieve but it is the easiest way.
If it is not then I can suggest more options.