php - Image uploading in yii framework

251

I have this form in view to upload image along with other information.

<h2>Enter your details</h2><br>
<form method="POST" action="<?php echo Yii::$app->request->baseUrl;?>/prac/add-now" role="form">
    <label>Upload your photo:<br></label><input type="file" name="image" ><br><br>
    <input type="name" name="name" id="name" placeholder="Name" required><br><br>
    <input type="text" name="mobile" placeholder="Mobile number" >  <br><br>

    <input type="email" name="email" placeholder="Email"><br><br>
    <button type="submit" class="btn btn-default">Add</button><BR><BR>
</form>

and this controller:

public function actionAddNow()
{
    $request = Yii::$app->request;
    $session = Yii::$app->session;
    $session->setFlash('message', 'You have successfully added the details.');
    $name = $request->post('name');
    $email = $request->post('email');
    $mobile = $request->post('mobile');

    $image=$request->post('image');

    if ($this->validate()) {
        $this->image->saveAs('/' . $this->image->baseName . '.' . $this->imageFile->extension);
        return true;
    } else {
        return false;
    }

    $add=new prac();
    $add->Name=$name;
    $add->Email=$email;
    $add->Contact=$mobile;  
    $add->Image=$image; 
    $add->save();

    return $this->redirect(Yii::$app->request->baseUrl.'/prac/index');
}

I have got this error:

Calling unknown method: app\controllers\PracController::validate()

486

Answer

Solution:

you have used$this->validate. That meansvalidate function should be incontroller. Is it in Controller or Model? if it is in Model then use$model->validate(). where $model is a object of yourModel class.

People are also looking for solutions to the problem: php - Queries of checkbox on octobercms/laravel

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.