Sanzeeb Aryal
Sanzeeb Aryal

Reputation: 3266

Image uploading in yii framework

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()

Upvotes: 0

Views: 213

Answers (1)

Passionate Coder
Passionate Coder

Reputation: 7294

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

Upvotes: 1

Related Questions