symfony1 - symfony 1.4 display images as choices in choice widget -


i have form choice widget (doctrinechoice). choices refer image file in server, , use expander=true option (for checkboxes/radiobuttons)

is there way display widget displaying images of each option? default, id of options in database.

using firebug, noticed generated html has tag id of each choice, , also, managed change image, i'm guessing need change text label of each choice. though, 'label' option of widget changes label whole select, won't do...

thanks!

ok, after lot of research, have managed kind of solution, maybe there's more correct?

instead of using sfwidgetformdoctrinechoice, used sfwidgetformselectradio (but checkbox can too, don't know if can work other widgets, or select widgets :/ because business rules require it, selectradio sufficient in particular case...)

the choices option of widget filled results of previous query using fill previous doctrinechoice widget, processed id of each record key , value of each choice:

$imgs = doctrine_core::gettable('projimages')->getimages(); $choices = array('' => ''); foreach ($imgs $img):   $choices[$img->getid()] = $img->getid(); endforeach; 

next, passed 'formatter' option widget:

$this->widgetschema['img'] = new sfwidgetformselectradio(array(                     'choices' => $choices,                     'formatter' => array($this, 'showasimages')                                 )); $this->validatorschema['img'] = new sfvalidatorchoice(array(                      'choices' => $choices,                      'required' => false                      )); 

i used 'required'=>false option in validator since need option select 'no image' in widget, reflected in $choices array first ('' => '') choice.

finally, wrote formatter callback:

public function showasimages($widget, $inputs) {   $rows = array();   foreach ($inputs $input)   {     $domdoc = new domdocument();     $domdoc->loadhtml($input['label']);     $node = $domdoc->getelementsbytagname('label')->item(0);     if ($node->nodevalue != "")     {       $img = doctrine_core::gettable('projimages')->find(array($node->nodevalue));       $input['label'] = '<label '.$node->attributes->item(0)->name .                         '="'.$node->attributes->item(0)->value.'">' .                         '<img src="'.$img->getimg().'" alt="image" />' .                         '</label>';     }     $rows[] = $widget->rendercontenttag('li',                     $input['input'].                     $widget->getoption('label_separator').                     $input['label']);   }   return $widget->rendercontenttag('ul',                        implode($widget->getoption('separator'), $rows),                        array('class' => $widget->getoption('class'))); } 

i used source code original default formatter of sfwidgetformselectradio , based on it, modified 'label' of each input element (all rest of code same source code used).

and label of each input element, used domdocument object value (id of image), make db query image, , re-assemble 'label' < img > tag... of course, if happen find empty choice, use default 'label'...

and it... think formatter callback can more work, suggestions, or better solutions problem, welcomed... can see, depend on 'formatter' option of widget, , far can see, widgets accept option...

thanks reading!


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -