PHP code example of flaviovs / yii2-form

1. Go to this page and download the library: Download flaviovs/yii2-form library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

flaviovs / yii2-form example snippets


	class MyForm extends fv\yii\form\Model
	{
		/** @var \app\models\Post */
		public $post;

		/** @var \app\models\Comment */
		public $comment;
	}
	

	protected function modelAttributes()
	{
		return ['post', 'comment'];
	}
	

    protected function rules()
    {
        return [
            [$this->modelAttributes(), 'safe'];
        ];
    }
    

	/** @var \fv\yii\form\Model $model */

	$form = ActiveForm::begin();

	echo $form->field($model->post, 'title');
	echo $form->field($model->comment, 'body');

	ActiveForm::End();
	

	$model = new MyForm([
		'post' => new Post(),
		'comment' => new Comment(),
	]);
	if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
		$model->post->save();
		$model->comment->save();
	}