Overriding the code validation and using a custom validation is a feature that may be useful for a number of people or cases.
In our case, we had the following requirement
We needed to create custom registration codes (custom format and specific data within).
The format of the registration code had to be in plain text in the backend (registration code text field), but sent to the user encoded so that it would be unreadable.
So upon registration the user would enter the encoded version of the code.
For that case we had to create our own code validation function in order to decrypt the code and validate it against the saved one.
In order for the custom validation function to work we had to actually disable (comment out) the validation callback set in the regcode_simple.module.
Based on the above little hack of the module, i created a patch which does the following
1. Provide a 'Bypass Validation' checkbox in the regcode_simple configuration form
2. If checked, the validation callback defined in the module is not set. This means that no validation will be done.
3. A new, custom validation function must be provided by the developer in order to actually validate the registration code.
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | regcode_simple-bypass_validation-2860862-D8.patch | 3.28 KB | _dcre_ |
Comments
Comment #2
_dcre_ commentedComment #3
rpsuThanks for the patch! Even though overriding the validation is somewhat overriding the core of this module, I can see the need to have custom validation done elsewhere. However, there are some things to consider and some code issues to fix.
No need to remove the trailing comma.
In this description I'd WARN users much more clearly about the fact, that code won't be validated. Also; checking this would disable all text fields (hashes etc.).
One thing to consider would be if user should provide a validation hook/function name here, if they want to override existing validation rules (ang that function existence would be verified on form submit). I think it might make this selection safer.
This must be defined also in
config/schema/regcode_simple.schema.yml, or tests will fail.
Comment #4
_dcre_ commentedHi rpsu, thanx for the quick feedback.
I had a look at your comments and would propose the following.
With the above implementation, i think it will be quite safe to include this feature.
One more thing, about the tests.
I tried to run the tests on a vanilla d8 (latest version) and i am getting the following error
Fatal error: Cannot declare class Drupal\regcode_simple\Tests\RegcodeSimpleCodePlainTextTest, because the name is already in use in /var/www/html/web/modules/regcode_simple/tests/src/Functional/RegcodeSimpleCodePlainTextTest.php on line 90
Comment #5
rpsuI've been pondering if this module should actually provide API and use it for anything else but for the very basic plain txt regcodes.
It would mean all current methods would be moved to separate module(s). However the current structure does not really support that very well, so it would mean quite a lot of work (for the benefit IMHO, regarding the current use base).
Any thoughts? Another way is the one you proposed in comment #4 added with some error handling mechanism in the case where the external module/method would disappear/get uninstalled. Maybe hook_module_preuninstall() would be helpful in that?
Comment #6
megachriz@_dcre_
To bypass the validation callback of this module, the module doesn't need a setting for that. In a custom module you can just alter the user_register form and remove "regcode_simple_form_user_register_form_validate" from
$form['#validate']. You do need to ensure that your custom module's form alter is called later than regcode_simple. You can do that by increasing the module weight:mymodule.install:
@rpsu
An alternative from the current implementation is to use the plugin system. So each registration code type would then be its own plugin. This way, custom modules can add their own implementation.