On Oct 3, 2008, at 3:19 PM, Matthew Lurz wrote:
Sorry, that didn't come through quite right. See http://www.paste2.org/p/81737 instead. Matthew Lurz wrote:I wouldn't include the image in the form at all actually. Instead I would just wrap a conditional around a div in the view script. Something like:<? if (this->image): ?> <div> "<?= $this- image->src ?>"></div> <? endif ?> <?= $this->form ?> Edward Haber wrote:That does sound like a better solution to me. Also, the link in the form could frustrate some users if they've partially completed the form then delete the image. Thanks for your comments. So then the best solution appears to be a decorator. On Oct 3, 2008, at 1:28 PM, Matthew Lurz wrote:In that case why not forego the remove action and the additional form logic and just display the form regardless of whether the image exists. When the user uploads a new image it would replace the existing one. Just wondering since it seems like the user would have to jump through an extra hoop to remove the image in order to upload a new one. The less a user does or has to do the better. Edward Haber wrote:Here's the solution I came up with. Thanks for all the input and help on this problem. This solution involves using a conditional statementin Zend_Form to create one of two element types. When the image ispresent the view helper decorator replaces the form element with theimage and a link to a remove action. The view helper is below and willstill be tweaked a bit but it should give the overall idea. This form class snippet has all the other elements removed from it for clarityclass MyForm extends Zend_Dojo_Form { public function __construct($options = null) { parent::__construct($options); $db = Zend_Registry::get('db'); $this->setName('article_form') ->setAction(URL.'/admin/article/save') ->setMethod('post')->addPrefixPath('MF_Form_Element', 'MF/Form/Element/', 'element')->setAttrib('enctype', 'multipart/form-data') ->setAttrib('id', 'article-form'); /** * image_name */ if ($options['image_id']) { $this->addElement('file', 'image_name', array( 'label' => 'Image', 'description' => 'To upload another photo delete this one first.', 'attribs' => array('imgSrc' => URL . '/media/articles/' . $options['image_id'] .'.jpg', 'action' => URL . '/admin/article/drop-image/' . $options['image_id'] ), 'decorators' => array( array('ViewHelper', array('helper' => 'mediaImage')), 'Errors', 'Description', array('HtmlTag', array('tag' => 'dd')), array('Label', array('tag' => 'dt')) ) )); } else { $this->addElement('file', 'image_name', array( 'label' => 'Upload an image', 'destination' => AP . '/application/tmp/upload-staging', 'required' => true, 'validators' => array( array('Size', '500Kb'), array('ImageSize', array(10, 10, 1024, 768)), array('Extension', array('jpg', 'jpeg')) ) )); } (other form elements removed) Then I use this helper: class MF_View_Helper_MediaImage {public function mediaImage($fieldname, $value = "", $attribs = null){ $imgSrc = $attribs['imgSrc']; $action = $attribs['action']; $class = isset($attribs['class']) ? $attribs['class'] : 'image- remove-button'; return <<<OUTPUT $imgSrc | $action Delete OUTPUT; } }This will pretty much be a reusable solution for the four CMS mediasubdirectories of image content and seems to work just fine. Happy for any comments. EH On Oct 3, 2008, at 8:12 AM, Matthew Lurz wrote:I would keep the image separate from the form unless you have a goodreasonnot to. Then the logic is simple. Display the image if it exists, ifnotthen display the form. Of course you would need an action to deletethe image so that the form could be redisplayed. Edward Haber wrote:Definitely view logic. Good call. Still I'm uncertain though because I'm working with an extended Zend_Form class. Perhaps I should make a custom Form Element? Or a custom Form Decorator? Or use the viewhelper decorator for just that element? I'd love to hear the Zendbest practice take on this solution. Thanks! On Oct 2, 2008, at 10:50 PM, Matthew Lurz wrote:Sounds like view logic to me and so I would add a conditional inthe view script. Edward Haber wrote:I have a form that I want offer an image upload form element on.Thishas worked great using Zend_Form_Element_File. The upload worksfine. What is the recommended way to have the uploaded image replace theupload form if an image exists? I don't need the solution coded,just the direction I should go.For example, I have the form as a class. SHould I pass the imagename (if one exists) in the config array to the form? In the form i wouldthen check for $config['imageName'] and if it exists display theimage in place of the form element. This is another trick - how do display an image in a form?Is the answer toggling in the form class between the form elementand an image? Or using CSS to display the image if it's there and "hide" the form (display:none for example).I'm just looking for best practice. I'm sure lots of people havealready confronted this situation. Any input much appreciated! Thanks! Eddie-- View this message in context: http://www.nabble.com/Toggle-file-upload-element-with-uploaded-image-tp19780259p19791263.htmlSent from the Zend Framework mailing list archive at Nabble.com.-- View this message in context: http://www.nabble.com/Toggle-file-upload-element-with-uploaded-image-tp19780259p19796584.html Sent from the Zend Framework mailing list archive at Nabble.com.-- View this message in context: http://www.nabble.com/Toggle-file-upload-element-with-uploaded-image-tp19780259p19802308.html Sent from the Zend Framework mailing list archive at Nabble.com.-- View this message in context: http://www.nabble.com/Toggle-file-upload-element-with-uploaded-image-tp19780259p19803976.html Sent from the Zend Framework mailing list archive at Nabble.com.