Re: QF_Controller dynamic list?

From: Date: Fri, 03 Oct 2003 19:52:22 +0000
Subject: Re: QF_Controller dynamic list?
References: 1 2  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-8270@lists.php.net to get a copy of this message
Hi Mike! Think, I know how to do it now. Just adding an element (text and a checkbox) to the form once you have detected that a file has been uploaded successfully is easy. However, you must ensure you can save the information to next time the user adds a new file. Therefore I had to add a new action-class to be trigged when a "upload file"-button is clicked. I've done like this. -- This is the action-object called when user clicks on "upload file"-button. Note $page->addFile();. (The last line reloads the page.) class UploadFile extends HTML_QuickForm_Action { function perform(&$page, $actionName) { $page->isFormBuilt() or $page->buildForm(); $pageName = $page->getAttribute('name'); $data =& $page->controller->container(); $data['values'][$pageName] = $page->exportValues(); $data['valid'][$pageName] = $page->validate(); if ( $data['valid'][$pageName] ) { $file =& $page->getElement('file'); $page->addFile($file); } return $page->handle('jump'); } } -- This is the class for the page where the file-upload form resides. When a file "arrives" a new group with the file information is added to the form and the values of the group's elements is set with HTML_QuickForm_Controller::setConstants() that saves the elements' data in the $_SESSION variable. This has to be done because on subsequent access to the "file-upload" page, this is the only way to make it possible to recreate the list. I.e. to determine which list items already exist and that should be added dynamically to the form, you have to seek through the constants (which consequently are saved beetween subsequent calls) for the appropriate elements. class Page_File extends HTML_QuickForm_Page { function buildForm() { ... // Creat the file information list $data =& $this->controller->container(); $i = 0; while ( isset($data['constants']["file_info_$i"]) ) { $this->insertFileGroup($i++); } } function addFile(&$file) { $save_dir = 'c:\some_path\\'; $save_name = time().'.txt'; if ( $file->moveUploadedFile($save_dir, $save_name) ) { $data =& $this->controller->container(); $i = 0; while ( isset($data['constants']["file_info_$i"]) ) { $i++; } $this->insertFileGroup($i); $file_value = $file->getValue(); $file_info['name'] = $file_value['name']; $file_info['size'] = $file_value['size']; $file_info['type'] = $file_value['type']; $file_info['path'] = $save_dir.$save_name; $this->controller->setConstants(array("file_info_$i"=>$file_info )); } } function insertFileGroup($i) { $file_info_group[1] =& $this->createElement('text', 'name', null, 'size=10'); $file_info_group[2] =& $this->createElement('text', 'size', null, 'size=5'); $file_info_group[3] =& $this->createElement('text', 'type', null, 'size=10'); $file_info_group[4] =& $this->createElement('text', 'path', null, 'size=50'); $this->addGroup($file_info_group, "file_info_$i", "file_info_$i", ' ', true); } } This code above is not the final version but shows the principle. If you have any other neat solution I will gladly acquaint myself with it. Hope you didn't fall asleep to many times during your reading :) All the best Markus

« previous php.pear.general (#8270) next »