1

这里定义了带有表单验证和视图文件的 codeigniter4 控制器。如何在有错误的视图页面上反映出来。控制器代码不会反映在视图页面上。谁能澄清codeigniter4验证的问题。

控制器

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);
    }
}

看法

<!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>

以下是当前结果: 当前结果

4

3 回答 3

1

你可以试试这个控制器

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');
            }
        }

这个观点:

<!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>

如果这样使用,请不要忘记添加路线:

$routes->add('/login', 'Login::login');
$routes->add('/user/register_user', 'User::register_user');
于 2019-11-27T04:36:56.333 回答
0

这篇文章未经验证,但 Wandi Tiger 的回应对我来说还可以。

使用几条规则,您可以简单地执行以下操作:

$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');
}
于 2020-01-02T23:09:55.160 回答
0

old()// 在重定向时保留旧的输入值,以便函数可以使用它们

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

// 设置一个闪光信息

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

您在示例中将两者结合起来,这将不起作用,因为您尝试在视图中访问的变量 $validation 不包含任何数据,因为您没有为其分配任何数据

所以你必须更换

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

使用此代码:

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

不要更改视图文件中的任何内容。

于 2020-11-11T07:43:30.123 回答