2016-06-14 53 views
0

我有这种形式可以上传图片以及其他信息。在yii框架中上传图片

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

与此控制器:

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'); 
} 

我有这个错误:

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

+1

显示您的模型 –

+0

查看http://www.yiiframework.com/doc-2.0/guide-input-file-upload.html了解如何上载和验证文件的示例。 – MacGyer

回答

1

你已经使用$this->validate。这意味着validate function应该在controller。它是在控制器还是模型? 如果它在Model中,则使用$model->validate()。 $ model是您的Model class的对象。