Tips & Tricks, Web Applications
- July 21, 2008
Silverstripe form validation translation
Silverstripe’s nice form builder, in its current version, has one big drawback : it doesnot handle translations very well. Coming from a discussion on Silverstripe’s forums, I have decided to manage my own translation solution for UserDefinedForms.
The idea is to extend the UserDefinedForm pagetype to let it accept an additional field to handle language information.
- in cms/code/PageTypes/UserDefinedForm.php, modify the UserDefinedForm class definition :
static $db = array(
“EmailTo” => “Varchar”,
“EmailOnSubmit” => “Boolean”,
“SubmitButtonText” => “Varchar”,
“OnCompleteMessage” => “HTMLText”,
“Language” => “Varchar”
); - add the input field in the main tab in getCMSFields function :
$fields->addFieldToTab(”Root.Content.Main”, new TextField( ‘Language’,'Language (fr, en, ar)’)); - in the same file, modify the UserDefinedForm_Controller init function in order to let the public form rendering to load specific javascript files :
Requirements::javascript(’jsparty/local/en.js’);
Requirements::javascript(’jsparty/local/’ . $this->Language . ‘.js’); - Create the language specific javascript files in the jsparty folder :
local/test.js : var global_validation_message=”Please fill out this field”;
local/fr.js : global_validation_message=”Veuillez renseigner ce champ”; - Modify the main validation handler, in spahire/javascript/Validation.js, line 149 :
replace var errorMessage = “Please fill out \”$FieldLabel\”, it is required.”;
with var errorMessage=global_validation_message;
Comments are open. For other methods, please use silverstripe’s forum dedicated thread.



One Response to “Silverstripe form validation translation”
Hey -nice work! You seem to be familiar with the silverstripeCMS or at least to be a good coder
Besides Validation – is there an easy way of translating all the fieldsets? For example name, email, message…feedback would be great!!!
Thanks Bernhard
By Bernhard on Sep 8, 2008