$form = $this->beginWidget('CActiveForm', array( 'id'=>'my-form', )); // Rules for validation $model = new MyModel(); $rules = $model->rules(); if(isset($_POST['MyModel'])) { // Populate the model with data from user $model->attributes = $_POST['MyModel']; // Validate the user input against the rules if($model->validate()) { // Input is valid } else { // Input is invalid } }
$form = $this->beginWidget('CActiveForm', array( 'id'=>'my-form', )); // Form fields echo $form->textField($model,'username'); // Validation rules $rules = array( array('username', 'required'), array('username', 'length', 'min'=>3, 'max'=>30), ); // Submit button echo CHtml::submitButton('Submit'); // Validate the form if(isset($_POST['MyModel'])) { $model->attributes = $_POST['MyModel']; if($model->validate()) { // Input is valid } else { // Input is invalid } }In this example, we create a text input field using `$form->textField()` and set the validation rules directly in an array. We then provide a submit button using `$html->submitButton()` and check for validation when the form is submitted. The `CActiveForm` class is part of the Yii Framework's `yii\web\helpers` package.