Welcome Guest, Not a member yet? Register   Sign In
Keep on getting error about max_byte
#1

(This post was last modified: 08-14-2024, 02:36 AM by Fabio-Zeus-Soft.)

Hi everyone and thanks in advance,
I'm working with CodeIgniter 4 and Shield, but every time I try to use max_byte
rule I get this error:
PHP Code:
CodeIgniter\Validation\Exceptions\ValidationException

"max_byte" is not a valid rule

This is happening on Login, where I'm using the config suggested in Shield
documentation:
PHP Code:
public $login = [
        'username' => [
            'label' => 'Auth.email',
            'rules' => [
                'required',
                'max_length[254]',
                'valid_email'
            ],
        ],
        'current-password' => [
            'label' => 'Auth.password',
                'rules' => [
                    'required',
                    'max_byte[72]',
                ],
            'errors' => [
                'max_byte' => 'Auth.errorPasswordTooLongBytes',
            ]
        ],
    ]; 
So I don't understand why it says that the rule is not valid.
Reply
#2

(This post was last modified: 08-14-2024, 03:42 AM by JustJohnQ.)

max_byte validation is part of \shield\Authentication\Passwords\ValidationRules.php.
If you look at the comments in \shield\Authentication\Passwords\ValidationRules.php, it states that if you want to use that class you have to add it to Config/Validation.php in the $rulesets array.
Modify the config/Validation.php file like this (according to ChatGPT):
PHP Code:
<?php

namespace Config;

use 
CodeIgniter\Validation\Validation as BaseValidation;
use 
CodeIgniter\Shield\Authentication\Passwords\ValidationRules// Add this line

class Validation extends BaseValidation
{
    /**
    * Stores the classes that contain the
    * rules that are available.
    *
    * @var string[]
    */
    public $ruleSets = [
        \CodeIgniter\Validation\Rules::class,
        \CodeIgniter\Validation\FormatRules::class,
        \CodeIgniter\Validation\FileRules::class,
        \CodeIgniter\Validation\CreditCardRules::class,
        ValidationRules::class, // Add this line
    ];

    // Your existing code

Reply
#3

Hi ,Try :

PHP Code:
use CodeIgniter\Shield\Authentication\Passwords;


public 
$login = [
        'username' => [
            'label' => 'Auth.email',
            'rules' => ['required','max_length[254]','valid_email'
            ],
        ],
        'current-password' => Passwords::getMaxLengthRule(),
    ]; 
Reply
#4

@JustJohnQ 's reply worked, but I added
PHP Code:
use CodeIgniter\Shield\Authentication\Passwords\ValidationRules
to make it work correctly.
I cannot find it from github, but do not you think that it would be better to add this rule to the ruleset by default since max_byte is already present in the login rules?
Thanks you guys!
Reply
#5

Glad to be of assistance, please mark as solved if you consider it closed.
Reply
#6

(08-16-2024, 07:20 AM)Fabio-Zeus-Soft Wrote: I cannot find it from github, but do not you think that it would be better to add this rule to the ruleset by default since max_byte is already present in the login rules?

The "CodeIgniter\Shield\Authentication\Passwords\ValidationRules" is added by Registrar.
See 
- https://github.com/codeigniter4/shield/b...istrar.php
- https://www.codeigniter.com/user_guide/g...registrars

So by default, you don't need to add it manually.

But if you disable Auto-Discovery of registrars, you need to add the rule manually.
See
- https://www.codeigniter.com/user_guide/g...-discovery
Reply




Theme © iAndrew 2016 - Forum software by © MyBB