symfony1 - Symfony 1.4 Form Templating -
i'm working on installation of symfony 1.4 we've needed re-skin match new company branding. 1 of requirements of new skin radio buttons have following syntax -
<label><input name="foo" type="radio" value="bar" id="foo_bar"><span></span></label> however default symfony code radio buttons -
<input name="foo" type="radio" value="bar" id="foo_bar"> where template can change add additional elements surround input tag? i've searched high , low through code , can't seem find code being generated.
thanks in advance!
you'll need create custom widget class. i've extended radio widget , changed formatter functionality.
you might need customise little. below mywidgetformselectradio class wrap each radio field in label you've specified.
// \lib\widget\mywidgetformselectradio.class.php class mywidgetformselectradio extends sfwidgetformselectradio { protected function formatchoices($name, $value, $choices, $attributes) { $inputs = array(); foreach ($choices $key => $option) { $baseattributes = array( 'name' => substr($name, 0, -2), 'type' => 'radio', 'value' => self::escapeonce($key), 'id' => $id = $this->generateid($name, self::escapeonce($key)), ); if (strval($key) == strval($value === false ? 0 : $value)) { $baseattributes['checked'] = 'checked'; } $inputs[] = array( 'input' => $this->rendercontenttag('label', $this->rendertag('input', array_merge($baseattributes, $attributes)), array('for' => $id)), ); } return call_user_func($this->getoption('formatter'), $this, $inputs); } public function formatter($widget, $inputs) { $rows = array(); foreach ($inputs $input) { $rows[] = $input['input'].$this->getoption('label_separator'); } return implode($this->getoption('separator'), $rows); } } then in form classes
// \lib\form\myform.class.php public function configure() { $this->widgetschema['my_field'] = new mywidgetformselectradio(array('choices' => $choices)); // .... } are using twitter bootstrap chance? if so, you'll want extend symfony checkbox widget well. if might want extend sfwidgetformchoice class , change getrenderer() method uses mywidgetformselectradio , mywidgetformselectcheckbox class when rendering radio/checkbox fields.
// \lib\widget\mywidgetformchoice.php class mywidgetformchoice extends sfwidgetformchoice { public function getrenderer() { if ($this->getoption('renderer')) { return $this->getoption('renderer'); } $prefix = $this->getoption('use_my_custom_widget') ? 'my' : 'sf'; if (!$class = $this->getoption('renderer_class')) { $type = !$this->getoption('expanded') ? '' : ($this->getoption('multiple') ? 'checkbox' : 'radio'); $class = sprintf($prefix . 'widgetformselect%s', ucfirst($type)); } return new $class(array_merge(array('choices' => new sfcallable(array($this, 'getchoices'))), $this->options['renderer_options']), $this->getattributes()); } } then in form class
public function configure() { $this->widgetschema['my_field'] = new mywidgetformchoice(array( 'choices' => $choices, 'expanded' => true, 'use_my_custom_widget' => true, )); // .... }
Comments
Post a Comment