exo_duz,

I have something similar that may be useful to you,
its not the best way to do it but it gets the job done.

First, you need prototype and scriptaculous on your webroot/js/ folder

on your Controller:

class EventsController extends AppController {
        var $name='Events';
        var $uses=array('Event', 'EventDetails');

        function add() {
                $totalEventDetails = 1; // unless i have already posted some
content...
                if (!empty($this->data)) {
                        $this->Event->set($this->data['Event']);
                        if ($this->Event->validates()) {
                                // data for Event model has been validated
                                $error_in_details = false;
                                foreach ($this->data['EventDetails'] as 
$key=>$detail) {
                                        
$this->Event->EventDetail->set($detail['EventDetail']);
                                        if 
(!$this->Event->EventDetail->validates()) {
                                                $error_in_details = true;
                                                $error_in_detail[$key] = true;
                                        } else {
                                                $error_in_detail[$key] = false;
                                        }
                                }
                                // if there are no errors, save Event and 
EventDetails
                                if (!$error_in_details) {
                                        // set user id
                                        $this->Event->create();
                                        
$this->Event->save($this->data['Event'], false);
                                        foreach ($this->data['EventDetails'] as 
$key=>$detail) {
                                                
$detail['EventDetail']['event_id'] = $this->Event->id;
                                                
$this->Event->EventDetail->create();
                                                
$this->Event->EventDetail->save($detail, false);
                                        }
                                        $this->Session->setFlash(__('The Event 
and EventDetails have been
saved', true));
                                        
$this->redirect(array('action'=>'index'));
                                } else {
                                        $this->Session->setFlash(__('The 
EventDetail could not be saved.
Please, try again.', true));
                                }
                        } else {
                                $this->Session->setFlash(__('The Event could 
not be saved. Please,
try again.', true));
                        }
                        $totalEventDetails = count($this->data['EventDetails']);
                }
                $this->set('totalEventDetails',$totalEventDetails);
        }

        function ajaxAddDetail($total_details) {
                $details = $this->data['EventDetails'];
                $this->set(compact('details'));
                $this->set('totalEventDetails',$total_details+1);
                $this->render('add','ajax');
        }
}

on your View:

<?php echo $javascript->link('prototype'); ?>
<?php echo $javascript->link('scriptaculous'); ?>
        <fieldset>
                <legend><?php __('Add Event');?></legend>
                <!-- your form fields here -->
<a href="/events/ajaxAddDetail/<?php echo $totalEventDetails; ?>"
id="ajax_add_detail_updater" onclick=" event.returnValue = false;
return false;">click to add details field</a>

                <?php echo $ajax->div('ajax_detail_updater', array
('class'=>'last')); ?>
                <script type="text/javascript">
        //<![CDATA[
        Event.observe(  'ajax_add_detail_updater',
                                                'click',
                                                function(event) {
                                                        new 
Ajax.Updater('ajax_detail_updater'
                                                                                
        ,'/events/ajaxAddDetail/<?php echo $totalEventDetails; ?
>',
                                                                                
        {
                                                                                
                asynchronous:true,
                                                                                
                evalScripts:true,
                                                                                
                parameters: Form.serialize($('EventAddForm')),
                                                                                
                requestHeaders:['X-Update', 'ajax_detail_updater']})
                                                },
                                                false
                                        );
        //]]>
        </script>
        <?php echo $form->text('EventDetails.'.
$c.'.EventDetail.description',array('id'=>'detail_'.$c,'value'=>@
$details[$c]['description'])); ?>
        <?php echo $ajax->divEnd('ajax_detail_updater'); ?>
        </fieldset>
<?php echo $form->end(array('label'=>__
('Submit',true),'class'=>'submit button'));?>

This code may contain errors, I copied it and modified it from a
working example but I didn't test it myself, I hope it helps

On Feb 6, 1:48 am, exo_duz <robin.jul...@gmail.com> wrote:
> Hi all,
>
> Just some quick questions and hoping to get pointed in the right
> direction. I have an events table which has many event_details and
> pictures.
>
> events --hasMany--> event_details
> events --hasMany--> pictures
>
> A couple of questions firstly with the event_details. Is there a way
> to create the the events and the event_details in 1 form. For the
> event_details section of the form I would like to ultimately be able
> to create fields on the fly. I have searched Google and most of the
> tutorials state that you'd have to create the fields in JS which is
> not efficient and very tedious for me as the event_details section has
> about 20 fields. What is the best way to achieve this? Does anyone
> have any exprience with this? I'm not too sure about this as JS is not
> my strong suite.
>
> Secondly, is there a multiple picture uploader with or without a
> resizer that is recommended to be used with Cake? This would really
> help a lot.
>
> Thanks everyone for all your replies.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to