php - Yii Framework file input skipOnEmpty validation always fails -
i doing project in php yii framework. have form file input field called company_logo.for field have added following rule in model [['company_logo'],'file','skiponempty'=>false]
when upload file, shows
please upload file.
even if uploaded file.
when remove
skiponempty uploading file.i have researched several places issue.but couldn't find solution.
the controller, view , model given below
view - add_company.php
<?php use yii\helpers\html; use yii\widgets\activeform; /*assigning parameters accessible layouts*/ foreach($layout_params $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>
controller - 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() { $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(); } }
model - 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() { 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() ; } }
<?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 comma 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() ; } }
Comments
Post a Comment