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.

Form Questions /

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

[SOLVED] validation disabling hell


Go to End


9 Posts   6547 Views

Avatar
Entar

Community Member, 23 Posts

3 March 2009 at 1:11am

Edited: 04/03/2009 5:33am

well, I just want to COMPLETELY disable JavaScript validation in my form, I've tried around 5-6 different ways to do it, including core hacks - no way. What I achieved is removing of Behavior parts, script includes are still on their place. Here's what I am doing:

	function Form() {
		$fields = new FieldSet(
			new CompositeField(
				new HeaderField(_t('Member.MAINDATA', "Main data")),
				new EmailField("Email", _t('Member.EMAIL', "Email", PR_MEDIUM, 'Noun')),
				new PasswordField("Password", _t('Member.PASSWORD', 'Password'))
			)
		);

		...
		//pushing many other fields
		...

		$actions = new FieldSet(
			new FormAction('register', 'Register')
		);

		$requiredFields = new RequiredFields();
		$requiredFields->removeValidation(); // first way
		$requiredFields->setJavascriptValidationHandler('none'); // second way - not working at all, figured out this var is always set to "prototype" in the end
		$requiredFields->set_javascript_validation_handler('none'); // works, but "Valdator.js" and company includes are still here

		$form = new Form($this, 'Form', $fields, $actions, $requiredFields);
		return $form;
	}

Question - how can I remove everything connected with prototype from my form? All my efforts end with JavaScript includes still here...

Another interesting thing is when I completely wiped out "includeJavascriptValidation" method from Validator it continues to add includes... Why the hell? Cache? Perhaps I just don't get it...

And another thing - is double call of "includeJavascriptValidation" really needed in Form.php? Found it in "FormAttributes" and "formHtmlContent"...

My version is 2.3.0 stable, if it does matter. Thanks!

Avatar
Ingo

Forum Moderator, 801 Posts

3 March 2009 at 3:44pm

The documentation for Validator::set_javascript_validatoin_handler() states:
This could be called from _config.php to set site-wide javascript validation, or from ContentController::init() to affect only the front-end site.

What it actually means: This has to be called before the validator is instanciated.. have a look at Validator::__construct().

I've rectified this a bit in http://open.silverstripe.com/changeset/72373 - should make its way into the next minor release.
To completely disable js validation on all forms, it should be sufficient to set the following in your mysite/_config.php:
Validator::set_javascript_validation_handler('none');
Thats assuming you're working on trunk or branches/2.3 - or wait for the next minor release ;)

We're aware the current validation system is ... suboptimal, and hope to find time for a bit more pluggable architecture in 2.4, specifically around removing dependencies to Validator.js. You can see some early-stage discussion at: http://open.silverstripe.com/wiki/development/validation

Avatar
Entar

Community Member, 23 Posts

3 March 2009 at 9:09pm

Edited: 03/03/2009 9:10pm

hi Ingo, I don't want to disable it site-wide, just on particular form. So that's why I'm trying all that code below in form creation method. of course I know about disabling in config.

p.s: I didn't find a way to disable includes in that document you sent link for...

Avatar
Ingo

Forum Moderator, 801 Posts

3 March 2009 at 10:35pm

The document is just brainstorming, not user/dev documentation.

You can disable on form-by-form instance with the following:
$validator = new RequiredFields();
$validator->setJavascriptValidationHandler('none');
$form = new Form($this,'Form',new FieldSet(), new FieldSet(), $validator);

Keep in mind, you'll need branches/2.3 or daily builds for this to work consistently, as it relies on the fixes i've mentioned earlier.

Avatar
Ingo

Forum Moderator, 801 Posts

3 March 2009 at 10:36pm

The document is just brainstorming, not user/dev documentation.

You can disable on form-by-form instance with the following:
$validator = new RequiredFields();
$validator->setJavascriptValidationHandler('none');
$form = new Form($this,'Form',new FieldSet(), new FieldSet(), $validator);

Keep in mind, you'll need branches/2.3 or daily builds for this to work consistently, as it relies on the fixes i've mentioned earlier.

Avatar
Entar

Community Member, 23 Posts

3 March 2009 at 10:40pm

		$requiredFields = new RequiredFields();
		$requiredFields->setJavascriptValidationHandler('none');

		$form = new Form($this, 'Form', $fields, $actions, $requiredFields);

not working

		$requiredFields = new RequiredFields();
		$requiredFields->set_javascript_validation_handler('none');

		$form = new Form($this, 'Form', $fields, $actions, $requiredFields);

working

I am using version from http://open.silverstripe.com/changeset/72373 and yes, it's 2.3.0 from downloads page stable.

Avatar
Ingo

Forum Moderator, 801 Posts

3 March 2009 at 10:49pm

> I am using version from http://open.silverstripe.com/changeset/72373 and yes, it's 2.3.0 from downloads page stable.

Thats kinda contradictory - are you using a subversion checkout of trunk on r72373, or a 2.3.0 stable download? Or did you patch your 2.3.0 with this specific fix? I've tried this specific notation before applying the patch, worked fine for me.

> $requiredFields->set_javascript_validation_handler('none');
Thats just using a static method as an instance method, which is sadly possible in PHP5. Should have the same effect as globally disabling validation.

Avatar
Entar

Community Member, 23 Posts

3 March 2009 at 10:55pm

> Or did you patch your 2.3.0 with this specific fix? I've tried this specific notation before applying the patch, worked fine for me.

It is clean 2.3.0 setup just patched Validator.php (actually replaced original with r72373).

> Thats just using a static method as an instance method, which is sadly possible in PHP5. Should have the same effect as globally disabling validation.

Not good for me then :)

Go to Top