简体   繁体   中英

Custom form type symfony

I need to set up a custom form type in Symfony that uses the choice type as a parent but doesn't actually require choices to be preloaded. As in I want to be able to populate the select with an ajax call and then submit with one of the options from the call without getting This value is not valid. errors, presumably because its not one of the preloaded options.

I don't need a custom data transformer as I am doing that through the bundle controller, I just need Symfony not to complain when I submit with an option that wasn't originally on the list. Here is what my custom form type looks like so far:

<?php

namespace ISFP\Index\IndexBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class NullEntityType extends AbstractType
{
    public function getDefaultOptions(array $options)
    {
        $defaultOptions = array(
            'em'                => null,
            'class'             => null,
            'property'          => null,
        );

        $options = array_replace($defaultOptions, $options);

        return $options;
    }

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

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

Dude look at the EntityType it has a parent as a choice . But entire display was handle by ChoiceType . When I was doing similar things I've started from overload Both ChoiceType and EntityType . And then set in overloaded Entity the getParent() to mine overloaded choice.

Finally In my case I modify the new choice and put there my embedded form. It's tricky to do It. And it consumes lot's of time.

But with that approach i don't have any problem with Validation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM