1

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?

user2770039
  • 59
  • 1
  • 2
  • 14
  • can you clarify what you want? do you want the error to be displayed on the right hand side of the text field, or inside the text field, or something else? – deacs Oct 07 '13 at 10:14
  • I want error to be displayed on the text field – user2770039 Oct 07 '13 at 10:37

1 Answers1

2

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.");

Daniel Vaquero
  • 1,315
  • 1
  • 8
  • 13
  • `
    labelEx($model,'current password'); ?> passwordField($model,'password_current',array('size'=>30,'maxlength'=>30)); ?> error($model,'password_current'); ?> addError('password_current', "Message error.");?>
    ` I did this but I get an exception - do not have a method or closure named "addError".
    – user2770039 Oct 07 '13 at 10:38
  • Edited. You are doing this on Controller, not in the model. Replace $this for $model – Daniel Vaquero Oct 07 '13 at 11:10
  • Update your question code, please. o see how you are doing right now. – Daniel Vaquero Oct 07 '13 at 11:51