Newscoop\GimmeBundle\Form\Type\SnippetType::buildForm PHP Метод

buildForm() публичный Метод

public buildForm ( Symfony\Component\Form\FormBuilderInterface $builder, array $options )
$builder Symfony\Component\Form\FormBuilderInterface
$options array
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $defaultRequired = true;
        $constraints = array(new NotBlank());
        if ($this->patch) {
            $defaultRequired = false;
            $constraints = array();
        }
        $builder->add('fields', 'collection', array('type' => new SnippetFieldType(array('patch' => $this->patch)), 'required' => $defaultRequired, 'constraints' => $constraints));
        $builder->add('name', null, array('required' => $defaultRequired, 'constraints' => $constraints));
        $builder->add('enabled', 'checkbox', array('required' => false));
        $callback = function (FormEvent $event) {
            if (null === $event->getData()) {
                // check if the Form's field is empty
                if (is_bool($event->getForm()->getData())) {
                    // check if it's a boolean
                    if ($event->getForm()->getData()) {
                        $event->setData('1');
                    } else {
                        $event->setData('0');
                    }
                } else {
                    // set the data back
                    $event->setData($event->getForm()->getData());
                }
            }
        };
        $builder->get('name')->addEventListener(FormEvents::PRE_SUBMIT, $callback);
        $builder->get('enabled')->addEventListener(FormEvents::PRE_SUBMIT, $callback);
    }