在提交表单之前,一切都很好。当我提交表单时,它在控制器中表单的函数中的特定行给出错误。该行如下:
$model->file->saveAs('uploads/'.$model->file->baseName.'.'.$model->file->extension);
查看文件(signup.php)如下:
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model \frontend\models\SignupForm */
$this->title = 'Signup';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-signup">
<h1><?= Html::encode($this->title) ?></h1>
<p>Please fill out the following fields to signup:</p>
<div class="row">
<div class="col-lg-5">
<?php $form = ActiveForm::begin(['id' => 'form-signup','options'=> ['enctype'=>'multipart/form-data']]); ?>
<?= $form->field($model, 'firstname') ?>
<?= $form->field($model, 'lastname') ?>
<?= $form->field($model, 'username') ?>
<?= $form->field($model, 'email') ?>
<?= $form->field($model, 'photo')->fileInput() ?>
<?= $form->field($model, 'password')->passwordInput() ?>
<div class="form-group">
<?= Html::submitButton('Signup', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
</div>
我在这里只写了那个特定形式的功能。控制器文件(SiteController.php)如下:
namespace frontend\controllers;
use Yii;
use common\models\LoginForm;
use frontend\models\PasswordResetRequestForm;
use frontend\models\ResetPasswordForm;
use frontend\models\SignupForm;
use frontend\models\ContactForm;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use yii\web\UploadedFile;
public function actionSignup()
{
$model = new SignupForm();
if ($model->load(Yii::$app->request->post())) {
$photo = UploadedFile::getInstances($model,'photo');
$model->file->saveAs('uploads/'.$model->file->baseName.'.'.$model->file->extension); **// error is in this line.**
if ($user = $model->signup()) {
if (Yii::$app->getUser()->login($user)) {
return $this->goHome();
}
}
}
return $this->render('signup', [
'model' => $model,
]);
}
模型文件(SignupForm.php)如下:
namespace frontend\models;
use common\models\User;
use yii\base\Model;
use Yii;
/**
* Signup form
*/
class SignupForm extends Model
{
public $username;
public $email;
public $password;
public $firstname;
public $lastname;
public $photo;
/**
* @inheritdoc
*/
public function rules()
{
return [
['username', 'filter', 'filter' => 'trim'],
['username', 'required'],
['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],
['username', 'string', 'min' => 2, 'max' => 255],
['firstname','required'],
['firstname','string','min' => 2, 'max' => 20],
['lastname','required'],
['lastname','string','min' => 2, 'max' => 20],
[['photo'],'file'],
['email', 'filter', 'filter' => 'trim'],
['email', 'required'],
['email', 'email'],
['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'],
['password', 'required'],
['password', 'string', 'min' => 6],
];
}
/**
* Signs user up.
*
* @return User|null the saved model or null if saving fails
*/
public function signup()
{
if ($this->validate()) {
$user = new User();
$user->firstname = $this->firstname;
$user->lastname = $this->lastname;
$user->photo = $this->photo;
$user->username = $this->username;
$user->email = $this->email;
$user->setPassword($this->password);
$user->generateAuthKey();
if ($user->save()) {
return $user;
}
}
return null;
}
}
任何人都可以告诉我究竟是什么问题并给我解决方案。提前谢谢。
答案 0 :(得分:2)
您的先知$model->file
为空。在Yii2中,与Yii 1. *不同,您需要将UploadedFile::getInstances($model,'photo')
设置为$model->file
,而不是保存文件。
$model->file = UploadedFile::getInstances($model,'photo');
$model->file->saveAs('uploads/'.$model->file->baseName.'.'.$model->file->extension);
此外,您拥有属性file
,在您的模型(SignupForm.php)中,您有photo
,您需要更改代码:
$model->photo= UploadedFile::getInstances($model,'photo');
$model->photo->saveAs('uploads/'.$model->photo->baseName.'.'.$model->photo->extension);
答案 1 :(得分:0)
我做到了这样的事情和它的工作。 在 UploadForm.php 模型中:
<?php
namespace app\models;
use yii\base\Model;
use yii\web\UploadedFile;
use Yii;
class UploadForm extends Model
{
public $file;
public $name;
public function rules()
{
return [
[['file'], 'file','skipOnEmpty' => false,'extensions' => 'jpg, png', 'mimeTypes' => 'image/jpeg, image/png'],
];
}
}
在控制器中
use app\models\UploadForm;
use yii\web\UploadedFile;
public function actionProfileImage()
{
$model = new UploadForm();
if (Yii::$app->request->isPost) {
$model->file = UploadedFile::getInstance($model, 'file');
if ($model->validate()) {
$fileName = $this->userData->username . '.jpg';
$image = $model->file->saveAs(Yii::$app->basePath.'/files/profile/' . $fileName);
if($image){
\Yii::$app->getSession()->setFlash('msg', 'Profile Image Updated Successfully.');
return $this->redirect('profile-image', [
'model' => $model,
]);
}
}
}
return $this->render('profileImage');
}
在视图文件中:
<?php $form = ActiveForm::begin(['id' => 'form-profile','options' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($model, 'file')->fileInput(); ?>
<?= Html::submitButton(Yii::t('keywords', 'Submit'), ['class' => 'btn btn-primary', 'name' => 'submit-button']) ?>
<?php ActiveForm::end(); ?>