Welcome Guest, Not a member yet? Register   Sign In
Unable to use validation rules that I set to the Config file
#1

Hi, I am trying to perform form validation for user registration page with CodeIgniter 4. To do this I have written below codes in Validation.php file located in Config directory.

Config/Validation.php
PHP Code:
    public $userSignUp = [
        
'username' => [
            
'rules' => 'required|min_length[3]|max_length[50]',
            
'errors' => [
                
'required'    => 'You must choose a username.',
                
'min_length'  => 'Username must have at least 3 characters',
                
'max_length'  => 'Username can not have more than 50 characters'
            
]
        ],
        
'email' => [
            
'rules' => 'required|max_length[100]|valid_email|is_unique[user.email]',
            
'errors' => [
                
'required'    => 'You must enter email address',
                
'max_length'  => 'Email address can not have more than 100 characters',
                
'is_unique'   => 'The email address must be unique',
                
'valid_email' => 'Please check the Email field. It does not appear to be valid.'
            
]
        ],
        
'password' => [
            
'rules' => 'required|min_length[8]|max_length[255]',
            
'errors' => [
                
'required'    => 'You must enter a password.',
                
'min_length'  => 'Password must have at least 8 characters',
                
'max_length'  => 'Password can not have more than 255 characters'
            
]
        ]
    ]; 

Then I get this "userSignup" rule in my Users controller.

Controllers/Users.php

PHP Code:
<?php namespace App\Controllers;
class 
Users extends BaseController
{
    public function 
__construct(){
            
        helper
(['url''form']);         
    
}
     
    public function 
index()
    {
        if(
$this->request->getMethod() == 'post') {

            
$validation = \Config\Services::validation(); 
            
$rules$validation->getRuleGroup('userSignup');

            if(!
$this->validate($rules)) {
                
$data['validation'] = $this->validator;
                return 
view('user_register'$data);
            }
            
        } else {
            return 
view('user_register');
        }
    }




Finally, to display those errors in the registration page.

Views/user_register.php


PHP Code:
<?php if(isset($validation)): ?>
       <div class="col-12">
            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                 <?php echo $validation->listErrors() ?>
                 <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                      <span aria-hidden="true">&times;</span>
                 </button>
             </div>
        </div>
 <?php endif; ?>

But, when I submit the form, I get this error "userSignup is not a validation rules group". What is going wrong with my code?
Reply
#2

Did you place your config rules in the class?

PHP Code:
class Validation
{
    public $signup = [
        'username'     => 'required',
        'password'     => 'required',
        'pass_confirm' => 'required|matches[password]',
        'email'        => 'required|valid_email'
    ];


Also you using getRuleGroup where I believe it shoulld be setRuleGroup.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Hi @InsiteFX, finally I solved my problem, I misspelled "userSignUp" in the controller. Now, everything is working fine.  Smile
Reply
#4

Glad you got it working.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB