How to add validation to custom text field

Hi,

How do I validate and show an error on a custom text field?

My form field:




<?= Html::input('text','password1','', $options=['class'=>'form-control', 'maxlength'=>10, 'style'=>'width:350px']) ?>

<?= Html::input('text','password2','', $options=['class'=>'form-control', 'maxlength'=>10, 'style'=>'width:350px']) ?>



My validation:




    $request = Yii::$app->request;

    $password1 = $request->post('password1');

    $password2 = $request->post('password2');

    		

    if($password1 != $password2)

    {

    	//echo "This is an error";

    	//$this->addError('error','Passwords dont match.');

    	$this->addError('request', 'The country must be either.....');

    }



The problem is when setting the addError. How do I do that if the text field is not part of the model?

addError is defined only for models.

try dynamic model

Thanks, but I went with a route explained by another member.

Basically this:

  1. Create a variable in the model.

  2. Do validation and if it fails, set variable to custom error message.

  3. Then in view check if model variable is set.

  4. If variable is set, echo custom error message.