CodeIgniter Forums
Keep on getting error about max_byte - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Keep on getting error about max_byte (/showthread.php?tid=91465)



Keep on getting error about max_byte - Fabio-Zeus-Soft - 08-14-2024

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.


RE: Keep on getting error about max_byte - JustJohnQ - 08-14-2024

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




RE: Keep on getting error about max_byte - datamweb - 08-14-2024

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(),
    ]; 



RE: Keep on getting error about max_byte - Fabio-Zeus-Soft - 08-16-2024

@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!


RE: Keep on getting error about max_byte - JustJohnQ - 08-16-2024

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


RE: Keep on getting error about max_byte - kenjis - 08-16-2024

(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/blob/develop/src/Config/Registrar.php
- https://www.codeigniter.com/user_guide/general/configuration.html#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/general/modules.html#auto-discovery