S
S
Serj_112016-09-08 16:51:42
Yii
Serj_11, 2016-09-08 16:51:42

Error: Call to a member function saveAs() on a non-object?

e0352ac4f69e4a00912304d772abf5bd.jpg7b5a2be7d3c44aae9fd0a8399d33e814.jpg
How to make saveAs() an object?
Please help the teapot!
<?php
File - SiteController.php
namespace app\controllers;
use Yii;
use yii\helpers\Html;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\UploadedFile;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use app\models\MyForm;
class SiteController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout'],
'rules' => [
[
'actions' => ['logout'],
'allow' => true,
'roles ' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
/**
* @inheritdoc
*/
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode '=> YII_ENV_TEST ? 'testme' : null,
],
];
}
/**
* Displays homepage.
*
* return string
*/
public function actionIndex()
{
return $this->render('index');
}
/**
* Login action.
*
* return string
*/
public function actionLogin()
{
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
return $this->goBack();
}
return $this->render('login', [
'model' => $model,
]);
}
/**
* Logout action.
Yii::$app->user->logout();
return $this->goHome();
}
/**
* Displays contact page.
*
* return string
*/
public function actionContact()
{
$model = new ContactForm();
if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
Yii::$app ->session->setFlash('contactFormSubmitted');
return $this->refresh();
}
return $this->render('contact', [
'model' => $model,
]);
*Displays about page.
*
* return string
*/
public function actionAbout()
{
return $this->render('about');
}
public function actionHello($message = 'Hello World!'){
return $this->render('hello', ['message' => $message]);
}
public function actionForm(){
$form = new MyForm();
if($form->load(Yii::$app->request->post()) && $form->validate()){
$name = Html::encode($form->name);
$email = Html::encode($form->email);
$form->file = UploadedFile::getInstance($form, 'file');
$form->file->saveAs('photo/'.$form->file->baseName.$form->file->extension);
}
else {
$name = '';
$email = '';
}
return $this->render('form',
['form' => $form,
'name' => $name,
'email' => $email]);
}
}
File - MyFopm.php
<?php
namespace app\models;
use Yii;
use yii\base\Model;
use yii\web\UploadedFile;
class MyForm extends Model
{
public $name;
public $email;
public $file;
[['name', 'email'], 'required', 'message' => 'You didn't fill in the field!'],
['email', 'email', 'message' => 'Incorrect e-mail address! '],
[['file'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg'],
];
}
File - ContactForm.php
<?php
namespace app\models;
use Yii;
use yii\base\Model;
/**
* ContactForm is the model behind the contact form.
*/
class ContactForm extends Model
{
public $name;
public $email;
public $subject;
public $body;
public $verifyCode;
/**
* returnarray the validation rules.
*/
public function rules()
{
return [
// name, email, subject and body are required
[['name', 'email', 'subject', 'body'], 'required'],
// email has to be a valid email address
['email', 'email'],
// verifyCode needs to be entered correctly
['verifyCode', 'captcha'],
];
}
/**
* return array customized attribute labels
*/
public function attributeLabels()
{
return [
'verifyCode' => 'Verification Code',
];
}
/**
* Sends an email to the specified email address using the information collected by this model.
* @param string $email the target email address
* return boolean whether the model passes validation
*/
public function contact($email)
{
if ($this->validate()) {
Yii::$app->mailer->compose ()
->setTo($email)
->setFrom([$this->email => $this->name])
->setSubject($this->subject)
->setTextBody($this->
return true;
}
return false;
}
}

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
D
Dmitry, 2016-09-08
@Serj_11

Good evening.
Have you made an UploadForm model?
That's just in this model, the image is validated and uploaded, using saveAs () in the upload () method.
In the controller, you just check if the download is successful, do one thing, and if not, then do something else.
An example of how to do this:
downloading files (English)
downloading files (Russian)
ps
Firstly, do you think that you can figure it out in that "footcloth" that you dumped here? Why didn't they use tags for design?
Secondly, did you bother to read the articles I gave links to?
Thirdly, what does ContactForm have to do with it? What does sending messages have to do with downloading a file? Explain.
Fourth, you haven't finished writing MyForm, the model that is responsible for uploading the file. Why are you initializing $name and $email there?
Fifth, the saveAs() public method must be used in MyForm.
As a result, take the trouble to read the documentation, I gave you links, in Russian and bourgeois languages. Once you do that, everything will work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question