Welcome Guest, Not a member yet? Register   Sign In
not_in_list form validation
#1
Question 

Good morning to you all,

I want to verify the entry of a form so that it is not in a defined list. I want to do the opposite of the in_list validation rule.

So I created my not_in_list function in system/Validation/Rules.php


PHP Code:
//--------------------------------------------------------------------

        /**
         * Value should not be included in an array of values
         *
         * @param  string $value
         * @param  string $list
         * @param  array  $data
         * @return boolean
         */
        public function not_in_list(string $value nullstring $list, array $data): bool
        
{
                $list explode(','$list);
                $list array_map(function ($value) {
                        return trim($value);
                }, $list);
                if (! in_array($value$listtrue))
                {
                        return true;
                }
                return false;
        


My controller audit process is as follows:

PHP Code:
        public function store()
        {
                helper(['form']);
                $sites = new Site();
                $sites implode(','$sites->getSites());
                $validation =  \Config\Services::validation();
                $validation->setRules([
                                'siteName' => 'required|not_in_list['.$sites.']',
                                'password' => 'required',
                                'passwordConfirmation' => 'required|matches[password]'
                        ],
                        [   // Errors
                                'siteName' => [
                                        'required' => 'Merci de fournir un nom de site.',
                                        'not_in_list' => 'Le nom du site est déjà utilisé.'
                                ],
                                'password' => [
                                        'required' => 'Merci de fournir un mot de passe.'
                                ],
                                'passwordConfirmation' => [
                                        'required' => 'Merci de confirmer le mot de passe.',
                                        'matches' => 'Les mots de passe ne correspondent pas.'
                                ]
                        ]
                );
                $validation->withRequest($this->request)
                        ->run();
                if (! $this->validator)
                {
                        echo view('createSite', [
                                'validation' => $validation
                        
]);
                }
                else
                {
                        //form ok
                        echo view('home', [
                                'message' => 'Success'
                        ]);
                }
        


And finally the translation file system/Language/en/Validation.php

PHP Code:
'not_in_list'           => 'The {field} field must not be a one of: {param}.'


Currently the function not_in_list works correctly and returns me true when the form entry is not present in my list. However the validation of my form does not pass and returns me an error without message.

Did I forget something?

My configuration:
  • PHP 7.3.14
  • Red Hat 7
  • CI4-rc3
Cordialy
Reply


Messages In This Thread
not_in_list form validation - by GotExx - 01-28-2020, 06:37 AM
RE: not_in_list form validation - by donpwinston - 01-28-2020, 12:14 PM
RE: not_in_list form validation - by GotExx - 01-29-2020, 06:25 AM
RE: not_in_list form validation - by donpwinston - 02-02-2020, 08:22 PM



Theme © iAndrew 2016 - Forum software by © MyBB