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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Customising the CMS /

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

Custom form validation messages

Go to End

4 Posts   2328 Views


Community Member, 24 Posts

24 March 2009 at 3:30am


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.


Community Member, 131 Posts

27 March 2009 at 10:18am

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


Community Member, 24 Posts

27 March 2009 at 1:23pm

Edited: 01/04/2009 6:46am

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() {

	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') {
				'#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


Community Member, 131 Posts

30 March 2009 at 8:58am

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