Why do you have $name = 'UserInfos' while your class is UserInfo (and also 
'Users')? Try to make it by Cake conventions and it should validate all 
associeated models on saveAll().

I would drop those lines, as they are not needed. And try changing UsrInfos 
to UserInfo in form generation.

Also:

 $this->User->create();
*// not needed:*   $this->loadModel('UserInfo');

UserInfo is available in $this->User->UserInfo (whith your current belongs 
to in $this->User->UserInfo).

And:


  'UsersInfos' => array( // why not just UserInfo ?
   'className' => 'users_infos', //Class name should be: UserInfo

Also read about associations in CakePHP, your foreign keys should be 
user_id (not users_id). It's easier to go by Cake's conventions.



On Tuesday, December 11, 2012 1:08:38 AM UTC+1, Robert Hunt wrote:
>
> Hi All,
>
> I hope someone can help clear this up for me.
>
> Basically I have two models producing fields in a form but when saving it 
> only validates the parent model.
>
> I have attempted to use saveAssociated and I have tried validating the 
> data on a per model basis with no success.
>
> Here is my code:
>
> *User Model*
>
>> class User extends AppModel {
>>  public $name = 'Users';
>>  var $hasOne = array(
>>   'UsersInfos' => array(
>>    'className' => 'users_infos',
>>    'foreignKey' => 'users_id'
>>   )
>>  );
>>  var $hasMany = array(
>>   'UsersIps' => array(
>>    'className' => 'users_ips',
>>    'foreignKey' => 'users_id'
>>   ),
>>   'UsersStats' => array(
>>    'className' => 'users_stats',
>>    'foreignKey' => 'users_id'
>>   ),
>>   'UsersSkills' => array(
>>    'className' => 'users_skills',
>>    'foreignKey' => 'users_id'
>>   ),
>>   'UsersBuildings' => array(
>>    'className' => 'users_buildings',
>>    'foreignKey' => 'users_id'
>>   ),
>>   'UsersItems' => array(
>>    'className' => 'users_items',
>>    'foreignKey' => 'users_id'
>>   ),
>>   'UsersMinerals' => array(
>>    'className' => 'users_minerals',
>>    'foreignKey' => 'users_id'
>>   )
>>  );
>>  
>>  public $validate = array(
>>   'username' => array(
>>    'required' => array(
>>     'rule' => array('notEmpty'),
>>     'message' => 'Please enter a Username'
>>    ),
>>    'isUnique' => array(
>>     'rule' => array('isUnique'),
>> 'on' => array('create'),
>> 'message' => 'This Username is already taken, please choose another'
>>    )
>>   ),
>>   'password' => array(
>>    'required' => array(
>>     'rule' => array('notEmpty'),
>>     'message' => 'Please enter a Password'
>>    ),
>>    'minLength' => array(
>>     'rule' => array('minLength', '6'),
>>     'message' => 'Your Password must be a minimum of 6 characters long'
>>    )
>>   )
>>  );
>
>
> *UserInfo Model *
>
>> class UserInfo extends AppModel {
>>  public $name = 'UsersInfos';
>>  public $useTable = 'users_infos';
>>  
>>  public $belongsTo = array(
>>   'Users' => array(
>>    'className' => 'Users',
>>    'foreignKey' => 'users_id'
>>   )
>>  );
>>  
>>  public $validate = array(
>>   'first_name' => array(
>>    'required' => array(
>>     'rule' => array('notEmpty'),
>>     'message' => 'Please enter your First Name'
>>    )
>>   ),
>>   'last_name' => array(
>>    'required' => array(
>>     'rule' => array('notEmpty'),
>>     'message' => 'Please enter your Last Name'
>>    )
>>   ),
>>   'email' => array(
>>    'required' => array(
>>     'rule' => array('notEmpty'),
>>     'message' => 'Please enter your Email'
>>    ),
>>    'email' => array(
>>     'rule' => array('email'),
>> 'message' => 'Please enter a valid Email'
>>    ),
>>    'isUnique' => array(
>>     'rule' => array('isUnique'),
>> 'on' => array('create'),
>> 'message' => 'This Email is already taken, duplicate accounts are not 
>> allowed'
>>    )
>>   ),
>>   'age' => array(
>>    'required' => array(
>>     'rule' => array('notEmpty'),
>>     'message' => 'Please select your Age'
>>    )
>>   ),
>>   'sex' => array(
>>    'required' => array(
>>     'rule' => array('notEmpty'),
>>     'message' => 'Please select your Sex'
>>    )
>>   ),
>>   'location' => array(
>>    'required' => array(
>>     'rule' => array('notEmpty'),
>>     'message' => 'Please enter your General Location'
>>    )
>>   ),
>>   'race_id' => array(
>>    'required' => array(
>>     'rule' => array('notEmpty'),
>>     'message' => 'Please select your Race'
>>    )
>>   )
>>  );
>
>
> *Create Account Form*
>
>>  echo $this->Session->flash();
>>    echo $this->Form->create('User');
>>    echo $this->Form->input('User.username', array(
>>      'label' => 'User:<br>'
>>     )
>>    );
>>    echo $this->Form->input('User.password', array(
>>      'label' => 'Pass:<br>' 
>>     )
>>    );
>>    echo $this->Form->input('UsersInfos.first_name', array(
>>      'label' => 'First Name:<br>'
>>     )
>>    );
>>    echo $this->Form->input('UsersInfos.last_name', array(
>>      'label' => 'Last Name:<br>'
>>     )
>>    );
>>    echo $this->Form->input('UsersInfos.email', array(
>>      'label' => 'Email:<br>'
>>     )
>>    ); 
>>    echo $this->Form->input('UsersInfos.age', array(
>>      'type' => 'select',
>>  'options' => array_combine(range(14,100), range(14,100)),
>>  'label' => 'Select Your Age:<br>',
>>  'empty' => 'Your Age'
>> )
>>    );
>>    echo $this->Form->input('UsersInfos.sex', array(
>>      'type' => 'select',
>>  'options' => array(
>>   'm' => 'Male',
>>   's' => 'Female'
>>  ),
>>  'label' => 'Select Your Sex:<br>',
>>  'empty' => 'Your Sex'
>>     )
>>    );
>>    echo $this->Form->input('UsersInfos.location', array(
>>      'label' => 'Location:<br>'
>>     )
>>    );
>>    // TODO : generate $raceIDs variable using race table
>>    $raceIDs = array(0 => 'Terran', 1 => 'Nexus');
>>    echo $this->Form->input('UsersInfos.race_id', array(
>>      'type' => 'select',
>>  'options' => $raceIDs,
>>  'label' => 'Choose Your In-Game Race:<br>',
>>  'empty' => 'Choose Your Race'
>> )
>>    );
>>    echo $this->Form->submit('Go!', array(
>>      'class' => 'btn'
>>     )
>>    ); 
>>    echo $this->Form->end();
>
>
> *Create Account Action*
>
>>  public function createAccount() {
>>   if ($this->request->is('post') || $this->request->is('put')) {
>>    $this->User->create();
>>    $this->loadModel('UserInfo');
>>    $dbo = $this->User->getDataSource();
>>    $now = $dbo->expression('NOW()');
>>    $this->request->data['UsersInfos']['date_created'] = $now ;
>>    $this->request->data['UsersInfos']['date_modified'] = $now;
>>    $this->request->data['UsersInfos']['register_ip'] = 
>> $this->request->clientIp();
>>    
>>    debug($this->request->data);
>>    
>>    if ($this->User->saveAll($this->request->data, array('validate' => 
>> 'first')))
>>     $this->Session->setFlash(__('Your account was successfully 
>> created!'));
>>    else
>>     $this->Session->setFlash(__('Your account could not be created, 
>> please check your entries and try again'));
>>   }
>>  }
>
>
> *The data returned*
>
> *\app\Controller\UsersController.php* (line *53*)
> array(
> 'User' => array(
> 'password' => '*****',
> 'username' => 'mrenigma'
> ),
> 'UsersInfos' => array(
> 'first_name' => '',
> 'last_name' => '',
> 'email' => '',
> 'age' => '',
> 'sex' => '',
> 'location' => '',
> 'race_id' => '',
> 'date_created' => object(stdClass) {
> type => 'expression'
> value => 'NOW()'
> },
> 'date_modified' => object(stdClass) {
> type => 'expression'
> value => 'NOW()'
> },
> 'register_ip' => '127.0.0.1'
> )
> )
>
>  Any help would be much appreciated with this, if you need any more 
> information then please let me know and I will do my best to provide it.
>

-- 
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

--- 
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
Visit this group at http://groups.google.com/group/cake-php?hl=en.


Reply via email to