【问题标题】:Codeigniter - Form validation runs, but won't display errors on FALSECodeigniter - 表单验证运行,但不会在 FALSE 时显示错误
【发布时间】:2020-08-23 03:38:44
【问题描述】:

我遇到了表单验证的情况。对于这么简单的事情,我似乎无法弄清楚我的问题在哪里,我已经在网上搜索了很长时间。如果form_validation 返回 FALSE,我的表单在留空时应该通过form_error 为每个字段抛出错误。然而,什么也没有发生。当表格完全填写后,它就可以插入到数据库中。我被卡住了,我似乎无法弄清楚解决方案是什么。我的想法是代码在某处有拼写错误,或者表单设计不正确。

这是我的控制器:

function create_job()
    {
        // validate each form field
        $this->form_validation->set_rules('repair_order', 'Repair Order', 'required');
        $this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
        $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
        $this->form_validation->set_rules('phone', 'Phone', 'required');
        $this->form_validation->set_rules('vin', 'VIN', 'trim|required');
        $this->form_validation->set_rules('year', 'Vehicle Year', 'trim|required|min_length[4]');
        $this->form_validation->set_rules('make', 'Vehicle Make', 'required');
        $this->form_validation->set_rules('model', 'Vehicle Model', 'required');
        $this->form_validation->set_rules('start_date', 'Start Date', 'required');
        $this->form_validation->set_rules('promise_date', 'Promise Date', 'required');
        $this->form_validation->set_rules('body_hours', 'Body Labor Hours', 'required');
        $this->form_validation->set_rules('paint_hours', 'Paint Labor Hours', 'required');
        $this->form_validation->set_rules('body_tech', 'Body Technician', 'required');
        $this->form_validation->set_rules('paint_tech', 'Paint Technician', 'required');

            if($this->form_validation->run())
            {
                $data = array(
                'repair_order'      => $this->input->post('repair_order'),
                'first_name'        => $this->input->post('first_name'),
                'last_name'         => $this->input->post('last_name'),
                'address'           => $this->input->post('address'),
                'city'              => $this->input->post('city'),
                'state'             => $this->input->post('state'),
                'zip_code'          => $this->input->post('zip_code'),
                'phone'             => $this->input->post('phone'),
                'vin'               => $this->input->post('vin'),
                'year'              => $this->input->post('year'),
                'make'              => $this->input->post('make'),
                'model'             => $this->input->post('model'),
                'start_date'        => $this->input->post('start_date'),
                'promise_date'      => $this->input->post('promise_date'),
                'body_hours'        => $this->input->post('body_hours'),
                'paint_hours'       => $this->input->post('paint_hours'),
                'insurance'         => $this->input->post('insurance'),
                'body_tech'         => $this->input->post('body_tech'),
                'paint_tech'        => $this->input->post('paint_tech')
                );
                $this->db->insert('jobs', $data);

                $this->session->set_flashdata("success", "New job has been successfully created.");
                redirect('main');
            }
            else
            {
                redirect('jobs/new');
            }
    }

这是我的表格:

<div class="my-3 my-md-5">
  <div class="container">
    <div class="row">
      <div class="col-lg-12">
        <div class="card">
          <div class="card-header">
          <h3 class="card-title">New Job</h3>
          </div>
            <div class="card-body">
               <form action="<?php echo base_url(); ?>jobs/create" method="post"> 
                <div class="form-group">
                  <label class="form-label">Repair Order/Job Name</label>
                  <input type="text" class="form-control" name="repair_order" placeholder="Enter Job Name or Number" value="<?php echo set_value('repair_order'); ?>">
                  <span class="text-danger"><small><?php echo form_error('repair_order'); ?></small></span>
                </div>
                <h3>Customer Information</h3>
                <div class="row">
                  <div class="form-group col-sm-6 col-md-6">
                    <label class="form-label">First Name</label>
                    <input type="text" class="form-control" name="first_name" placeholder="First Name" value="<?php echo set_value('first_name'); ?>">
                    <span class="text-danger"><small><?php echo form_error('first_name'); ?></small></span>
                  </div>
                  <div class="form-group col-sm-6 col-md-6">
                    <label class="form-label">Last Name</label>
                    <input type="text" class="form-control" name="last_name" placeholder="Last Name" value="<?php echo set_value('last_name'); ?>">
                    <span class="text-danger"><small><?php echo form_error('last_name'); ?></small></span>
                  </div>
                  <div class="form-group col-md-6">
                    <label class="form-label">Address</label>
                    <input type="text" class="form-control" name="address" placeholder="Home Address" value="<?php echo set_value('address'); ?>">
                  </div>
                  <div class="form-group col-sm-6 col-md-6">
                    <label class="form-label">City</label>
                    <input type="text" class="form-control" name="city" placeholder="City" value="<?php echo set_value('city'); ?>">
                  </div>
                  <div class="form-group col-md-4">
                    <label class="form-label">State</label>
                    <select class="form-control" name="state">
                      <option value="<?php echo set_value('state'); ?>"><?php echo set_value('state'); ?></option>
                      <option value="">Ohio</option>
                    </select>
                  </div>
                  <div class="form-group col-sm-6 col-md-4">
                    <label class="form-label">Zip Code</label>
                    <input type="text" class="form-control" name="zip_code" placeholder="Zip Code" value="<?php echo set_value('zip_code'); ?>">
                  </div>
                  <div class="form-group col-sm-6 col-md-4">
                    <label class="form-label">Phone</label>
                    <input type="text" class="form-control" name="phone" placeholder="Phone" value="<?php echo set_value('phone'); ?>">
                    <span class="text-danger"><small><?php echo form_error('phone'); ?></small></span>
                  </div>
                </div>
                <h3>Vehicle Information</h3>
                <div class="row">
                  <div class="form-group col-sm-6 col-md-9">
                    <label class="form-label">VIN</label>
                    <input type="text" class="form-control" name="vin" placeholder="VIN" value="<?php echo set_value('vin'); ?>">
                    <span class="text-danger"><small><?php echo form_error('vin'); ?></small></span>
                  </div>
                  <div class="form-group col-sm-3 col-md-3">
                    <label class="form-label">Vehicle Year</label>
                    <input type="text" class="form-control" name="year" placeholder="Year" value="<?php echo set_value('year'); ?>">
                    <span class="text-danger"><small><?php echo form_error('year'); ?></small></span>
                  </div>
                  <div class="form-group col-sm-6 col-md-6">
                    <label class="form-label">Vehicle Make</label>
                    <input type="text" class="form-control" name="make" placeholder="Make" value="<?php echo set_value('make'); ?>">
                    <span class="text-danger"><small><?php echo form_error('make'); ?></small></span>
                  </div>
                  <div class="form-group col-sm-6 col-md-6">
                    <label class="form-label">Vehicle Model</label>
                    <input type="text" class="form-control" name="model" placeholder="Model" value="<?php echo set_value('model'); ?>">
                    <span class="text-danger"><small><?php echo form_error('model'); ?></small></span>
                  </div>
                </div>
                <h3>Workflow Information</h3> 
                <div class="row">
                  <div class="form-group col-sm-6 col-md-6">
                    <label class="form-label">Start Date</label>
                    <input type="date" class="form-control" name="start_date" value="<?php echo set_value('start_date'); ?>">
                    <span class="text-danger"><small><?php echo form_error('start_date'); ?></small></span>
                  </div>
                  <div class="form-group col-sm-6 col-md-6">
                    <label class="form-label">Promise Date</label>
                    <input type="date" class="form-control" name="promise_date" value="<?php echo set_value('promise_date'); ?>">
                    <span class="text-danger"><small><?php echo form_error('promise_date'); ?></small></span>
                  </div>
                  <div class="form-group col-sm-6 col-md-3">
                    <label class="form-label">Total Body Labor Hours</label>
                    <input type="text" class="form-control" name="body_hours" placeholder="Body Labor" value="<?php echo set_value('body_hours'); ?>">
                    <span class="text-danger"><small><?php echo form_error('body_hours'); ?></small></span>
                  </div>
                  <div class="form-group col-sm-6 col-md-3">
                    <label class="form-label">Total Paint Labor Hours</label>
                    <input type="text" class="form-control" name="paint_hours" placeholder="Paint Labor" value="<?php echo set_value('paint_hours'); ?>">
                    <span class="text-danger"><small><?php echo form_error('paint_hours'); ?></small></span>
                  </div>
                  <div class="form-group col-sm-6 col-md-6">
                    <label class="form-label">Insurance Company</label>
                    <input type="text" class="form-control" name="insurance" placeholder="Insurance Company" value="<?php echo set_value('insurance'); ?>">
                  </div>
                  <div class="form-group col-sm-6 col-md-6">
                    <label class="form-label">Body Technician</label>
                    <select class="form-control" name="body_tech">
                      <option value="<?php echo set_value('body_tech'); ?>"><?php echo set_value('body_tech'); ?></option>
                      <option value="Roy Whittenberger">Roy Whittenberger</option>
                    </select>
                    <span class="text-danger"><small><?php echo form_error('body_tech'); ?></small></span>
                  </div>
                  <div class="form-group col-sm-6 col-md-6">
                    <label class="form-label">Paint Technician</label>
                    <select class="form-control" name="paint_tech">
                      <option value="<?php echo set_value('paint_tech'); ?>"><?php echo set_value('paint_tech'); ?></option>
                      <option value="Joel Lakosh">Joel Lakosh</option>
                    </select>
                    <span class="text-danger"><small><?php echo form_error('paint_tech'); ?></small></span>
                  </div>
                </div>
                <div class="form-footer">
                  <button class="btn btn-primary btn-block" name="submit" type="submit">Submit</button>
                </div>
              </form>
              <div class="clearfix"></div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

我是 CodeIgniter 的新手,只是想学习。如果有人能看到问题所在并能指导我找到答案,我将不胜感激。

【问题讨论】:

  • 如果表单成功,重定向到主要工作吗?
  • 是的,TRUE 的参数有效,只有在表格填写完整的情况下。如果表单没有填写完整,它会返回 FALSE,但不显示错误消息。
  • 另外,所有需要的东西都已经在自动加载中设置好了。我的项目中也有多个没有问题的表单。尤其是这个,就是不想正常工作。
  • 而不是redirect('jobs/new');使用 $this->load->view('jobs/new') - 或任何适合您表单的视图
  • 修复了它。谢谢!有趣的是,redirectview 这样简单的事情如何改变事情。如果您愿意,请提交作为答案,我会为您投票。

标签: php forms codeigniter validation


【解决方案1】:

您的重定向不会传递 form_error 消息。您需要加载视图(您的表单)才能使用 $this-&gt;load-&gt;view() 调用 form_error

试试这个:

function create_job()
{
    // validate each form field
    $this->form_validation->set_rules('repair_order', 'Repair Order', 'required');
    $this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
    $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
    $this->form_validation->set_rules('phone', 'Phone', 'required');
    $this->form_validation->set_rules('vin', 'VIN', 'trim|required');
    $this->form_validation->set_rules('year', 'Vehicle Year', 'trim|required|min_length[4]');
    $this->form_validation->set_rules('make', 'Vehicle Make', 'required');
    $this->form_validation->set_rules('model', 'Vehicle Model', 'required');
    $this->form_validation->set_rules('start_date', 'Start Date', 'required');
    $this->form_validation->set_rules('promise_date', 'Promise Date', 'required');
    $this->form_validation->set_rules('body_hours', 'Body Labor Hours', 'required');
    $this->form_validation->set_rules('paint_hours', 'Paint Labor Hours', 'required');
    $this->form_validation->set_rules('body_tech', 'Body Technician', 'required');
    $this->form_validation->set_rules('paint_tech', 'Paint Technician', 'required');

        if($this->form_validation->run())
        {
            $data = array(
            'repair_order'      => $this->input->post('repair_order'),
            'first_name'        => $this->input->post('first_name'),
            'last_name'         => $this->input->post('last_name'),
            'address'           => $this->input->post('address'),
            'city'              => $this->input->post('city'),
            'state'             => $this->input->post('state'),
            'zip_code'          => $this->input->post('zip_code'),
            'phone'             => $this->input->post('phone'),
            'vin'               => $this->input->post('vin'),
            'year'              => $this->input->post('year'),
            'make'              => $this->input->post('make'),
            'model'             => $this->input->post('model'),
            'start_date'        => $this->input->post('start_date'),
            'promise_date'      => $this->input->post('promise_date'),
            'body_hours'        => $this->input->post('body_hours'),
            'paint_hours'       => $this->input->post('paint_hours'),
            'insurance'         => $this->input->post('insurance'),
            'body_tech'         => $this->input->post('body_tech'),
            'paint_tech'        => $this->input->post('paint_tech')
            );
            $this->db->insert('jobs', $data);

            $this->session->set_flashdata("success", "New job has been successfully created.");
            redirect('main');
        }
        else
        {
            $this->load->view('yournewjobform');
        }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    • 2016-02-18
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    • 2013-09-05
    相关资源
    最近更新 更多