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
#2

(This post was last modified: 01-28-2020, 12:17 PM by donpwinston.)

You shouldn't be modifying stuff in the system directory. Create app/Validation/MyRules.php with the class MyRules(or whatever name you want to call it) that has the method not_in_list(). Make sure you namespace it with App\Validation. Then add \App\Validation\MyRules::class to the $ruleSets array property in  app/Config/Validation.php.

Now when you update CI4 your rules won't get wiped out.
Simpler is always better
Reply
#3

(01-28-2020, 12:14 PM)donpwinston Wrote: You shouldn't be modifying stuff in the system directory. Create app/Validation/MyRules.php with the class MyRules(or whatever name you want to call it) that has the method not_in_list(). Make sure you namespace it with App\Validation. Then add \App\Validation\MyRules::class to the $ruleSets array property in  app/Config/Validation.php.

Now when you update CI4 your rules won't get wiped out.

Hello donpwinston,
Thank you for your answer, so I made the changes you indicated to me. However, the validation continues to fail during the audit.

When I enter a form without error, the validation fails and gives me no error message...

Here is my code

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)
                {
                        var_dump($validation->listErrors()); die;
                        echo view('createSite', [
                                'validation' => $validation
                        
]);
                }
                else
                {
                        //form ok
                        echo view('home', [
                                'message' => 'Success'
                        ]);
                

The var_dump returns the following message:

Code:
string(131) " "

Thank's !!
Reply
#4

(This post was last modified: 02-02-2020, 08:39 PM by donpwinston.)

The way I do it is the following:

PHP Code:
if ($this->request->getMethod() !== 'post' || !$validation->withRequest($this->request)->run())
    print view('your_view', ['validation' => $validation]);
else
    print 'Success'

The reason being validation doesn't fail when nothing is submitted.
Simpler is always better
Reply




Theme © iAndrew 2016 - Forum software by © MyBB