Welcome Guest, Not a member yet? Register   Sign In
Right Sets for Validation Callbacks
#1
Lightbulb 

I'm having a bit of an issue on understanding how to create my own custom callback form validation. I keep getting the message "callback_test_validation is not a valid rule"

What I've done:

  1. I created a directory outside of app: Modules\Rules, so my setup looks like:
    app\
    Modules\
      |- Rules\

  2. My class (Modules\Rules\MyRules.php) is setup as such:
    Code:
    <?php namespace Modules\Rules;

    class MyRules
    {
        public function test_validate(string $str, string &$error = null): bool
        {
            $error = 'test error';
            return false;
        }
    }

  3. I've set the Config\Validation as such:
    Code:
        public $ruleSets = [
            Rules::class,
            FormatRules::class,
            FileRules::class,
            CreditCardRules::class,
            \Modules\Rules\MyRules::class,
        ];

  4. In my controller, I'm trying to reference the callback validation as such:
    Code:
    $validation =  \Config\Services::validation();
    $rules = ['myname'=>['label'=>'My Name','rules'=>'callback_test_validation']];
    $validation->setRules($rules);
Is this the right way to do it? Do I need to setup my folder structure differently?
Will [daytodata]
Reply
#2

Read
https://codeigniter4.github.io/CodeIgnit...namespaces
https://codeigniter4.github.io/CodeIgnit...stom-rules

And the rule name is `test_validation`, not `callback_test_validation`.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB