CodeIgniter Forums
custom validation rule(s) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: custom validation rule(s) (/showthread.php?tid=90461)



custom validation rule(s) - bobw - 03-21-2024

I'm getting close to my setup working as I want but came across a problem this evening with the name of prospective club member.
When adding someone to the database (or updating their details) I have a set of validation rules like so...

PHP Code:
            'family_name'      => 'required|max_length[30]|alpha_numeric_space|min_length[3]'
However this person's name contained an apostrophe, think "Scarlett O'hara", well you get the idea.
A look at the documentation and I thought it was a decent enough reason to create a custom validation rule. So created a file "app/Config/ClubRules.php"
PHP Code:
<?php
namespace CodeIgniter\Config;

class 
ClubRules 
Then in Config\Validation.php added
PHP Code:
use CodeIgniter\Config\ClubRules

And updated the $ruleSets array with
PHP Code:
ClubRules::class, 

I.e., pretty much letter for letter as the documentation describes. However I get "Class 'CodeIgniter\Config\ClubRules' not found" which emanates from "CodeIgniter\Validation\Validation->loadRuleSets ()"

Not sure what I've done wrong Confused


RE: custom validation rule(s) - luckmoshy - 03-21-2024

(03-21-2024, 02:33 PM)bobw Wrote: I'm getting close to my setup working as I want but came across a problem this evening with the name of prospective club member.
When adding someone to the database (or updating their details) I have a set of validation rules like so...

PHP Code:
            'family_name'      => 'required|max_length[30]|alpha_numeric_space|min_length[3]'
However this person's name contained an apostrophe, think "Scarlett O'hara", well you get the idea.
A look at the documentation and I thought it was a decent enough reason to create a custom validation rule. So created a file "app/Config/ClubRules.php"
PHP Code:
<?php
namespace CodeIgniter\Config;

class 
ClubRules 
Then in Config\Validation.php added
PHP Code:
use CodeIgniter\Config\ClubRules

And updated the $ruleSets array with
PHP Code:
ClubRules::class, 

I.e., pretty much letter for letter as the documentation describes. However I get "Class 'CodeIgniter\Config\ClubRules' not found" which emanates from "CodeIgniter\Validation\Validation->loadRuleSets ()"

Not sure what I've done wrong Confused

Ok do you have an understanding of the PHP and CI namespace please


RE: custom validation rule(s) - bobw - 03-22-2024

(03-21-2024, 11:42 PM)luckmoshy Wrote: Ok do you have an understanding of the PHP and CI namespace please

I'd say "somewhat" which is why I used the existing namespace rules as a guide. I suspect that the user guide at https://www.codeigniter.com/user_guide/libraries/validation.html#creating-custom-rules assumes you know what to do with regard to namespaces.

Thanks @luckmoshy, I was over specifying the namespaces. I just needed
PHP Code:
<?php
namespace Config;

class 
ClubRules

In my new class file and just update the $ruleSets array with:
PHP Code:
ClubRules::class, 

So I'd say that for clarity the user guide is missing that "namespace XXX;" line from its example.