php - Symfon2 Spit out 3 text fields with a widget -
can set widget such can spit out 1-3 text fields depending on desired format? have coordainate entity long , lat field. of ours users prefer degrees minutes seconds, degrees minutes, decimal. have customizations handle on our current system (symfony 1.4) , use these same customization files. i'm sure that's passed in through options?
here's form class:
<?php namespace ***\adminbundle\form; use symfony\component\form\abstracttype; use symfony\component\form\formbuilderinterface; use symfony\component\optionsresolver\optionsresolverinterface; class coordinatetype extends abstracttype { /** * @param formbuilderinterface $builder * @param array $options */ public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('gps84lat') ->add('gps84long') ->getform() ; } /** * @param optionsresolverinterface $resolver */ public function setdefaultoptions(optionsresolverinterface $resolver) { $resolver->setdefaults(array( 'data_class' => '***\adminbundle\entity\coordinate' )); } /** * @return string */ public function getname() { return '***_adminbundle_coordinate'; } }
**edit: example use of customization file:
$custom = $options['attr']['customization_file']; $cf = $custom['form']; if(!array_key_exists(0, $cf['fields']['field'])) $cf['fields']['field'] = array($cf['fields']['field']); foreach($cf['fields']['field'] $field) { if(in_array($field['name'], array('gps_84_lat', 'gps_84_long'))) { if($field['format'] == 'dms') { // standard degrees-minutes-seconds gps field if($field['name'] == 'gps_84_lat') $gps = $this->getobject()->getgps84lat(); elseif($field['name'] == 'gps_84_long') $gps = $this->getobject()->getgps84long(); else $gps = ''; // degrees $this->setwidget($field['name'].'_degrees', customwidgetextender::createsfwidget($field, $user, $this)); $this->widgetschema->setlabel($field['name'].'_degrees', $field['label']); if(!$this->isnew()) $this->setdefault($field['name'].'_degrees', dataman::getgpsdeg($gps)); elseif(trim($field['default']) != '') $this->setdefault($field['name'].'_degrees', dataman::getgpsdeg($field['default'])); // minutes $this->setwidget($field['name'].'_minutes', customwidgetextender::createsfwidget($field, $user, $this)); $this->widgetschema->setlabel($field['name'].'_minutes', $field['label']); if(!$this->isnew()) $this->setdefault($field['name'].'_minutes', dataman::getgpsmin($gps)); elseif(trim($field['default']) != '') $this->setdefault($field['name'].'_minutes', dataman::getgpsmin($field['default'])); // seconds $this->setwidget($field['name'].'_seconds', customwidgetextender::createsfwidget($field, $user, $this)); $this->widgetschema->setlabel($field['name'].'_seconds', $field['label']); if(!$this->isnew()) $this->setdefault($field['name'].'_seconds', dataman::getgpssec($gps)); elseif(trim($field['default']) != '') $this->setdefault($field['name'].'_seconds', dataman::getgpssec($field['default'])); // add field name use_fields array array_push($use_fields, $field['name'].'_degrees'); array_push($use_fields, $field['name'].'_minutes'); array_push($use_fields, $field['name'].'_seconds'); } elseif($field['format'] == 'dm') { // degrees-decimal minutes gps field if($field['name'] == 'gps_84_lat') $gps = $this->getobject()->getgps84lat(); elseif($field['name'] == 'gps_84_long') $gps = $this->getobject()->getgps84long(); else $gps = ''; // degrees $this->setwidget($field['name'].'_degrees', customwidgetextender::createsfwidget($field, $user, $this)); $this->widgetschema->setlabel($field['name'].'_degrees', $field['label']); if(!$this->isnew()) $this->setdefault($field['name'].'_degrees', dataman::getgpsdeg($gps)); elseif(trim($field['default']) != '') $this->setdefault($field['name'].'_degrees', dataman::getgpsdeg($field['default'])); // minutes $this->setwidget($field['name'].'_minutes', customwidgetextender::createsfwidget($field, $user, $this)); $this->widgetschema->setlabel($field['name'].'_minutes', $field['label']); if(!$this->isnew()) $this->setdefault($field['name'].'_minutes', dataman::getgpsmin($gps, 5)); elseif(trim($field['default']) != '') $this->setdefault($field['name'].'_minutes', dataman::getgpsmin($field['default'], 5)); // add field name use_fields array array_push($use_fields, $field['name'].'_degrees'); array_push($use_fields, $field['name'].'_minutes'); } else { // standard decimal degrees gps field $this->setwidget($field['name'], customwidgetextender::createsfwidget($field, $user, $this)); $this->widgetschema->setlabel($field['name'], $field['label']); if(trim($field['default']) != '') $this->setdefault($field['name'], $field['default']); // add field name use_fields array array_push($use_fields, $field['name']); }
you can use conditions in builder method basing on in options:
public function buildform(formbuilderinterface $builder, array $options) { if(<some state of options>) { $builder->add(...); } // , on }
by way, mean 'customizations handle this'?
Comments
Post a Comment