1

Here is defined codeigniter4 controller with form validation and view file. How can be reflected on view pages with errors. Controller code is not reflected on view page . Could anyone clarify the issue of codeigniter4 validation.

Controller

public function register_user(){
    helper(['form', 'url']);
    $this->validation =  \Config\Services::validation();
    $validation = $this->validation;
    $rules = [
      'user_name' => [
          'required' => 'All accounts must have usernames provided',
      ],
    ];

    $this->validation->setRules([
            'user_name' => 'required|min_length[2]',
        ],
        $rules
    );

    if (! $this->validate($rules))
    {
      $validationErrors = $this->validation->getErrors();
      return redirect()->back()->withInput()->with('errors', $validationErrors);
    }
}

View

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Login Registration</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" media="screen" title="no title">
  </head>
<body>
 <style type="text/css">
     .error{color: red;}
</style>
<span style="background-color:red;">
  <div class="container">
      <div class="row">
          <div class="col-md-4 col-md-offset-4">
              <div class="login-panel panel panel-success">
                  <div class="panel-heading">
                      <h3 class="panel-title">Please do Registration here</h3>
                  </div>
                  <div class="panel-body">
                      <form role="form" method="post" action="<?php echo base_url('user/register_user'); ?>">
                          <fieldset>
                              <div class="form-group">
                                  <input class="form-control" placeholder="Please enter Name" name="user_name" type="text" autofocus>
                                  <span class="error"><?php echo $validation->getError('user_name'); ?></span>
                              </div>

                              <input class="btn btn-lg btn-success btn-block" type="submit" value="Register" name="register" >

                          </fieldset>
                      </form>
                      <center><b>You have Already registered ?</b> <br></b><a href="<?php echo base_url('user/login_view'); ?>"> Please Login</a></center>
                  </div>
              </div>
          </div>
      </div>
  </div>
</span>
</body>
</html>

Following is the current result: Current result

3 Answers 3

1

you can try this controller

public function register_user(){
            helper('form');
            if (! $this->validate([
            'user_name' => 'required|min_length[3]|max_length[255]'
            ]))
            {
                echo view('user/login');
            }
            else{
                echo view('user/login_view');
            }
        }

and this view:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Login Registration</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" media="screen" title="no title">
  </head>
<body>
<h2></h2>
 <style type="text/css">
 .error{color: red;}


</style>
<span style="background-color:red;">
  <div class="container">
      <div class="row">
          <div class="col-md-4 col-md-offset-4">
              <div class="login-panel panel panel-success">
                  <div class="panel-heading">
                      <h3 class="panel-title">Please do Registration here</h3>
                  </div>
                  <div class="panel-body">

                      <form role="form" method="post" action="<?php echo base_url('user/register_user'); ?>">
                          <fieldset>
                              <div class="form-group">
                              <?= esc($user_name); ?>
                              <span class="error"><?= \Config\Services::validation()->listErrors(); ?></span>
                                  <input class="form-control" placeholder="Please enter Name" name="user_name" type="text" autofocus>

                              </div>

                              <input class="btn btn-lg btn-success btn-block" type="submit" value="Register" name="register" >

                          </fieldset>
                      </form>
                      <center><b>You have Already registered ?</b> <br></b><a href="<?php echo base_url('user/login_view'); ?>"> Please Login</a></center>
                  </div>
              </div>
          </div>
      </div>
  </div>
</span>
</body>
</html>

Dont forget to add route if this use:

$routes->add('/login', 'Login::login');
$routes->add('/user/register_user', 'User::register_user');
Sign up to request clarification or add additional context in comments.

Comments

0

This post is not validated but the response of Wandi Tiger is ok for me.

With several rules you can do simply :

$validation = $this->validate([
    'user_name' => 'required|min_length[10]|max_length[255]',
    'user_pass' => 'required|min_length[10]|max_length[255]'
]); 

if($validation)
{
    echo view('user/login');
}
else{
    echo view('user/login_view');
}

Comments

0

// Keep the old input values upon redirect so they can be used by the old() function

return redirect()->back()->withInput();

// Set a flash message

return redirect()->back()->with('foo', 'message');

You have combined both in your example which will not work because the variable $validation which you are trying to access in view doesn't contain any data because you have not assigned any data to it

So you have to replace

if (! $this->validate($rules))
{
  $validationErrors = $this->validation->getErrors();
  return redirect()->back()->withInput()->with('errors', $validationErrors);
}

with this code:

 if (! $this->validate($rules))
    {
      $data['validation'] = $this->validatior;
      return view('view-name',$data);    //note don't forget to replace view-name. 
    }

Don't change anything in the view file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.