Я хочу добавить поле загрузки файла в ActiveForm в среде Yii2, но оно не работает, файл не загружен, а имя файла не сохранено в столбце базы данных «company_cover».
Если кто-то может помочь мне найти мою ошибку. Заранее спасибо.
ПРОСМОТР
<div class="block-body collapse" id="company-block">
<?php $form = ActiveForm::begin([
'id' => 'form-change-company',
'method' => 'post',
'action' => ['account/update-company'],
'enableAjaxValidation' => false,
'options' => ['enctype' => 'multipart/form-data'],
]);
?>
<div class="row">
<div class="col-lg-2 col-md-2 col-sm-2 col-xs-12">
<?= $form->field($modelAbout, 'group_id', [
'template' => '{input} {error}',
])->dropDownList([2 => t('app','Enable'), 1 => t('app','Disable')])->label(false);
?>
</div>
</div>
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<?= $form->field($modelCompany, 'store_name', [
'template' => '{input} {error}',
])->textInput([ 'placeholder' => t('app','Store Name'), 'class' => ''])->label(false); ?>
</div>
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<?= $form->field($modelCompany, 'company_name', [
'template' => '{input} {error}',
])->textInput([ 'placeholder' => t('app','Company Name'), 'class' => ''])->label(false); ?>
</div>
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<?= $form->field($modelCompany, 'company_mail', [
'template' => '{input} {error}',
])->textInput([ 'placeholder' => t('app','Company E-Mail'), 'class' => ''])->label(false); ?>
</div>
</div>
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<?= $form->field($modelCompany, 'company_no', [
'template' => '{input} {error}',
])->textInput([ 'placeholder' => t('app','Company No'), 'class' => ''])->label(false); ?>
</div>
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<?= $form->field($modelCompany, 'vat', [
'template' => '{input} {error}',
])->textInput([ 'placeholder' => t('app','VAT'), 'class' => ''])->label(false); ?>
</div>
</div>
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
</div>
</div>
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<?= $form->field($modelCompany, 'company_lat', [
'template' => '{input} {error}',
])->textInput([ 'placeholder' => t('app','Company Latitude'), 'class' => ''])->label(false); ?>
</div>
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<?= $form->field($modelCompany, 'company_lng', [
'template' => '{input} {error}',
])->textInput([ 'placeholder' => t('app','Company Longitude'), 'class' => ''])->label(false); ?>
</div>
</div>
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<?= $form->field($modelCompany, 'file')->fileInput([ 'placeholder' => t('app','Company Cover'), 'class' => '']) ?>
</div>
</div>
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<button type="submit" id="submit-company-info" class="btn-as" value="Submit"><?=t('app','Submit');?></button>
</div>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
МОДЕЛЬ
class CustomerStore extends \app\models\auto\CustomerStore
{
// when inactive model
const STATUS_INACTIVE = 'inactive';
// when active model
const STATUS_ACTIVE = 'active';
// when deactivated
const STATUS_DEACTIVATED = 'deactivated';
public $file;
public function rules()
{
return [
[['store_name', 'company_name', 'company_mail', 'company_lat', 'company_lng', 'company_no', 'vat'], 'trim'],
[['store_name', 'company_name'], 'required'],
[['file'], 'file'],
[['store_name', 'company_name'], 'string', 'max' => 30],
[['company_no', 'vat'], 'string', 'max' => 20],
[['status'], 'safe']
];
}
/**
* @inheritdoc
*/
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors[] = [
'class' => SluggableBehavior::className(),
'value' => [$this, 'getSlug'] //https://github.com/yiisoft/yii2/issues/7773
];
return $behaviors;
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return ArrayHelper::merge(parent::attributeLabels(), [
'store_id' => t('app', 'Store ID'),
'customer_id' => t('app', 'Customer'),
'store_name' => t('app', 'Store Name'),
'company_name' => t('app', 'Company Name'),
'company_mail' => t('app', 'Company E-Mail'),
'company_no' => t('app', 'Company No'),
'vat' => t('app', 'VAT'),
'status' => t('app', 'Status'),
'company_lat' => t('app', 'Company Latitude'),
'company_lng' => t('app', 'Company Longitude'),
'company_cover' => t('app', 'Company Cover'),
'created_at' => t('app', 'Created At'),
'updated_at' => t('app', 'Updated At'),
]);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getCustomer()
{
return $this->hasOne(Customer::className(), ['customer_id' => 'customer_id']);
}
/**
* @return bool
*/
public function deactivate()
{
$this->status = self::STATUS_INACTIVE;
$this->save(false);
return true;
}
/**
* @return bool
*/
public function activate()
{
if($this->status == self::STATUS_DEACTIVATED || $this->status == self::STATUS_INACTIVE) {
$this->status = self::STATUS_ACTIVE;
$this->save(false);
}
return true;
}
/**
* @param $slug
* @return array|null|\yii\db\ActiveRecord
*/
public function findBySlug($slug)
{
return $this->find()->where(array(
'slug' => $slug,
))->one();
}
/**
* @param $event
* @return string
* //https://github.com/yiisoft/yii2/issues/7773
*/
public function getSlug($event)
{
if(!empty($event->sender->slug)) {
return $event->sender->slug;
}
return Inflector::slug($event->sender->store_name);
}
}
КОНТРОЛЛЕР (контроллер довольно длинный, поэтому я просто вставляю деталь для загрузки файла поле)
public function actionUpload()
{
$modelCompany = new CustomerStore();
if (Yii::$app->request->isPost) {
$modelCompany->file = UploadedFile::getInstance($modelCompany, 'file');
if ($modelCompany->validate()) {
$modelCompany->file->saveAs('/' . $modelCompany->file->baseName . '.' . $modelCompany->file->extension);
}
}
return $this->render('upload', ['model' => $modelCompany]);
}