Model
In building a form interaction it's often best to begin with a model; a place where the data will be sent for checking, and or converting and putting into a source. A model is therefore also required far all CRUD operations.rules and validate
The model below handles an entry form post. It doesn't do much with the data, but it does contain a method with a reserved word, rules(). This method will be called later from the controller using the term validate()The rules method return to the validate call within the controller validation of 2 expectations:
- That the name and emailAddress fields are required.
- That the emailAddress is a valid email field.
<?php
namespace app\models;
use Yii;
use yii\base\Model;
class EntryForm extends Model
{
public $name;
public $emailAddress;
public function rules()
{
return [
[
['name', 'emailAddress'], 'required'
],
[
'emailAddress', 'email'
]
];
}
}
Controller
The controller makes our newly created model available to us through the lineuse app\models\EntryForm;
A method is created called actionEntry() is created which will be called from the form (contained in a view) using the view file name of entry.php
and a new instance of the model is created through the line
$model = new EntryForm();
The POSTed data is sent to the model using the
Yii::$app->request->post() method. A list of other request options can be found at
https://www.yiiframework.com/doc/api/2.0/yii-web-request
At the same time, the model validates the data using
$model->validate() which as mentioned previously makes use of the rules() method.
Once these tests have been passed and you've done something useful with the data, you can render the confirmation view, entry-confirm.php.
If something went wrong you can take the user back to the form view entry.php.
In both cases, though it may seem a little confusing, $model is passed as 'model' to the views. This means that 'model' becomes $model within the views.
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use yii\web\Request;
use app\models\EntryForm;
class SiteController extends Controller
{
// actionIndex(), actionSay() etc.
public function actionEntry()
{
$model = new EntryForm();
$request = new Request();
if ($model->load($request->post()) && $model->validate()) {
// valid data received in $model do something meaningful here
return $this->render('entry-confirm', ['model' => $model]);
} else {
// either the page is initially displayed or there is some validation error
return $this->render('entry', ['model' => $model]);
}
}
}
Views
As mentioned in the controller section above, there are 2 views; entry.php and entry-confirm.php.
entry-confirm.php
Residing at views/site/entry-confirm.php is fairly easy to understand.
<?php
use yii\helpers\Html;
?>
<p>You have entered the following information:</p>
<ul>
<li><label>Name</label>: <?php echo Html::encode($model->name); ?></li>
<li><label>Email</label>: <?php echo Html::encode($model->emailAddress); ?></li>
</ul>
entry.php
Residing at views/site/entry.php requires a little more explanation.The ActiveForm::begin() method creates form tag. This tag has an action field into which the ActiveForm::begin() method the filename of the form. This action field is used to make a request to the actionEntry() method in the controller.
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<?php $form = ActiveForm::begin(); ?>
<?php echo $form->field($model, 'name'); ?>
<?php echo $form->field($model, 'emailAddress'); ?>
<div class="form-group">
<?php echo Html::submitButton('Submit', ['class' => 'btn btn-primary']); ?>
</div>
<?php ActiveForm::end(); ?>