15

I have a registration form and I am creating a record in both User and Identity tables (a user hasMany identities)

the form looks like this

<?php echo $this->Form->create('User');?>
    <fieldset>
        <legend><?php __('Register'); ?></legend>
    <?php
        echo $this->Form->input('Identity.name');
        echo $this->Form->input('Identity.surname');
        echo $this->Form->input('User.username');
        echo $this->Form->input('User.pass');
        echo $this->Form->input('User.pass_confirm', array('type' => 'password'));      
        echo $this->Form->input('Identity.email');      
    ?>
    </fieldset>
<?php echo $this->Form->end(__('Submit', true));?>

I get all the validation error messages for User.* fields but Identity.* fields are shown without messages.

screenshot

validation rules:

Identity:

var $validate = array(
        'name' => array(
            'notempty' => array(
                'rule' => 'notempty',              
                'required' => true,
                'message' => 'Your name is required.'
            )
        ),
        'surname' => array(
            'notempty' => array(
                'rule' => 'notempty',              
                'required' => true,
                'message' => 'Your surname is required.'
            )
        ),
        'email' => array(
            'validateEmail' => array(
                'rule' => 'validateEmail',              
                'required' => true,
                'message' => 'The email seems invalid.'
            ),
            'notempty' => array(
                'rule' => 'notempty',
                'message' => 'You have to enter an email address.'
            )
        ),
    );

User:

var $validate = array(
        'pass' => array(
            'required' => array(
                'rule' => array('custom','/^.*[0-9].*$/i'),
                'message'=>'Password must contain numbers'),
            'length' => array(
                'rule' => array(
                    'minLength',8),
                    'message' => 'Password must be at least 8 characters long')
        ),
        'pass_confirm' => array(
            'required' => array(
                'rule' => 'notempty',
                'message' => 'You have to confirm the password'
            ),
            'length' => array( 
                'rule' => 'validatePassword',
                'message'=>'Your passwords don\'t match!' )
        ),
        'username' => array(
            'unique' => array(
                'rule' => 'validateUniqueUsername',
                'message' => 'Username is already taken, please choose a different one.'
            ),
            'notempty' => array(
                'rule' => 'notempty',
                'message' => 'You have to choose a username.'
            )
        ),
    );

2 Answers 2

12

hasMany model's fields should be as array (when combined with parent model), see .0 added between field names

echo $this->Form->input('Identity.0.name');
echo $this->Form->input('Identity.0.surname');
...
echo $this->Form->input('Identity.0.email');
Sign up to request clarification or add additional context in comments.

3 Comments

Are you doing $this->User->saveAll($data); in your controller?
Thanks, now it works fine ;] I don't know why it did what it did for the few seconds, but it's ok finaly ;]
hello @ish hope you are doing well i have an issue can you please see this issue i have posted stackoverflow.com/questions/51120021/…
5

One answer I found was on this page much about the same problem. The solution was to add the validate attribute to saveAll

if($this->Member->saveAll($this->data, array('validate'=>'only'))){
   //your custom save function
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.