Codeigniter - проверка формы выполняется, но не отображает ошибки при FALSE - PullRequest
0 голосов
/ 07 мая 2020

Я столкнулся с ситуацией с проверкой формы. Что-то такое простое, я не могу понять, в чем моя проблема, и довольно долго искал в Интернете. Моя форма, если она оставлена ​​пустой, должна выдавать ошибки через form_error для каждого поля, если form_validation возвращает FALSE. Однако ничего не происходит. Когда форма заполнена полностью, она отлично вставляется в БД. Я застрял и, кажется, не могу понять, какое решение. Я думаю, что в коде где-то есть орфографическая ошибка или форма неправильно спроектирована.

Вот мой контроллер:

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, просто пытаюсь научиться. Я был бы очень признателен, если бы кто-нибудь мог понять, в чем заключается проблема, и помочь мне найти ответ.

1 Ответ

0 голосов
/ 07 мая 2020

Ваше перенаправление не будет передавать сообщения form_error. Вам нужно загрузить представление (вашу форму), чтобы вызвать form_error, используя $this->load->view()

Попробуйте это:

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');
        }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...