Symfony\Component\Form\FormFactory::createNamedBuilder PHP Метод

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

public createNamedBuilder ( $name, $type = 'form', $data = null, array $options = [], Symfony\Component\Form\FormBuilderInterface $parent = null )
$options array
$parent Symfony\Component\Form\FormBuilderInterface
    public function createNamedBuilder($name, $type = 'form', $data = null, array $options = array(), FormBuilderInterface $parent = null)
    {
        if (null !== $data && !array_key_exists('data', $options)) {
            $options['data'] = $data;
        }

        if ($type instanceof FormTypeInterface) {
            $type = $this->resolveType($type);
        } elseif (is_string($type)) {
            $type = $this->registry->getType($type);
        } elseif (!$type instanceof ResolvedFormTypeInterface) {
            throw new UnexpectedTypeException($type, 'string, Symfony\Component\Form\ResolvedFormTypeInterface or Symfony\Component\Form\FormTypeInterface');
        }

        return $type->createBuilder($this, $name, $options, $parent);
    }

Usage Example

Пример #1
1
 /**
  * Obtains any form metadata from the entity and adds itself to the form
  * @param null $data
  * @param string $name
  * @param array $options
  * @return \Symfony\Component\Form\FormBuilderInterface
  */
 public function createFormBuilder($data = null, $name = '', array $options = array())
 {
     if (array_key_exists('method', $options)) {
         $method = $options['method'];
         unset($options['method']);
     }
     // Build the $form
     $formBuilder = $this->factory->createNamedBuilder($name, FormType::class, $data, $options);
     if (isset($method)) {
         $formBuilder->setMethod($method);
     }
     // Read the entity meta data and add to the form
     if (empty($this->drivers)) {
         return $formBuilder;
     }
     // Look to the readers to find metadata
     foreach ($this->drivers as $driver) {
         $metadata = $driver->getMetadata($data);
         if (!empty($metadata)) {
             break;
         }
     }
     if (empty($metadata)) {
         return $formBuilder;
     }
     // Configure the form
     $fields = $metadata->getFields();
     foreach ($fields as $field) {
         // TODO: Detect "new x()" in field value or type option for AbstractType creation
         // TODO: Detect references to "%service.id%" for service constructor dependency
         $fieldOptions = $field->options;
         if (in_array($formBuilder->getMethod(), ['POST']) && array_key_exists('onCreate', $fieldOptions)) {
             if ($fieldOptions['onCreate']) {
                 $fieldOptions = array_replace($fieldOptions, $fieldOptions['onCreate']);
             } else {
                 continue;
             }
         } elseif (in_array($formBuilder->getMethod(), ['PUT', 'PATCH']) && array_key_exists('onEdit', $fieldOptions)) {
             if ($fieldOptions['onEdit']) {
                 $fieldOptions = array_replace($fieldOptions, $fieldOptions['onEdit']);
             } else {
                 continue;
             }
         }
         unset($fieldOptions['onCreate']);
         unset($fieldOptions['onEdit']);
         $formBuilder->add($field->name, $field->value, $fieldOptions);
     }
     return $formBuilder;
 }
All Usage Examples Of Symfony\Component\Form\FormFactory::createNamedBuilder