Question

I'm new to Yii framework and I need to display the validation error message as in login form "Username cannot be blank". Now, I have a text field where I updated the fields and the during validation I want a message to be displayed. How can I do this?

Controller

public function actionUpdate($id)
    {
        $model = $this->loadModel($id);

    // set the parameters for the bizRule
    $params = array('GroupzSupport'=>$model);
    // now check the bizrule for this user
    if (!Yii::app()->user->checkAccess('updateSelf', $params) &&
        !Yii::app()->user->checkAccess('admin'))
    {
        throw new CHttpException(403, 'You are not authorized to perform this action');
    }
      else
    {

       if(isset($_POST['GroupzSupport']))
        {                        

           $password_current=$_POST['GroupzSupport']['password_current'];   
           $pass=$model->validatePassword($password_current);

            $model->attributes=$_POST['GroupzSupport'];
                        if($pass==1)
                        {
                        $model->password = $model->hashPassword($_POST['GroupzSupport']['password_new']);
            if($model->save())
                $this->redirect(array('/messageTemplate/admin'));
                        }
                        else {$errors="Incorrect Current password"; print '<span style="color:red"><b>';
print '</b><b>'.$errors;
print '</b></span>';}
        }

        $this->render('update',array(
            'model'=>$model,
        ));
    }
    }

View

<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'password-recovery-reset-password-form',
    'enableAjaxValidation'=>false,
)); ?>


    <div class="row"><?php 
        echo $form->labelEx($model,'username'); 
        echo $form->textField($model,'username',array('size'=>45,'maxlength'=>150)); 
        echo $form->error($model,'username'); 
    ?></div>


<div class="row">
        <?php echo $form->labelEx($model,'current password'); ?>
        <?php echo $form->passwordField($model,'password_current',array('size'=>30,'maxlength'=>30)); ?>
        <?php echo $form->error($model,'password_current'); ?>
    </div>

        <div class="row">
        <?php echo $form->labelEx($model,'new password'); ?>
        <?php echo $form->textField($model,'password_new',array('size'=>30,'maxlength'=>30)); ?>
        <?php echo $form->error($model,'password_new'); ?>
    </div>

        <div class="row">
        <?php echo $form->labelEx($model,'confirm new password'); ?>
        <?php echo $form->passwordField($model,'password_repeat',array('size'=>30,'maxlength'=>30)); ?>
        <?php echo $form->error($model,'password_repeat'); ?>
    </div>


    <div class="row buttons"><?php 
        echo CHtml::submitButton('Reset Your Password');
        ?></div><?php

$this->endWidget(); ?>
</div>

Now currently I'm displaying it at the top. enter image description here

I want to display it right on the textfield as in login page. enter image description here How can I do this?

Was it helpful?

Solution

Before redirect, add the message to the desired field.

In the model Validator:

$this->addError('field_name', "Message error.");

Or in Controller action:

$model->addError('field_name', "Message error.");

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top