path in custom field type

Workaround : By now cnhanging form parent from form to text did the trick.

i've just created a custom field type whose parent is form.

Does any one know how can i get the right property_path? I mean, inside MyFieldType i would like to access to the property of MyFormType which made use of my_field_type field so i would be able to dinamically set the right property_path.

Here's my custom field type. Inside the following class would like to dinamically set the Form Type property who makes use of ColorPaletteField as propery_path value.



    namespace WEBobbyWebAppBundleFormField;

    use SymfonyComponentFormAbstractType;
    use SymfonyComponentFormFormView;
    use SymfonyComponentFormFormInterface;
    use SymfonyComponentOptionsResolverOptionsResolverInterface;
    use SymfonyComponentPropertyAccessPropertyAccess;
    use SymfonyComponentFormFormBuilderInterface;
    use SymfonyComponentFormExtensionCoreEventListenerTrimListener;

    class ColorPaletteField extends AbstractType
    {
        public function setDefaultOptions( OptionsResolverInterface $resolver )
        {
            $resolver->setDefaults( array(
                    'mapped'            => true,
                    'error_bubbling'    => false,
                    'colors'            => array()
                )
            );
        }

        /**
         * Pass the help to the view
         *
         * @param FormView $view
         * @param FormInterface $form
         * @param array $options
         */
        public function buildView( FormView $view, FormInterface $form, array $options )
        {
            $parentData = $form->getParent()->getData();

            if( null !== $parentData )
            {
                $accessor       = PropertyAccess::getPropertyAccessor();
                $defaultColor   = $accessor->getValue( $parentData, 'calendar_color' );
            }
            else { $defaultColor = null; }

            if( array_key_exists( 'colors', $options ) )
            {
                $colors = $options[ 'colors' ];
            }
            else { $colors = array(); }

            $view->vars[ 'colors' ]         = $colors;
            $view->vars[ 'defaultColor' ]   = $defaultColor;
        }

        public function getParent()
        {
            return 'form';
        }

        public function getName()
        {
            return 'color_palette';
        }
    }

Thanks in advanced,


You can pass it in options. First set default in your custom field

$resolver->setDefaults(array(
    'mapped'            => true,
    'error_bubbling'    => false,
    'colors'            => array()
    'property_name'     => 'calendar_color'
)); 

then you add this field to form and define property name it in options

->add('some_name', 'color_palette', array('property_name' => 'some_name'));
链接地址: http://www.djcxy.com/p/57888.html

上一篇: 验证复选框未以symfony2格式映射到实体

下一篇: 自定义字段类型中的路径