user3813360
user3813360

Reputation: 586

cakePHP validation errors not showing

So basically I have a view action in my users controller where the user can modify his information(username,first name, last name, email..) this form sends to another update action which does the saving stuff, problem is that when I submit the form and one or more fields don't meet the validation rules it doesn't show underneath each fields but the data doesn't save and

$this->User->validationErrors

outputs the errors.

my update action (accessed after submiting the form on view.ctp)

view.ctp:

<?php

        echo $this->Form->create('User', array(
            'inputDefaults' => array(
                'div' => 'form-group',
                'wrapInput' => false,
                'class' => 'form-control'
            ),
            'class' => 'well',
            'url'=>array('controller'=>'users','action'=>'update'),
            'id'=>'info-form'
        ));
        ?>

        <fieldset>
            <legend>Personal Information</legend>

            <?php
            echo $this->Form->input('id', array('value' => $userinfo['User']['id']));
            echo $this->Form->input('User.username', array(
                'label' => 'Username',
                'value'=>$userinfo['User']['username']
            ));
            ?>
            <td><?php echo $this->Form->error('username'); ?></td>
            <?php
            echo $this->Form->input('User.email', array(
                'label' => 'E-mail',
                'value'=>$userinfo['User']['email']
            ));
            ?>
            <?php
            echo $this->Form->input('User.fname', array(
                'label' => 'First name',
                'value'=>$userinfo['User']['fname']
            ));
            ?>
            <?php
            echo $this->Form->input('User.lname', array(
                'label' => 'Last name',
                'value'=>$userinfo['User']['lname']
            ));
            ?>
        <?php
        echo $this->Form->submit('Update', array(
            'div' => 'form-group',
            'class' => 'btn btn-success'
        ));
        ?>
        </fieldset>
        <?php echo $this->Form->end(); ?>

update function:

function update() {
        $this->autoRender = false;
        $this->User->set($this->request->data);

        if ($this->request->is('post')) {
                if ($this->User->save($this->request->data)) {
                    $this->Session->setFlash(__('Information updated Successfuly.'), 'alert', array(
                        'plugin' => 'BoostCake',
                        'class' => 'alert-success'), 'success');
                    return $this->redirect('/users/view/' . $this->request->data['User']['id']);
                } else {
                    // $errors = $this->User->validationErrors; var_dump($errors);die;
                    $this->Session->setFlash(__('An error occured'), 'alert', array(
                        'plugin' => 'BoostCake',
                        'class' => 'alert-danger'), 'danger');
                    return $this->redirect('/users/view/' . $this->request->data['User']['id']);
                }

        } else {
            $this->Session->setFlash(__('Request was not of POST type.'), 'alert', array(
                'plugin' => 'BoostCake',
                'class' => 'alert-danger'), 'danger');
            return $this->redirect('/users/index/');
        }

Upvotes: 0

Views: 1273

Answers (1)

Dave
Dave

Reputation: 29121

It's because you're redirecting after - that will make it lose the validation warnings.

Upvotes: 3

Related Questions