good afternoon my project has a client model, a clientController driver and a form guide me in the documentation of Yii2 but I do not give with the problem since it saves all the fields but I do not load the image in the route that I indicated , the REquest post is not generated.
namespace app\models;
use Yii;
class Client extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public $image;
public static function tableName()
{
return 'client';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['name', 'country'], 'required'],
[['name', 'country'], 'string', 'max' => 45],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'idclient' => 'Codigo Cliente',
'name' => 'Nombre',
'country' => 'Pais',
'image' => 'Imagen',
];
}
public function upload()
{
if ($this->validate()) {
$this->image->saveAs('uploads/' . $this->image->baseName . '.' .
$this->image->extension);
return true;
} else {
return false;
}
}
}
my controller
public function actionUpload(){
$model = new Client();
if ($model->load(Yii::$app->request->post())) {
$model->image = UploadedFile::getInstance($model, 'image');
if($model->validate()) {
if ($model->upload() !== false) {
$model->save();
}
return $this->redirect(['view', 'id' => $model->id]);
}
}
return $this->render('create', [
'model' => $model,
]);
}
}
my Form
<?php $form = ActiveForm::begin([
"method" => "post",
"enableClientValidation" => true,
"options" => ["enctype" => "multipart/form-data"],
]);
?>
<?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'country')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'image')->fileInput()?>
<div class="form-group">
<?= Html::submitButton('Guardar', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
the route
C: \ xampp \ htdocs \ basic \ uploads
POST request value
that is the request file