Codeignitor表单验证错误:使用表单验证后无法访问页面以填写表单

时间:2018-11-15 05:13:55

标签: php frameworks

我在codeignitor中创建了一个“添加用户”表单,其链接为http://example.com/users/add。在控制器中,我有以下代码用于表单验证:

$this->form_validation->set_rules('firstName', 'First Name', 'required');
$this->form_validation->set_rules('lastName', 'Last Name', '');
$this->form_validation->set_rules('guardian1', 'Guardian First', 'required');
$this->form_validation->set_rules('guardian2', 'Guardian Second', '');
$this->form_validation->set_rules('mobile', 'Mobile Number', 'required');
$this->form_validation->set_rules('phone', 'Phone Number', '');
$this->form_validation->set_rules('dob', 'Date Of Birth', 'required');
$this->form_validation->set_rules('status', 'Account Status', 'required');
$this->form_validation->set_rules('confirmation', 'Confirm Creation', 'required');

if ($this->form_validation->run == FALSE)
{
  redirect('news');
}
else
{
  redirect('users/view');
}

当我尝试访问要添加用户的URL时,它直接将我重定向到http://example.com/news,而无需打开可填写表单的表单页面。 这是我的“添加用户”表单的表单代码:

<form id="form-project" role="form" method="post" action="<?php echo base_url(); ?>users/add" autocomplete="off" novalidate>
    <div class="form-group-attached">
    <div class="row clearfix">
        <div class="col-md-6">
          <div class="form-group form-group-default required">
            <label>First name</label>
            <input type="text" class="form-control" name="firstName" required>
          </div>
        </div>
        <div class="col-md-6">
          <div class="form-group form-group-default">
            <label>Last name</label>
            <input type="text" class="form-control" name="lastName">
          </div>
        </div>
      </div>
      <div class="row clearfix">
        <div class="col-md-6">
          <div class="form-group form-group-default required">
            <label>Parent I / Guardian</label>
            <input type="text" class="form-control" name="guardian1" required>
          </div>
        </div>
        <div class="col-md-6">
          <div class="form-group form-group-default">
            <label>Parent II / Guardian</label>
            <input type="text" class="form-control" name="guardian2">
          </div>
        </div>
      </div>
      <div class="row clearfix">
        <div class="col-md-6">
          <div class="form-group form-group-default required">
            <label>Mobile</label>
            <input type="text" class="form-control" name="mobile" required>
          </div>
        </div>
        <div class="col-md-6">
          <div class="form-group form-group-default">
            <label>Phone</label>
            <input type="text" class="form-control" name="phone">
          </div>
        </div>
      </div>
      <div class="row clearfix">
        <div class="col-md-6">
          <div class="form-group form-group-default input-group required">
            <div class="form-input-group">
              <label>Date Of Birth</label>
              <input id="start-date" type="text" class="form-control date" name="dob" required>
            </div>
          </div>
        </div>
        <div class="col-md-6">
          <div class="form-group form-group-default input-group">
            <div class="form-input-group">
              <label class="inline">Account Status</label>
            </div>
            <div class="input-group-addon bg-transparent h-c-50">
              <input type="checkbox" name="status" data-init-plugin="switchery" data-size="small" data-color="primary" checked="checked" required />
            </div>
          </div>
        </div>
      </div>
    </div>
    <br>
    <div class="pull-left">
      <div class="checkbox check-primary">
        <input type="checkbox" checked="unchecked" value="1" id="checkbox-agree" name="confirmation">
        <label for="checkbox-agree">I hereby certify that the information above is true and accurate
        </label>
      </div>
    </div>
    <br>
    <button class="btn btn-primary" type="submit">Submit</button>
    <button class="btn btn-default" type="reset"><i class="pg-close"></i>Clear</button>
  </form>

P.s我已经加载了表单验证库和URL帮助器。

2 个答案:

答案 0 :(得分:0)

如果出现以下情况,您需要更改您的身份

if ($this->form_validation->run == FALSE)

if ($this->form_validation->run != FALSE)

然后,如果验证失败,则用户将被重定向回“用户/视图”,如果通过则将重定向至“新闻”。

答案 1 :(得分:0)

如果您想重定向回表单,则重定向语句应位于if块体中

if($this->form_validation->run == FALSE)
{
  redirect('users/view');
}
else
{
  redirect('news');
}