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.

Form Questions

[SOLVED] validation disabling hell


Go to End
Reply

9 Posts   3501 Views

Avatar
Entar

3 March 2009 at 1:11am (Last edited: 4 March 2009 5:33am), Community Member, 19 Posts

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

3 March 2009 at 3:44pm Forum Moderator, 801 Posts

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

3 March 2009 at 9:09pm (Last edited: 3 March 2009 9:10pm), Community Member, 19 Posts

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

3 March 2009 at 10:35pm Forum Moderator, 801 Posts

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

3 March 2009 at 10:36pm Forum Moderator, 801 Posts

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

3 March 2009 at 10:40pm Community Member, 19 Posts

      $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 [url]http://open.silverstripe.com/changeset/72373[/url] and yes, it's [url=http://silverstripe.org/stable-download/]2.3.0 from downloads page[/url] stable.

Avatar
Ingo

3 March 2009 at 10:49pm Forum Moderator, 801 Posts

> 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

3 March 2009 at 10:55pm Community Member, 19 Posts

> 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 [url=http://open.silverstripe.com/changeset/72373]r72373[/url]).

> 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