Bypassing Form Validations and Required Fields in Drupal: the BFV module.
Bypassing Form Validations and Required Fields in Drupal: the BFV module.
Required or not required? To validate or not to validate? That is the question. So you've setup (the site builder's way, no custom forms) your required fields and custom validations for Node types, just to get this feedback from the customer:
That field we defined as mm..... as required (something trivial and not really critical such as an image file) is actually not always required. Users X and Y should be able to bypass that restriction.
No problem, you hook into hook_form_alter and type in some magic to remove those validations, just to find out that they come up with the same thing again and again on other fields. Now your hook_alter starts to look messy. The truth is that on nearly any project there will be a sort of Super role, that expects to see little to no restrictions on what they are doing on the site. And this is not good because those restrictions were put there for something in the first place. But the truth is that some of this restrictions are critical to your application's logic, and other are simply not.
At DrupalOnWindows.com we came up with some utility methods (we have a small development framework module that is consumed in all projects) that could be used to remove all form validations and required fields in a form with ease. This is how Bypass Form Validations module was born.
The module exposes two new permissions (no need to explain what they do....):
- Bypass Form Validations
- Bypass Required Fields
By default, the module will only take action on Node forms.
If you need something more customized, or to use it on other forms, you have two options.
You can use the module directly as an API, with this two calls:
\Drupal\bypass_form_validations\FormBypasser::RemoveRequiredFields($form, $form_id);
\Drupal\bypass_form_validations\FormBypasser::RemoveFormValidations($form, $form_id);
The module is designed to work at a form level, not field level, so those two calls remove all form validations and required fields.
The module also exposes a hook, hook_bypass_form_validations($form, $form_id), that you can consume to tell the module that it should remove Form Validations and Required Fields in the $form by simply returning TRUE if you custom conditions are met.
mymodule_bypass_form_validations($form, $form_id) {
global $user;
// Your custom logic here
if ($form_id == 'My Form Id' && $user->uid == 1) {
return TRUE;
}
}
Be advised, the module only takes default action on Node forms because:
- It is anoying to have validations and required fields removed in all forms. Imagine something as the site-wide search form that shows up in every page. This module does a drupal_set_message when it has taken action on a form, so the user will be seeing messages all over the site - not friendly.
- I have seen some form validations functions perform non validation logic in contributed modules. If you remove validation from these forms, you are actually breaking them.
As a side note, despite being a very small module, we decided to make it dependant on the XAutoload module. If you are not using XAutoload (or any autoload strategy) in all your D7 projects by now mmmm.... no words.
Comments
vernond replied on Permalink
Thanks so much for this module - a massive help!
For a client of ours I needed to add extra options:
* support for Webform forms;
* handle browser-based validation (HTML5 novalidate & formnovalidate attributes).
I've posted my code changes as an issue at https://www.drupal.org/node/2600318 if you want to have a look-see (I'm not set up for creating patches).
Thanks again.
Add new comment