Validation Errors Not Showing In Activeform

I’ve created action with CActiveForm, validation works fine, but in form errors don’t displayed.

My code:

Model:




class Account extends CActiveRecord {

 ...

  public function rules() {

    array('old_password, new_password, password_repeat', 'required'),

    array('new_password', 'compare', 'compareAttribute' => 'password_repeat'),

  }

 ...

}



View:




<div class="item">

  <?php var_dump($model->getErrors()); // #1 ?>

  <?php $form = $this->beginWidget('CActiveForm'); ?>

  <?php echo $form->errorSummary($model); ?>


  <div class="row">

    <?php echo $form->label($model, 'old_password'); ?>

    <?php echo $form->textField($model, 'old_password') ?>

    <?php echo $form->error($model, 'old_password') ?>

  </div>


  <div class="row">

    <?php echo $form->label($model, 'new_password'); ?>

    <?php echo $form->passwordField($model, 'new_password') ?>

    <?php echo $form->error($model, 'new_password') ?>

  </div>


  <div class="row">

    <?php echo $form->label($model, 'password_repeat'); ?>

    <?php echo $form->passwordField($model, 'password_repeat') ?>

    <?php echo $form->error($model, 'password_repeat') ?>

  </div>


  <div class="row submit">

    <?php echo CHtml::submitButton('Edit'); ?>

  </div>

  <?php $this->endWidget(); ?>

</div>



Controller:




public function actionChangepassword() {

    $model = Account::model()->findByPk(Yii::app()->user->id);


    if (isset($_POST['ajax']) && $_POST['ajax'] === 'Account') {

      echo CActiveForm::validate($model);

      Yii::app()->end();

    }

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

      $model->attributes = $_POST['Account'];

      if ($model->validate()) {

        $model->save();

      }

    }

    var_dump($model->getErrors()); // #2

    $this->render('change-password', array(

      'model' => $model,

    ));

  }



In #1 line prints empty array, but in #2 I see errors.

P.S.: In other pages validation and errors displaying works fine.

Is the view you posted called ‘change-password’? I mean does the controller renders it directly? Because there’s no way errors would be cleared after you call getErrors() in the controller. Is the form filled with posted values?

Hmmmm, if I print fields in Controller, fields are filled, but in View it empty.




public function actionChangepassword() {

....

var_dump($model->old_password);

var_dump($model->new_password);

var_dump($model->password_repeat);

$this->render('change-password', array(

      'model' => $model,

    ));

  }




<?php

d($model->getErrors());

var_dump($model->password);

var_dump($model->old_password);

var_dump($model->new_password);

var_dump($model->password_repeat);

?>

<div class="item">

  <?php $form = $this->beginWidget('CActiveForm'); ?>

....

<?php $this->endWidget(); ?>



Somebody can help me? :unsure:

You did not respond to nineinchnick, as he wrote there is no way that if all is right a variable would loose it’s values from the controller to the view… there must be something in the middle that resets the values

Yes.

I trying:




public function actionChangepassword() {

    $model = $this->account_data;

    $model->scenario = 'edit-password';

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

      $model->attributes = $_POST['GeneralAccount'];

      $model->validate();

      $model->addError('old_password', 'test');

      $errors = $model->errors;

    };

    $this->render('change-password', array(

      'model' => $model,

      'errors' => $errors,

    ));

  }

But It did not help. In View $errors is Null (If I print $errors in Controller I see errors)

Update:

Oh, I trying remove in my rules scenario (on property) and form works fine:




//      array('old_password, new_password, password_repeat', 'required', 'on' => 'edit-password'),

//      array('new_password', 'compare', 'compareAttribute' => 'password_repeat', 'on' => 'edit-password'),

      array('old_password, new_password, password_repeat', 'required'),

      array('new_password', 'compare', 'compareAttribute' => 'password_repeat'),



But I need scenario in it.

Update2:

setScenario not fix it

Update3:

I trying use scenario with other model and all works fine. Also I trying


$model = new GeneralAccount('editpassword');

it did not help

Hi,

Can you create a not working sample app with this model-view-controller and upload to a post as attachment? I want to test it on my localhost. I have no idea, but maybe a framework-debug helps.

What is your yii version?

Problem solved