با سلام و خسته نباشید
من میخوام دو مدل رو برای یک فرم ارسال کنم ، اما این ارور رو میده
PHP Fatal Error – yii\base\ErrorException
Call to a member function formName() on null
Model Gullery
model FormUpload
Controller
Form
من میخوام دو مدل رو برای یک فرم ارسال کنم ، اما این ارور رو میده
PHP Fatal Error – yii\base\ErrorException
Call to a member function formName() on null
Model Gullery
<?php namespace app\models; use yii\base\Model; use Yii; /** * This is the model class for table "{{%gullery}}". * * @property integer $pictureid * @property string $pictureaddress * @property string $picturetitle */ class Gullery extends \yii\db\ActiveRecord { /** * @inheritdoc */ public static function tableName() { return '{{%gullery}}'; } /** * @inheritdoc */ public function rules() { return [ [['pictureaddress', 'picturetitle'], 'required'], [['picturetitle','pictureaddress'], 'string', 'max' => 300], ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'pictureid' => 'Pictureid', 'pictureaddress' => 'Pictureaddress', 'picturetitle' => 'Picturetitle', ]; } }
model FormUpload
<?php namespace app\models; use yii\base\Model; use yii\web\UploadedFile; use Yii; class UploadForm extends Model { /** * @var UploadedFile */ public $imageFile; public function rules() { return [ [['imageFile'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg'], ]; } public function upload() { if ($this->validate()) { $this->imageFile->saveAs('uploads/' . $this->imageFile->baseName . '.' . $this->imageFile->extension); return true; } else { return false; } } }
Controller
public function actionCreate() { $model = new Gullery(); $picturemodel=new UploadForm(); $picturemodel->imageFile = UploadedFile::getInstance($picturemodel, 'imageFile'); $postData = Yii::$app->request->post(); if ($model->load($postData) && $picturemodel->load($postData) && $model->save() && $picturemodel->upload() && Model::validateMultiple([$model, $picturemodel])) { return $this->redirect(['view', 'id' => $model->pictureid]); } else { return $this->render('create', [ 'model' => $model, 'picturemodel'=>$picturemodel ]); } }
Form
<?php use yii\helpers\Html; use yii\widgets\ActiveForm; use yii\helpers\ArrayHelper; /* @var $this yii\web\View */ /* @var $model app\models\Gullery */ /* @var $form yii\widgets\ActiveForm */ ?> <div class="gullery-form"> <?php $form = ActiveForm::begin([ 'id' =>'foem', ]); ?> <?= $form->field($picturemodel, 'imageFile')->fileInput() ?> <?= $form->field($model, 'picturetitle')->textInput(['maxlength' => true]) ?> <?= $form->field($model, 'pictureaddress')->textInput(['maxlength' => true]) ?> <div class="form-group"> <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> </div> <?php ActiveForm::end(); ?> </div>