Jump to:

5552 Posts in 1742 Topics by 1226 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Custom form validation messages

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

Page: 1
Go to End
Author Topic: 1813 Views
  • rndmerle
    Avatar
    Community Member
    24 Posts

    Custom form validation messages Link to this post

    Hi.

    In order to custom the validation messages (javascript ones or others) the only way is to mess up with sapphire files ?
    aka Sapphire/Forms/EmailField.php and/or Sapphire/Lang/en_EN.php ?

    Any way to properly override the $lang array ?

    Thanks for any hint.
    Be safe.

  • theAlien
    Avatar
    Community Member
    131 Posts

    Re: Custom form validation messages Link to this post

    I've been looking for that as well, but I'm afraid that's the only solution.
    Still hoping I'm wrong

  • rndmerle
    Avatar
    Community Member
    24 Posts

    Re: Custom form validation messages Link to this post

    So, I managed to modify texts and translations from within my controller, and thus avoiding to mess up with Sapphire.
    It's not an elegant solution but it works.

    There are 2 issues :
    - It's not possible to set a specific required-validation text for each field. A global "%s is required" is the only option I'm aware of.
    - For the Javascript required-validations, you have to target the field by their Html IDs. Those IDs can change if you modify the form in the CMS.

    class ContactPage_Controller extends UserDefinedForm_Controller {
       
    public function init() {
       parent::init();

       global $lang;

       // Php and Javascript formating-validations for email fields
       $lang['en_US']['EmailField']['VALIDATION'] = $lang['en_US']['EmailField']['VALIDATIONJS'] = 'It seams your email address is not really valid.';

       // Php required-validations
       $lang['en_US']['Form']['FIELDISREQUIRED'] = 'Could you please fill the field %s because it\'s important to us';

       // Javascript required-validations for each field
       Requirements::customScript( "
          if( typeof Behaviour != 'undefined') {
             Behaviour.register({
                '#Form_Form_EditableTextField2' : { requiredErrorMsg: 'Knowing who you are is important to us. Please give us a name, and your company\'s one too, if applicable.' },
                '#Form_Form_EditableEmailField4' : { requiredErrorMsg: 'Please give us your email address, or we sadly won\'t be able ton get in touch with you.' }
             }); // register()
          } // if != undefined
       "); // customScript()
    } // init()
    } // ContactPage_Controller

  • theAlien
    Avatar
    Community Member
    131 Posts

    Re: Custom form validation messages Link to this post

    Didn't know that. That's pretty cool!

    1813 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.