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.

  1. in cms/code/PageTypes/UserDefinedForm.php, modify the UserDefinedForm class definition :
    static $db = array(
    "EmailTo" => "Varchar",
    "EmailOnSubmit" => "Boolean",
    "SubmitButtonText" => "Varchar",
    "OnCompleteMessage" => "HTMLText",
    "Language" => "Varchar"
  2. add the input field in the main tab in getCMSFields function :
    $fields->addFieldToTab("Root.Content.Main", new TextField( 'Language','Language (fr, en, ar)'));
  3. 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/' . $this->Language . '.js');

  4. 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";
  5. 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.

1 reply
  1. Bernhard
    Bernhard says:

    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, would be great!!!

    Thanks Bernhard


Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *