【问题标题】:validation with form & class upload in codeigniter在 codeigniter 中使用表单和类上传进行验证
【发布时间】:2018-04-10 12:23:41
【问题描述】:

我正在 Codeigniter 中测试上传类,我遇到了以下问题:

如果我发送标题、描述和图片,我可以毫无问题地发布(我将数据保存在数据库中)
如果我只发送图片,我也可以毫无问题地发布,但我也需要标题和描述

我的问题:设置文件输入验证的正确方法是什么

  <?php

    class Upload extends CI_Controller {

            public function __construct()
            {
                    parent::__construct();
                    $this->load->helper(array('form', 'url'));
            }

            public function index()
            {
                    $this->load->view('upload_form', array('error' => '' ));
            }

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


                   $this->load->library('upload', $config);
            $this->form_validation->set_rules('titulo','Titulo','trim|required');
            $this->form_validation->set_rules('descripcion','Descripcion','trim|required');
            $this->form_validation->set_rules('userfile','Imagen','trim|required');


                    if ( ! $this->upload->do_upload('userfile') && ($this->form_validation->run() == FALSE))
                    {
                            $error = array('error' => $this->upload->display_errors());

                            $this->load->view('home_view', $error);
                     } else
                     {

                            $data = array('upload_data' => $this->upload->data());

                            $this->articulo_model->create_post();

                            $this->load->view('upload_success', $data);

                    }
            }
    }
    ?>

视图/表单:

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    ?><!DOCTYPE html>
    <html lang="es">
    <head>
        <meta charset="utf-8">
        <title>Welcome to CodeIgniter</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>


    </head>
    <body>

    <div id="containertop" class="wrapper">
    <div class="container">
            <p><a href="<?php echo site_url()?>"> Portada</a></p>

        <div class="alert alert-primary" >
            <p>1.</p>

            <p>2</p>

        </div>
        <div class="alert alert-danger" role="alert">

        </div>
    </div>


    <div class="container"> 
    <?php echo validation_errors() ?>
                <div class="row">

                <div class="col-xs-9 col-md-9 col-lg-9">

                <div class="form-group">

            <?php $attributes = array ('id'=>'login_form','class'=>'form_horizontal'); ?>
                <?php echo form_open_multipart('upload/do_upload');?>


            <?php echo form_label('* Titulo'); ?>


            <?php
            $data = array(

        'class' => 'form-control',
        'name' => 'titulo',
        'placeholder' => 'Titulo'

    )
    ?>
            <?php echo form_input($data);?>
              <small id="input-titulo" class="form-text text-muted">algo asi</small>
      </div>

            <div class="form-group">

    <?php echo form_label('* Descripción'); ?>


            <?php
            $data = array(

        'class' => 'form-control',
        'name' => 'descripcion',
        'placeholder' => 'Descripcion'

    )
    ?>
            <?php echo form_textarea($data);?>

            </div>

    </div>
    <div id="sidebar" class="col-xs-3 col-md-3 col-lg-3">
            Noticias relacionadas
            <?php   

            $data['entries'] = $this->blog_model->getEntries(); 

             $this->load->view('sidebar_view',$data);             ?>


            </div>
                <div class="form-group">
        <label>Adjuntar un archivo</label>
        <input id="userfile" type="file" name="userfile">
      </div>
            <!-- <input type="file" name="userfile" size="20" /> -->
    <br><br>

    <?php
    $data = array(

        'class' => 'btn btn-primary',
        'name' => 'submit',
        'value' => 'Publicar'


    )
    ?>

    <?php echo form_submit($data);?> 

    <br /><br />

    <!-- <input type="submit" value="Subir Todo" />
     -->
    </form>
            <?php echo form_close(); ?>    
       </div>

        </div>  
        </div> 
     </body>
    </html>

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    您只需分开上传和验证。逐个检查错误,这样就可以显示所有错误。

    $upload_errors = FALSE;
    $validation_errors = FALSE;
    
    if( ! $this->upload->do_upload('userfile') )
    {
        $upload_errors = $this->upload->display_errors();
        $this->load->vars( 'upload_errors', $upload_errors );
    }
    
    if( $this->form_validation->run() == FALSE )
    {
        $validation_errors = validation_errors();
        $this->load->vars( 'validation_errors', $validation_errors );
    } 
    
    // No errors
    if( ! $upload_errors && ! $validation_errors )
    {
        // Success!
        $this->load->vars( 'submission_success', TRUE );
    }
    
    // Errors occurred
    else
    {
        // There were errors!
        // You might delete the uploaded file (if it was uploaded).
    }
    

    在您看来,您可以查看是否设置了 $upload_errors$validation_errors

    if( isset( $upload_errors ) OR isset( $validation_errors ) )
    {
        echo '<h3>Submission Errors:</h3>
                <ul>';
    
        if( isset( $upload_errors ) )
            echo $upload_errors;
    
        if( isset( $validation_errors ) )
            echo $validation_errors;
    
        echo '</ul>';
    }
    else if( isset( $submission_success ) )
    {
        echo 'Submission successful...';
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-08
      • 1970-01-01
      • 2016-09-08
      • 2010-12-13
      相关资源
      最近更新 更多