簡體   English   中英

與Codeigniter中的其他輸入字段一起驗證可選的圖片上傳字段

[英]Validate optional image upload field with other input fields in Codeigniter

我在Codeigniter中遇到問題,當文件上傳為可選時如何驗證表單。 我創建了普通表單,可以同時驗證表單中的輸入字段和文件上傳字段,並且工作正常。 但是我想知道當文件選擇是可選的時如何驗證該表格。

這是我的View php文件(news_add.php)

<?php
    /**
     * Created by PhpStorm.
     * User: Anu
     * Date: 8/31/2018
     * Time: 10:00 PM
     */?>
    <?php include('partials/admin_header.php'); ?>
    <?php include("partials/admin_sidebar.php"); ?>
    <div class="content-wrapper">
        <div class="container-fluid">
            <!-- Breadcrumbs-->
            <ol class="breadcrumb">
                <li class="breadcrumb-item">
                    <a href="<?php echo base_url(); ?>admin/dashboard">Dashboard</a>
                </li>
                <li class="breadcrumb-item active">Test</li>
            </ol>
            <div class="container mb-4">
                <div class="row">
                    <div class="mx-auto col-10">
                        <!-- form user info -->
                        <div class="card">
                            <div class="card-header">
                                <h3>Add News & Event</h3>
                            </div>
                            <div class="card-body">
                                <?php $attributes = array('id'=>'news_add','class'=>'form-horizontal'); ?>
                                <?php echo form_open_multipart('Add/add_data', $attributes); ?>
                                <div class="form-group">
                                    <b>
                                        <?php echo form_label('User Full Name <small class="text-success"> (required) </small>','', $attributes=array("class" => "control-label col-sm-12 col-lg-12 col-md-12 col-xs-12")); ?>
                                    </b>
                                    <div class="col-sm-12 col-lg-12 col-md-12 col-xs-12">
                                        <?php echo form_input('u_name','', $attributes=array("class" => 'form-control col-sm-12 col-lg-12 col-md-12 col-xs-12',
                                            "id"=>"u_name","placeholder"=>"Enter your full name")); ?>
                                        <?php echo form_error('u_name','<div class="text-danger mb-3">', '</div>'); ?>
                                    </div>
                                </div>
                                <div class="col-sm-12 col-lg-12 col-md-12 col-xs-12">
                                    <div class="form-group">
                                        <label for="img">Image <small class="text-success"> (required) </small></label><br>
                                        <div class="form-group">
                                            <input type="file" class="form-control-file" name="userfile" size="20">
                                        </div>
                                        <?php echo form_error('userfile','<div class="text-danger mb-3">', '</div>'); ?>
                                        <?php if(isset($error)) echo "<div class='text-danger mb-3'>$error</div>"; ?>
                                    </div>
                                <div class="form-group">
                                    <div class="col-sm-12 col-lg-12 col-md-12 col-xs-12">
                                        <?php $data = array('
                                            class'=> 'btn btn-success btn-md center mt-2',
                                            'name'=> 'submit',
                                            'value'=>'Register Now',
                                            'type'=>'submit'
                                        ); ?>
                                        <center>
                                            <?php echo form_submit($data); ?>
                                        </center>
                                    </div>
                                </div>
                                <?php echo form_close(); ?>
                            </div><!-- container -->
                        </div> <!--card-body-->
                    </div> <!--mx-auto col-sm-6-->
                </div><!--row-->
            </div><!--container py-3-->
        </div><!-- /.container-fluid-->
    </div><!-- /.content-wrapper-->
    <?php include("partials/admin_footer.php") ?>

在這里,我使用form_error()方法為每個輸入字段和文件字段顯示相應的錯誤。 我還使用一種額外的方式來顯示我在這里使用的其他文件上傳錯誤<?php if(isset($error)) echo "<div class='text-danger mb-3'>$error</div>"; ?> <?php if(isset($error)) echo "<div class='text-danger mb-3'>$error</div>"; ?> $error變量來自控制器

這是我的控制器類(Add.php)

        <?php
    /**
     * Created by PhpStorm.
     * User: Anu
     * Date: 8/31/2018
     * Time: 10:07 PM
     */

    class Add extends CI_Controller
    {
         public function __construct()
        {
            parent::__construct();
        }
        public function add_data(){

            $this->form_validation->set_rules('u_name', 'News Title', 'required');
            if (empty($_FILES['userfile']['name']))
            {
                $this->form_validation->set_rules('userfile', 'User Image', 'trim|xss_clean|required');
            }
            $config['upload_path']          = './upload/';
            $config['allowed_types']        = 'gif|jpg|png';
            $config['max_size']             = 2048000;
            // if use helper "upload" don't you need load upload helper here like $this->load->library->('upload',$config)
            // and only you need initialize your configuration rules for your file
            // and $config can be other name it is globle array variable to codeigniter framework
            $this->upload->initialize($config);

            if ($this->form_validation->run() == true) {
                if ( ! $this->upload->do_upload('userfile')) {
                // set file upload error here
                $error = array('error' => $this->upload->display_errors());
                $this->load->view('admin/news_add', $error);
            }
            }else{
                $this->load->view('admin/news_add');
            }
        }
    }

當同時需要輸入字段和文件上傳字段時,這很好用! 但是假設文件上傳字段是可選的,那么如何驗證當前表單? 如果用戶選擇文件,該如何驗證時間呢?

僅在上傳文件時為文件添加驗證。

if (!empty($_FILES) && !empty($_FILES['userfile']['name']))
 {
  $this->form_validation->set_rules('userfile', 'User Image', 'trim|xss_clean|required');
  $config['upload_path']          = './upload/';
  $config['allowed_types']        = 'gif|jpg|png';
  $config['max_size']             = 2048000;
  $this->upload->initialize($config);
  if ($this->form_validation->run() == true) {
     //upload here
  }else{
     // handle error
  }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM