I've been experimenting with advanced validation per
http://wiki.cakephp.org/tutorials:advanced_validation:advance_validation_with_parameters
and the ErrorHelper  class.

I need my UsersController's update() method to update both the
underlying 'users' table as well as the 'people'  table that the User
model belongsTo. I've discovered that when you just say

   $this->User->save($this->data['User'])

cake updates 'users'  table but not 'people'. So I instantiate my own
Person object in the UsersController constructor, and assign it to an
instance var, which I update it "manually" (it's still pretty easy,
thank you Cake) in my update().

But I don't want to update anything unless both models validate. So I
check that manually before calling save(). Fine. The only hitch is that
in my view update.thtml, I have not been able to figure out to get
e.g., $error->showMessage('Person/lastname') to show the validation
error that I know to exist from the controller's point of view in
$this->Person->validationErrors. I've been trying an ugly workaround as
commented in the code below, but I would like to know how to make it
work as it does for $error->showMessage('User/username'), and/or what I
am doing wrong. Thank you!

Here is the controller:

<?php
class UsersController extends AppController {

        var $name = 'User';
        var $helpers = array('Error','Html');

        function __construct() {

                parent::__construct();
                $this->Person = new Person();
        }

        function index() {

                $this->set('users',$this->User->findAll());

        }

        function update($id = null) {
                if (empty($this->data))

                {
                        $this->User->person_id = $id;
                        $this->data = $this->User->read();
                }
                else
                {

                        // load validation rules
                        $this->User->load_validation();
                        $this->Person->load_validation();

                        // if both models validate...
                        $valid_user = $this->User->validates($this->data);
                        $valid_person = $this->Person->validates($this->data);
                        if ($valid_user && $valid_person)
                        {
                                // ...try to save
                                if ($this->User->save($this->data['User']) &&
$this->Person->save($this->data['Person']))
                                {
                                        $this->flash("User 
{$this->data['User']['username']} has been
updated.",'/users');

                                } else {

                                        debug($this->User->validationErrors);
                                        debug($this->Person->validationErrors);

                                }
                        } else {
                                // nasty workaround
                                
$this->set('validationErrors',$this->Person->validationErrors);
                        }
                }
        }
}
?>

and here is the view:

<form method="POST" action="<?=$html->url('/users/update')?>">
<?=$html->hidden('Person/id')?>
<?=$html->hidden('User/person_id')?>
<tr>
        <td>First name</td>
        <td><?=$html->input('Person/firstname')?></td>
</tr>
<tr>
        <td>Last name</td>
        <td><?php
// the ugly workaround...
if (@$validationErrors['lastname']):?>
<div class="error_message"><?=$validationErrors['lastname']?></div>
<?php endif;
// ..because this doesn't work
$error->showMessage('Person/lastname')?><?=$html->input('Person/lastname')?></td>
</tr>
<tr>
        <td>Username</td>
        <td>
        <?=$error->showMessage('User/username') // <-- but this works fine ?>
        <?=$html->input('User/username')?></td>
</tr>
<tr><td>&nbsp;</td><td><?=$html->submit('Save')?></td>
</tr>
</form>

Thanks again.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php
-~----------~----~----~----~------~----~------~--~---

Reply via email to