Question

I am using an Account Controller which doesnt have its own table but uses User Model.

All works fine except - when I validate any form. It says validation fails (when I try to fail the validation to check) but doesnt throw the error below the field

View

<?php echo $this->Form->input('id'); ?>
              <label for="UserPassword">New Password:</label>
              <?php echo $this->Form->text('password', array('type' => 'password', 'value' => 'harsha')); ?><em>Password must be min 6 characters.</em> <?php echo $form->error('password'); ?>

Controller Action

            if($this->User->saveField('password', $this->data['User']['password'], array('validate' => 'first'))) {
                $this->Session->setFlash('Password has been changed.', 'flash-success');
            } else {
                $this->Session->setFlash('There was some problem with our system. Please try after some time.', 'flash-warning');
            }
Was it helpful?

Solution

Try debug()ing the contents of $this->validationErrors in your view, as well as $this->data in your controller just after a form submission. This should give you a lot more information to work from.

I suspect that your problem is Cake is building form inputs based on the wrong model -- building form fields for Account.id and Account.password instead of User.id and User.password. This is because FormHelper takes its default model from the controller/view it's invoked from, which in your case appears AccountsController.

In order to generate the User.id and User.password fields your controller's submission handling expects, you'll need to prepend User. in your FormHelper calls. Thus:

$this->Form->input('User.id');
$this->Form->text('User.password');

OTHER TIPS

Have you tried:

echo $session->flash();

Note that whatever the manual says, it returns, not echoes. I logged this a while back and it has been changed in the 1.3 manual, but not the 1.2.

Hi you who's asking
If you want to show error-message that return from UserModel's validate
So you can add line code bellow after input form password

  <?php 
      if ($this->Form->isFieldError('password')) { 
       echo $this->Form->error('password', array('class' => 'error')); 
    ?>

and if you want to show error-message that set by method setFlash
you must redirect page and then use $this->Session->flash('flash-name') in page you want to show it

   <?php
    //in UsersController
    $this->Session->setFlash('message here', 'flash-name');
    //in view
    echo $this->Session->flash('flash-name');
   ?>

Good luck!

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