我希望此表单能够上传图片以及其他信息。
<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');
}
我收到了这个错误:
调用未知方法:app \ controllers \ PracController :: validate()
答案 0 :(得分:1)
您使用过validate function
。这意味着controller
应该在$model->validate()
中。是控制器还是型号?
如果它在模型中,则使用Model class
。其中$ model是{{1}}的对象。