Symfony2使用小部件吐出3个文本字段

时间:2014-12-01 23:55:44

标签: php forms symfony entity

我可以设置一个小部件,以便它可以根据所需的格式吐出1-3个文本字段吗?我有一个Coordainate实体,有一个long和lat字段。我们的一些用户更喜欢Degrees Minutes Seconds,一些Degrees Minutes,一些Decimal。我们有自定义来处理我们当前的系统(Symfony 1.4),我想使用这些相同的自定义文件。我确定通过选项传递了吗?

这是我的表单类:

    <?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';
    }
}

**编辑:自定义文件的示例使用:

    $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'] as $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 to 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 to 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 to use_fields array
                        array_push($use_fields, $field['name']);

                    }

1 个答案:

答案 0 :(得分:1)

您可以根据选项中的内容使用构建器方法中的条件:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    if(<some state of options>) {
        $builder->add(...);
    }
    // and so on
}

顺便说一句,“处理此问题的自定义”是什么意思?