Yii Framework文件输入skipOnEmpty验证总是失败

时间:2015-04-25 07:58:28

标签: php yii

我目前正在使用PHP Yii框架进行项目。 我有一个带有名为company_logo的文件输入字段的表单。对于该字段,我在模型[['company_logo'],'file','skipOnEmpty'=>false]中添加了以下规则 当我上传文件时,它会显示

  

请上传文件。

即使我上传了一个文件。

当我删除

  

skipOnEmpty   它正在上传文件。我已经研究了几个问题的地方。但是找不到解决方案。

控制器,视图和模型如下所示

  

查看 - add_company.php



<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;

    /*Assigning the parameters to be accessible by layouts*/
    
  foreach($layout_params as $layout_param => $value) {
   	  
	  $this->params[$layout_param] = $value;
	 
   }
   ?>
    
    <div class="form-group">
         
    </div>

<div class="col-md-12">
<div class="box box-primary">
                <div class="box-header">
                  <h3 class="box-title">Add Company</h3>
                </div><!-- /.box-header -->
                <!-- form start -->
 

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>


                  <div class="box-body">
                  <?php if(isset($message)&&sizeof($message)): ?>
                    <div class="form-group">
                    	<div class="callout callout-info  alert-dismissible">
	                    <h4><?php if(isset($message['title']))echo $message['title'];?></h4>
	                    <p>
	                        <?php if(isset($message['body']))echo $message['body'];?>
	                    </p>
	                   </div>
                    </div>
                    <?php endif;?>

                    <div class="form-group"> 
                      <?= $form->field($model, 'company_name')->textInput(array('class'=>'form-control')); ?>
                    </div>
                    <div class="form-group">
                    
                      <?= $form->field($model, 'company_address')->textArea(array('class'=>'form-control')); ?>
                      
                     </textarea> 
                     <div class="form-group"> 
                      <?= $form->field($model, 'company_logo')->fileInput(array('class'=>'form-control')); ?>
                    </div>
 

                    <div class="form-group">
	                     <?= $form->field($model, 'admin_name')->textInput(array('class'=>'form-control')); ?>
                    </div>
                    <div class="form-group">
                    	
	                     <?= $form->field($model, 'admin_email')->textInput(array('class'=>'form-control','type'=>'email')); ?>
                     </div>

                    <div class="form-group">
                     	<?= $form->field($model, 'admin_phone_number')->textInput(array('class'=>'form-control')); ?>
                                         </div>
                    <div class="form-group">
                    
                     	<?= $form->field($model, 'admin_password')->passwordInput(array('class'=>'form-control')); ?>
                    </div>
                    <div class="form-group">
                       <?= $form->field($model, 'retype_admin_password')->passwordInput(array('class'=>'form-control')); ?>
                    </div>
                    

                  <div class="box-footer">
                    
        <?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
                  </div>
                  </div><!-- /.box-body -->
<?php ActiveForm::end(); ?>                
              </div>
</div>
&#13;
&#13;
&#13;

  

Controller - CompanyController.php

&#13;
&#13;
<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter; 
use app\models\CompanyModel;
use yii\web\UploadedFile;

global $username;
class CompanyController extends Controller
{ 
     
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout'],
                'rules' => [
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    } 
    
     public function actionEntry()
    {
    }
    
    public function actionAdd() {
	           	 
    	
    		$layout_params=array(
    			'username'=>'admin',
    			'sidebar_menu1_class' =>'active',
    			'sidebar_menu12_class' =>'active',
    			'dash_title' => 'Companies',
    			'dash_sub_title'=>'Add new company'
    			
    		);
        $message = array();
        $model = new CompanyModel();
        
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
        	 echo "hello";
        	 $model->company_logo = UploadedFile::getInstance($model, 'company_logo');
        	 echo "world";
            if ($model->company_logo && $model->validate()) {                
                $model->company_logo->saveAs('uploads/' . $model->company_logo->baseName . '.' . $model->company_logo->extension);
            } else {
	         echo "Yo Yio ture";   
	         exit;
            }

             $model->add_company();
             $message['title'] = 'Wow !';
             $message['body'] = 'Successfully added company '.$model->company_name;
         }else {
                                        $message = $model->getErrors(); 
                                     //   print_r( $message );
                                     //   exit;
         }          
    	return $this->render('add-company', ['model' => $model,
            	'layout_params'=>$layout_params,
            	'message' =>$message
        ]);
        //return $this->render('add-company',$data);
    }
    public function actionSave() {
	    //print_r($_POST);
    }
    public function actionIndex()
    {         
    	$data = array(
    		'layout_params'=>array(
    		'username'=>'admin',
    		'sidebar_menu11_class' =>'active'
    	)
    	);//
	   
  
         
    }
     public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }
 }
&#13;
&#13;
&#13;

模型 - CompanyModel.php

&#13;
&#13;
<?php

namespace app\models;
use yii;
use yii\db;
use yii\base\Model;
use yii\web\UploadedFile;

class CompanyModel extends Model
{
    public $company_name;
    public $company_address;
    public  $company_logo;
    public $admin_email;
    public $admin_name;
    public $admin_password;
    public $retype_admin_password;
    public $admin_phone_number; 
    
    public function rules()
    {
        return [
            
            [['company_name'], 'required'],
            [['company_address'],'required'], 
            [['admin_name'],'required'],
            [['admin_email'],'required'],
            [['admin_password'],'required'],
            [['retype_admin_password'],'required'],
            [['admin_phone_number'],'required'],
            [['company_logo'],'file','skipOnEmpty'=>false]
        ];
    }
    public function add_company() {
    	 Yii::$app->db->close();
    	 Yii::$app->db->open();
	     $comm =  Yii::$app->db->createCommand("CALL create_company('".$this->company_name."','".$this->company_address."','".$this->admin_email."','".$this->admin_phone_number."',1)");
	     return $comm->execute() ;
    }

}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:-2)

&#13;
&#13;
<?php

namespace app\models;
use yii;
use yii\db;
use yii\base\Model;
use yii\web\UploadedFile;

class CompanyModel extends Model
{
    public $company_name;
    public $company_address;
    public  $company_logo;
    public $admin_email;
    public $admin_name;
    public $admin_password;
    public $retype_admin_password;
    public $admin_phone_number; 
    
    public function rules()
    {
        return [
            
            [['company_name'], 'required'],
            [['company_address'],'required'], 
            [['admin_name'],'required'],
            [['admin_email'],'required'],
            [['admin_password'],'required'],
            [['retype_admin_password'],'required'],
            [['admin_phone_number'],'required'],
            [['company_logo'],'file','skipOnEmpty'=>false],//here the comma is missing
        ];
    }
    public function add_company() {
    	 Yii::$app->db->close();
    	 Yii::$app->db->open();
	     $comm =  Yii::$app->db->createCommand("CALL create_company('".$this->company_name."','".$this->company_address."','".$this->admin_email."','".$this->admin_phone_number."',1)");
	     return $comm->execute() ;
    }

}
&#13;
&#13;
&#13;