0

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/');
        }

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

How am I supposed to do this then?
@user3813360 - That's an all-together different question. Feel free to ask it here on StackOverflow, and I'm sure you'll get some great answers.
Give this man a medal. i was looking for this solution so desperately. everythings working so perfect now. greatly thanx!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.