如何与图像上传验证一起进行表单验证

时间:2019-03-27 17:32:47

标签: codeigniter

我有一个表单,其中有一些输入字段和一个图像上传字段。我验证输入字段,但不知道如何验证图像上传字段以及其他输入字段。我仅使用一种方法来完成这两项任务。我正在使用Codeigniter 3

以下是我的查看文件

<div class="box box-primary">
      <!-- /.box-header --> 
      <!-- form start -->
      <?php 
        $data = array(
          'id' => 'add_store_form'
        );
        echo form_open_multipart('ramay/store/add_store', $data); 
      ?>

        <div class="box-body add-store-form">
          <div class="form-group">
              <label for="store_name">Store Name <span class="text-red">*</span></label>
              <?php
                  $store_name = array(
                      'type'  => 'text',
                      'name'  => 'store_name',
                      'class' => 'form-control',
                      'id'    => 'input_store_name',
                      'value' => ''
                  );
                  echo form_input($store_name);
              ?>
              <?php echo form_error('store_name', '<div class="alert alert-danger" role="alert">', '</div>'); ?>
          </div>

          <div class="form-group">
              <label for="store_logo">Store Logo <span class="text-red">*</span></label>
              <?php
                  $store_logo = array(
                    'type' => 'file',
                    'name' => 'store_logo',
                    'class' => 'form-control',
                    'id' => 'input_store_logo',
                    'value' => ''
                  );
                  echo form_upload($store_logo);
              ?>
              <?php echo form_error('store_logo', '<div class="alert alert-danger" role="alert">', '</div>'); ?>
          </div>

        </div>
        <!-- /.box-body -->

        <div class="box-footer">
          <?php
              $hidden = array(
                  'type'  => 'hidden',
                  'name'  => 'add_store',
                  'value' => ''
              );
              echo form_input($hidden);
          ?>
          <?php
              $submit = array(
                  'name'          => 'add_store',
                  'class'         => 'btn btn-primary',
                  'id'            => 'add_store_submit',
                  'type'          => 'submit',
                  'content'       => 'Submit'
              );
              echo form_button($submit);
          ?> 
          <?php
              $reset = array(
                  'class'         => 'btn btn-default',
                  'type'          => 'reset',
                  'content'       => 'Cancel',
                  'onClick'       => "location.href='ramay/store/store_listing'",
                  'style'         => 'margin-left:10px'
              );
              echo form_button($reset);
          ?> 
        </div>
      <?php echo form_close(); ?>

    </div>

下面是我的控制器

public function add_store()
{
    if (isset($_POST['add_store'])) {
        $config['upload_path']          = './uploads/stores/';
        $config['allowed_types']        = 'gif|jpg|png';
        $config['max_size']             = 100;
        $config['max_width']            = 1024;
        $config['max_height']           = 768;

        $this->load->library('upload', $config);

        $store_form_rules = array(
            array(
                'field' => 'store_name',
                'label' => 'Store name',
                'rules' => 'required|trim|xss_clean|max_length[75]'
            ),
            array(
                'field' => 'store_logo',
                'label' => 'Store logo',
                'rules' => 'required|trim|xss_clean'
            )
        );

        $this->form_validation->set_rules($store_form_rules);

        $this->form_validation->set_message('required', '{field} is required');


        if ($this->form_validation->run()) {
            echo 'working';
        }
    }

    $data['page_title'] = 'Add Store';
    $this->load->view('ramay/add_store', $data);
}

我想知道如何同时验证表单和文件上传

2 个答案:

答案 0 :(得分:0)

使用CodeIgniter的上传库时,您可以在控制器中执行以下操作:

if (!$this->upload->do_upload('store_logo'))
{
  // do whatever it is that you do if the image wasn't uploaded
  // you may even use $this->upload->display_errors() to see or display the error messages
}

您可能已经知道,form_validation帮助程序不适用于多部分表单的二进制部分

答案 1 :(得分:0)

如果出现回显“工作”,则表示您已通过基本验证进行了验证

if ($this->form_validation->run()) {
      echo 'working'; // all fine except image upload
      if (!$this->upload->do_upload('store_logo')){
        //store_logo is input name in your form
        //if coming in this block means error 
        $err = $this->upload->display_errors();
      }else{
        //image has been uploaded
        // you can get all details of uploaded images data
        $upload_data_array = $this->upload->data();
      }

}

有关更多信息,您可以访问-https://www.codeigniter.com/userguide3/libraries/file_uploading.html