Welcome Guest, Not a member yet? Register   Sign In
Registering custom validation methods in a module
#1

Here is the problem. I've got a module called Contact. It lives in a directory called modules/Contact. I registered a namespace Contact for the module in app/Config/Autoload.php. Everything seems to load fine. Now I want to use a custom validation method to validate input coming from a form field. My question is about the best way to do it. My approach is as follows: According to CI4 documentation, Validation class loads App/Config/Validation.php when used for validation. In the file there is public property called $ruleSets, which points to an array, it contains names of classes with validation rules. I want to add my own class with validation methods to this array. According to CI4 documentation on validation, configuration classes, and their public properties in App/Config/ can be updated using registrars. So, I crated one such registrar, a class, in my Contact module. It lives in Contact\Config\Registrar.php . The class has public static method Validation, which returns an array with an updated $ruleSet. The new value is an array and contains all the standard validation class names + my validation class name. The trouble is public static method Validation I added in my registrar seems not to change the value of $ruleSet defined in App/Config/. Contact/Config/Registrar.php is loaded, but Validation method it contains is not called. So, what's the best method to add custom validation rules without having to updated files which live beyond my module? There is very little on registrars in CI4 documentation. Have I misunderstood how they work and how they are supposed to be used?
Reply
#2

You can add it to the app/Config/Validation.php $ruleSets you must also use the namespace that it is in.

A good example of this is in Myth/Auth README.md.

PHP Code:
Edit app/Config/Validation.php and add the following value to the ruleSets array: \Myth\Auth\Authentication\Passwords\ValidationRules::class 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Thx for the reply. I know this way from the docs. However I want my module to be self-contained. The only change in app/Config/* should be enabling the module through adding its namespace. Everything else, including configuration extensions related to the module should be handled by the module. I have a feeling that this is achievable, just can't get Contact/Config/Registrar.php and Validation() static method get to update Validation config in the app/Config/Validation before getting new instance of Validation class. The implicit registrar example in the docs is good, but somehow does not work for Validation. What am I doing wrong? Any other ideas how to add validation methods in a module without touching app/Config/* files?
Reply
#4

You can try to create your own validation.php file in your module Config folder may work.

I checked all over and cannot find any info on it for modules.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

I looked how Validation object is created. If instantiated directly it accepts a config. The trouble with this option is, __construct also requires some other things when used like this. Besides that, it just feels wrong to use these core classes like that in a framework like CodeIgniter, where services are the way to go. I wish validation class had a setter for manipulating protected properties like validation classes it has loaded upon instantiation. It has not got one for now. Another way is through extending the Validation class in the module and loading an extended config from the module. However, this also feels wrong. There should be no need for extending the class just to add a custom validation method. I wish registrars worked as advertised, because the simplest and the best method would be a quick modification to the App/Config/Validation.php file.  As the last resort one can turn to add Class::validationMethod as a validation rule + add error message for the rule with the validation method used being Class::validationMethod, that's how I used to do it in CI3 and Kohana. It works, but having a validation method like 'Class::validationMethod' rather than 'validationMethod' looks ugly. Due to lack of better options I can live with this ugliness though. Finally, my custom method is for a captcha image. In this case some preloaded method can be used to do validation on user input. However, I'm pretty sure, sooner or later I'll run into something I won't be able to validate without a custom method in a module. As mentioned above, it would be really useful if the validation object had methods for manipulating loaded rules and possibly also templates. Hopefully, in some newer version. Thx for the advice and devoted time. Highly appreciated.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB