1
votes

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 WE\BobbyWebAppBundle\Form\Field;

    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormView;
    use Symfony\Component\Form\FormInterface;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;
    use Symfony\Component\PropertyAccess\PropertyAccess;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\Form\Extension\Core\EventListener\TrimListener;

    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,

1
please show us some code exampleuser975475
I've just edited and exposed the used code above. Thanks.peris

1 Answers

0
votes

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'));