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.

Customising the CMS /

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

Custom form validation messages


Go to End


4 Posts   2731 Views

Avatar
rndmerle

Community Member, 24 Posts

24 March 2009 at 3:30am

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.

Avatar
theAlien

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

Avatar
rndmerle

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

Avatar
theAlien

Community Member, 131 Posts

30 March 2009 at 8:58am

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