Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Display validation messages of multiple models in cakephp

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 121
    Comment on it

    First need to create a model relationship lets assume that a user registration form and for each and every user has a profile.so there is one to one relationship between user model and profile model.

    <?php
    class User extends AppModel {
        var $name = 'User';
        var $validate = array(
            'name' => array(
                'notempty' => array(
                    'rule' => array('notempty')
                ),
            ),
        );
    
        var $hasOne = array(
            'Profile' => array(
                'className' => 'Profile',
                'foreignKey' => 'user_id',
                'dependent' => false
            )
        );
    
    }
    
    <?php
    class Profile extends AppModel {
        var $name = 'Profile';
        var $validate = array(
            'first&#95;name' => array(
                'notempty' => array(
                    'rule' => array('notempty')
                ),
            ),
            'user&#95;id' => array(
                'numeric' => array(
                    'rule' => array('numeric')
                ),
            ),
        );
    
        var $belongsTo = array(
            'User' => array(
                'className' => 'User',
                'foreignKey' => 'user_id'
            )
        );
    }
    

    Next step is to create a form with all user profile fields.

    <?php echo $this->Form->create('Profile');?>
        <?php
            echo $this->Form->input('User.name');
            echo $this->Form->input('first_name');     
        ?>
    <?php echo $this->Form->end(__('Submit', true));?>
    

    Now Next is to use SaveAll() function to save the profile data.As we are working with associated models we will use SaveAll()

    function add() {
            if (!empty($this->data)) {
                $this->Profile->create();
    
                unset($this->Profile->validate['user_id']);
                if ($this->Profile->saveAll($this->data)) {
                    $this->Session->setFlash(__('The profile has been saved', true));
                    $this->redirect(array('action' => 'index'));
                } else {
                    $this->Session->setFlash(__('The profile could not be saved. Please, try again.', true));
                }
            }      
        }
    

    So Now the form will automatically show the error for both user and profile fields if you try to create profile with empty data.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: