2011-01-12 3 views
15

Mam formularz rejestracyjny i tworzę rekord w obu tabelach użytkownika i tożsamości (A tożsamości hasMany użytkownika)CakePHP jedna forma, wiele modeli, nie wyświetlając komunikaty walidacji jeden model za

forma wygląda to

<?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));?> 

uzyskać wszystkie komunikaty o błędach walidacji dla User.* dziedzinach, ale Identity.* pól są wyświetlane bez komunikatów.

screenshot

zasady walidacji:

Tożsamość:

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.' 
      ) 
     ), 
    ); 

użytkownika: Pola

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.' 
      ) 
     ), 
    ); 

Odpowiedz

12

hasMany modelu powinny być tablicy (w połączeniu z dominującą modelu), patrz .0 dodany pomiędzy nazwami pól

echo $this->Form->input('Identity.0.name'); 
echo $this->Form->input('Identity.0.surname'); 
... 
echo $this->Form->input('Identity.0.email'); 
+2

Czy robisz '$ this-> User-> saveAll ($ data);' w swoim kontrolerze? – Ish

+0

Dzięki, teraz działa dobrze;] Nie wiem, dlaczego zrobił to, co zrobił przez kilka sekund, ale jest w porządku;] – Elwhis

5

Jedna odpowiedź znalazłem na this page dużo o tym samym problemie. Rozwiązaniem było dodanie atrybutu sprawdzania poprawności do zapisania wszystkich

Powiązane problemy