How To Fix Form Validation Error in Codeigniter 4

4.6k Views Asked by At

I have used Codeigniter 4 Form Validation in login form.Also,the validation form works well or good. When the login page is loaded Before that imported field also is showed the errors. In below ,I inserted the controller and view codes that relate to section the login. I check different ways but i didnt get way that to solve that. Please guidance me for solve it.

User Controller:

<?php namespace App\Controllers;

use App\Models\UserModel;

class User extends BaseController
{
    public function index()
    {
        echo "User index";
        // Load views
//        echo view('static/header');
//        echo view('front/all');
//        echo view('static/footer');
    }

    public function login()
    {
        helper('form');

        $val = $this->validate([
            'username' => 'required',
            'password' => 'required',
        ]);

        if (!$val)
        {
            echo view('front/login', [
                'validation' => $this->validator
            ]);
        }
        else
        {
            return redirect()->to('/');
        }

    }

    public function logout()
    {

    }

    public function register()
    {
        // Load views
        echo view('front/register');
    }
}

login.php(login page view):

<body>
<?= \Config\Services::validation()->listErrors(); ?>
<div class="limiter">
    <div class="container-login100">
        <div class="wrap-login100 p-b-160 p-t-50">
            <?php echo form_open(base_url('user/login'),array('class'=>'login100-form validate-form')); ?>
                    <span class="login100-form-title p-b-43">
                        Login Form
                    </span>

                <div class="wrap-input100 rs1 validate-input">
                    <input class="input100" type="text" name="username">
                    <span class="label-input100">User Name</span>
                </div>


                <div class="wrap-input100 rs2 validate-input">
                    <input class="input100" type="password" name="password">
                    <span class="label-input100">Password</span>
                </div>

                <div class="container-login100-form-btn">
                    <button class="login100-form-btn" type="submit">
                        Login
                    </button>
                </div>

                <div class="text-center w-full p-t-23">
                    <a href="#" class="txt1">
                        Forget Password?
                    </a>
                </div>

                <div class="text-center w-full p-t-23">
                    <a href="<?php echo base_url('user/register'); ?>" class="txt1">
                        SignUp
                    </a>
                </div>
            </form>
        </div>
    </div>
</div>

</body>

Output(Login Page First Time Visit): localhost/user/login

2

There are 2 best solutions below

0
usman ikram On

As per my understanding of your problem, you are always getting error. And your expected behavior is to only get error when you post empty username and password.

Solution: You have to run validation when form is posted.

Description In your code, before showing form you are validating data. Which is obviously be empty. Below is the solution, change your login function with following code. (It's not optimal solution, but it will clear your concept).


    public function login()
    {
        helper('form');

        $val = null;

        if($this->request->getMethod() === 'post') {// checking if request is post
            $val = $this->validate([
                'username' => 'required',
                'password' => 'required',
            ]);
        }


        if (!$val)
        {
            echo view('front/login', [
                'validation' => $this->validator
            ]);
        }
        else
        {
            return redirect()->to('/');
        }

    }
0
Sanath L S On

this is simple solution for validating form and retaining values if error in form

controller

public function form()
{
    $validation =  \Config\Services::validation();
    $validation->setRules([
    'email' => 'required',
    'password' => 'required',
    ]);

    if($validation->withRequest($this->request)->run())
    {
         //valid form
    }
    else
    {
      return redirect('')->withInput();
    }
}

view

<form method="post" action="<?= base_url('');?>">
    <input type="email" name="email" value="<?= old('email') ?>">
     <input type="password" name="password" value="<?= old('password') ?>">
     <button type="submit" name="submit">Submit</button>
</form>