Newscoop\GimmeBundle\Form\Type\SnippetTemplateFieldType::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('name', null, array('required' => $defaultRequired, 'constraints' => $constraints));
        $builder->add('type', 'choice', array('choices' => array('integer' => 'integer', 'text' => 'text', 'textarea' => 'textarea', 'url' => 'url', 'boolean' => 'boolean'), 'required' => $defaultRequired, 'constraints' => $constraints));
        $builder->add('scope', 'choice', array('choices' => array('frontend' => 'frontend', 'backend' => 'backend'), 'required' => $defaultRequired, 'constraints' => $constraints));
        $builder->add('required', 'checkbox', array('required' => $defaultRequired, 'constraints' => $constraints));
        $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('type')->addEventListener(FormEvents::PRE_SUBMIT, $callback);
        $builder->get('scope')->addEventListener(FormEvents::PRE_SUBMIT, $callback);
        $builder->get('required')->addEventListener(FormEvents::PRE_SUBMIT, $callback);
    }