1
我目前正在PHP Yii框架中做一個項目。 我有所謂的文件輸入字段company_logo.For領域我已經在模型[['company_logo'],'file','skipOnEmpty'=>false]
添加以下規則。當我上傳文件的形式,它顯示Yii框架文件輸入skipOnEmpty驗證總是失敗
請上傳文件。
即使我上傳了一個文件。
當我刪除了
skipOnEmpty 它上傳file.I已經研究好幾個地方的issue.But無法找到一個解決方案。
的控制器,視圖和模型在下面給出
視圖 - 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) {
\t
\t $this->params[$layout_param] = $value;
\t
}
?>
<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">
\t <div class="callout callout-info alert-dismissible">
\t <h4><?php if(isset($message['title']))echo $message['title'];?></h4>
\t <p>
\t <?php if(isset($message['body']))echo $message['body'];?>
\t </p>
\t </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">
\t <?= $form->field($model, 'admin_name')->textInput(array('class'=>'form-control')); ?>
</div>
<div class="form-group">
\t
\t <?= $form->field($model, 'admin_email')->textInput(array('class'=>'form-control','type'=>'email')); ?>
</div>
<div class="form-group">
\t <?= $form->field($model, 'admin_phone_number')->textInput(array('class'=>'form-control')); ?>
</div>
<div class="form-group">
\t <?= $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>
控制器 - CompanyController.php
<?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() {
\t \t
\t
\t \t $layout_params=array(
\t \t \t 'username'=>'admin',
\t \t \t 'sidebar_menu1_class' =>'active',
\t \t \t 'sidebar_menu12_class' =>'active',
\t \t \t 'dash_title' => 'Companies',
\t \t \t 'dash_sub_title'=>'Add new company'
\t \t \t
\t \t);
$message = array();
$model = new CompanyModel();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
\t echo "hello";
\t $model->company_logo = UploadedFile::getInstance($model, 'company_logo');
\t echo "world";
if ($model->company_logo && $model->validate()) {
$model->company_logo->saveAs('uploads/' . $model->company_logo->baseName . '.' . $model->company_logo->extension);
} else {
\t echo "Yo Yio ture";
\t exit;
}
$model->add_company();
$message['title'] = 'Wow !';
$message['body'] = 'Successfully added company '.$model->company_name;
}else {
$message = $model->getErrors();
// print_r($message);
// exit;
}
\t return $this->render('add-company', ['model' => $model,
\t 'layout_params'=>$layout_params,
\t 'message' =>$message
]);
//return $this->render('add-company',$data);
}
public function actionSave() {
\t //print_r($_POST);
}
public function actionIndex()
{
\t $data = array(
\t \t 'layout_params'=>array(
\t \t 'username'=>'admin',
\t \t 'sidebar_menu11_class' =>'active'
\t)
\t);//
\t
}
public function actionLogout()
{
Yii::$app->user->logout();
return $this->goHome();
}
}
型號 - CompanyModel.php
<?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() {
\t Yii::$app->db->close();
\t Yii::$app->db->open();
\t $comm = Yii::$app->db->createCommand("CALL create_company('".$this->company_name."','".$this->company_address."','".$this->admin_email."','".$this->admin_phone_number."',1)");
\t return $comm->execute() ;
}
}
也許這會幫助:http://stackoverflow.com/questions/25237661/skip-on-empty-not-working-in-yii2-file-upload? –
這實際上並沒有幫助:( –
因此,您刪除了該過濾器,添加了一個代碼來檢查文件的大小,以便手動檢查文件是否已上傳並且仍然無法工作?是否可以更新您的代碼相應提問並分享結果? –