Saving multiple models from a view and validation

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Saving multiple models from a view and validation

realien

I've been looking at this for a few hours now and searched a lot of
posts, so I'm throwing my hands in the air and asking for help :)

I have an add method on my controller :

 function add()
           {

                if (empty($this->data) )
                {

                    $this->render('add');
                }
                else
                {


                    $this->data['Asset']['archive_state_id'] = 1;
                    $this->data['Asset']['asset_uri'] =
"tbd/fakefolder";
                    $this->data['Asset']['active'] = 'y';

                    if ($this->Asset->save($this->data))
                    {
                        $assetId = $this->Asset->getLastInsertId();

                        $this->data['AssetData']['asset_id'] =
$assetId;


                        if ($this->Asset->AssetData->save($this->data))
                        {
                            $this->flash("saved the asset",
"/assets/add");
                        }
                        else
                        {

                            $this->set('data', $this->params['data']);
                            $this->validateErrors($this->AssetData);
                            $this->render('add');
                        }
                    }
                }
            }


my add view has the appropriate html tag tagErrorMsg

<form method="post" action="<?php echo $html->url('/assets/add')?>">
    <p>
        Slug :
        <?php echo $html->input('AssetData/slug', array('size' =>
'50'))?>
        <?php echo $html->tagErrorMsg('AssetData/slug', 'Slug is
required.') ?>
    </p>
    <p>
        Other Info:
        <?php echo $html->textarea('AssetData/other_info',
array('rows'=>'10')) ?>
    </p>
    <p>
        <?php echo $html->submit('Save') ?>
    </p>
</form>

my asset data model has the validate array :

var $validate = array(

        'slug'  => VALID_NOT_EMPTY


    );

and my AssetController has the correct uses :

           var $uses = array('Asset', 'AssetData');


What happens right now is it correctly saves the asset and then save on
the AssetData model returns false since the validation fails (I see it
by single stepping the code) but my html tag error messages never get
displayed...

Please someone help me from this frustration :)

Thanks
Grant


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

Reply | Threaded
Open this post in threaded view
|

RE: Saving multiple models from a view and validation

realien

I got validation to work by changing this :

$this->validateErrors($this->AssetData);

to

$this->validateErrors($this->Asset->AssetData);

Can someone tell me why the former doesn't work ?

Grant

-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On
Behalf Of realien
Sent: Wednesday, November 01, 2006 10:31 AM
To: Cake PHP
Subject: Saving multiple models from a view and validation


I've been looking at this for a few hours now and searched a lot of
posts, so I'm throwing my hands in the air and asking for help :)

I have an add method on my controller :

 function add()
           {

                if (empty($this->data) )
                {

                    $this->render('add');
                }
                else
                {


                    $this->data['Asset']['archive_state_id'] = 1;
                    $this->data['Asset']['asset_uri'] =
"tbd/fakefolder";
                    $this->data['Asset']['active'] = 'y';

                    if ($this->Asset->save($this->data))
                    {
                        $assetId = $this->Asset->getLastInsertId();

                        $this->data['AssetData']['asset_id'] =
$assetId;


                        if ($this->Asset->AssetData->save($this->data))
                        {
                            $this->flash("saved the asset",
"/assets/add");
                        }
                        else
                        {

                            $this->set('data', $this->params['data']);
                            $this->validateErrors($this->AssetData);
                            $this->render('add');
                        }
                    }
                }
            }


my add view has the appropriate html tag tagErrorMsg

<form method="post" action="<?php echo $html->url('/assets/add')?>">
    <p>
        Slug :
        <?php echo $html->input('AssetData/slug', array('size' =>
'50'))?>
        <?php echo $html->tagErrorMsg('AssetData/slug', 'Slug is
required.') ?>
    </p>
    <p>
        Other Info:
        <?php echo $html->textarea('AssetData/other_info',
array('rows'=>'10')) ?>
    </p>
    <p>
        <?php echo $html->submit('Save') ?>
    </p>
</form>

my asset data model has the validate array :

var $validate = array(

        'slug'  => VALID_NOT_EMPTY


    );

and my AssetController has the correct uses :

           var $uses = array('Asset', 'AssetData');


What happens right now is it correctly saves the asset and then save on
the AssetData model returns false since the validation fails (I see it
by single stepping the code) but my html tag error messages never get
displayed...

Please someone help me from this frustration :)

Thanks
Grant




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

Reply | Threaded
Open this post in threaded view
|

RE: Saving multiple models from a view and validation

realien

Ok, now I have my validation working with a save I have a bit of a conundrum.

 

It doesn't look like cake fully supports transactions,

 

My Asset hasOne AssetData

 

The asset_data table has a foreign key asset_id back to the asset table.

 

To insert a complete asset, I need to insert a record in the asset table and in the asset_data table.

 

The code does this :

 

$this->data['Asset']['archive_state_id'] = 1;

                        $this->data['Asset']['asset_uri'] = "tbd/fakefolder";

                        $this->data['Asset']['active'] = 'y';

                       

                        if ($this->Asset->save($this->data))

                        {

                            $assetId = $this->Asset->getLastInsertId();

                           

                            $this->data['AssetData']['asset_id'] = $assetId;

                            

                           

                            if ($this->Asset->AssetData->save($this->data))

                            {

                                $this->flash("saved the asset", "/assets/add");

                            }

                            else

                            {

                                $this->flash("Unknown error trying to save asset", "/assets/add"); 

                               

                            }

 

The problem is if validation fails on my assetData then I will have already saved the asset record. The cake docs and some discussion did not recommened calling invalidates($this->data) from my model since save calls that before saving, but since there are no real transactions and I can’t rollback, should I just throw caution to the wind and directly call invalidates before saving the asset model so that if validation fails I’ll know before I’ve created an asset record ?

 

Thanks

Grant


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