public function buildForm(FormBuilderInterface $builder, array $options) { if (!isset($options['object_accessor']) || !isset($options['object_mutator'])) { return; } $builder->setDataMapper($this); }
/** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { /** @var $dataWrapper \Netgen\Bundle\EzFormsBundle\Form\DataWrapper */ $dataWrapper = $options["data"]; if (!$dataWrapper instanceof DataWrapper) { throw new RuntimeException("Data must be an instance of Netgen\\EzFormsBundle\\Form\\DataWrapper"); } $content = $dataWrapper->target; if (!$content instanceof Content) { throw new RuntimeException("Data payload must be an instance of eZ\\Publish\\API\\Repository\\Values\\Content\\Content"); } $contentUpdateStruct = $dataWrapper->payload; if (!$contentUpdateStruct instanceof ContentUpdateStruct) { throw new RuntimeException("Data payload must be an instance of eZ\\Publish\\API\\Repository\\Values\\Content\\ContentUpdateStruct"); } $contentType = $dataWrapper->definition; if (!$contentType instanceof ContentType) { throw new RuntimeException("Data definition must be an instance of eZ\\Publish\\API\\Repository\\Values\\ContentType\\ContentType"); } if ($content->contentInfo->contentTypeId !== $contentType->id) { throw new RuntimeException("Data definition (ContentType) does not correspond to the data target (Content)"); } $builder->setDataMapper($this->dataMapper); foreach ($contentType->getFieldDefinitions() as $fieldDefinition) { // Users can't be created through Content, if ezuser field is found we simply skip it if ($fieldDefinition->fieldTypeIdentifier === "ezuser") { continue; } $languageCode = $contentUpdateStruct->initialLanguageCode; $handler = $this->fieldTypeHandlerRegistry->get($fieldDefinition->fieldTypeIdentifier); $handler->buildFieldUpdateForm($builder, $fieldDefinition, $content, $languageCode); } // Intentionally omitting submit buttons, set them manually as needed }
public function buildForm(FormBuilderInterface $builder, array $options) { $builder->setDataMapper(new IndexByTranslationMapper()); $formOptions = isset($options['form_options']) ? $options['form_options'] : array(); foreach ($options['locales'] as $locale) { $builder->add($locale, $options['form_type'], $formOptions); } }
public function buildForm(FormBuilderInterface $builder, array $options) { $isDefaultTranslation = 'defaultLocale' === $builder->getName(); // Custom mapper for translations if (!$isDefaultTranslation) { $builder->setDataMapper(new GedmoTranslationMapper()); } foreach ($options['locales'] as $locale) { if (isset($options['fields_options'][$locale])) { $builder->add($locale, 'a2lix_translationsFields', array('fields' => $options['fields_options'][$locale], 'translation_class' => $options['translation_class'], 'inherit_data' => $isDefaultTranslation)); } } }
public function buildForm(FormBuilderInterface $builder, array $options) { // Simple way is enough if (!$options['inherit_data']) { $builder->setDataMapper(new GedmoTranslationMapper()); $builder->addEventSubscriber($this->translationsListener); } else { if (!$options['translatable_class']) { throw new \Exception("If you want include the default locale with translations locales, you need to fill the 'translatable_class' option"); } $childrenOptions = $this->translationForm->getChildrenOptions($options['translatable_class'], $options); $defaultLocale = (array) $this->translationForm->getGedmoTranslatableListener()->getDefaultLocale(); $builder->add('defaultLocale', 'a2lix_translationsLocales_gedmo', array('locales' => $defaultLocale, 'fields_options' => $childrenOptions, 'inherit_data' => true)); $builder->add($builder->getName(), 'a2lix_translationsLocales_gedmo', array('locales' => array_diff($options['locales'], $defaultLocale), 'fields_options' => $childrenOptions, 'inherit_data' => false, 'translation_class' => $this->translationForm->getTranslationClass($options['translatable_class']))); } }
/** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { /** @var $dataWrapper \Netgen\Bundle\EzFormsBundle\Form\DataWrapper */ $dataWrapper = $options["data"]; if (!$dataWrapper instanceof DataWrapper) { throw new RuntimeException("Data must be an instance of Netgen\\EzFormsBundle\\Form\\DataWrapper"); } $userCreateStruct = $dataWrapper->payload; if (!$userCreateStruct instanceof UserCreateStruct) { throw new RuntimeException("Data payload must be an instance of eZ\\Publish\\API\\Repository\\Values\\User\\UserCreateStruct"); } $builder->setDataMapper($this->dataMapper); foreach ($userCreateStruct->contentType->getFieldDefinitions() as $fieldDefinition) { $handler = $this->fieldTypeHandlerRegistry->get($fieldDefinition->fieldTypeIdentifier); $handler->buildFieldCreateForm($builder, $fieldDefinition, $userCreateStruct->mainLanguageCode); } // Intentionally omitting submit buttons, set them manually as needed }
/** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder->setDataMapper($this); $factory = $builder->getFormFactory(); $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use($factory) { $form = $event->getForm(); $jobInstance = $form->getRoot()->getData(); if (null === $jobInstance->getId()) { return; } $job = $this->jobRegistry->get($jobInstance->getJobName()); $configProvider = $this->configProviderRegistry->get($job); $configs = $configProvider->getFormConfiguration(); $constraintProvider = $this->constraintProviderRegistry->get($job); $constraintsCollection = $constraintProvider->getConstraintCollection(); $fieldConstraints = $constraintsCollection->fields; foreach ($configs as $parameter => $config) { if (isset($config['system']) && true === $config['system']) { continue; } $config = array_merge(['type' => 'text', 'options' => []], $config); $options = array_merge(['auto_initialize' => false, 'required' => false, 'label' => ucfirst($parameter)], $config['options']); if (isset($fieldConstraints[$parameter])) { $options['constraints'] = $fieldConstraints[$parameter]->constraints; } $form->add($factory->createNamed($parameter, $config['type'], null, $options)); } }); $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) { $form = $event->getForm(); $jobInstance = $form->getRoot()->getData(); if (null === $jobInstance->getId()) { return; } $job = $this->jobRegistry->get($jobInstance->getJobName()); $configProvider = $this->configProviderRegistry->get($job); $configs = $configProvider->getFormConfiguration(); $data = $event->getData(); if (isset($configs['filters']) && isset($data['filters'])) { $data['filters'] = json_decode($data['filters'], true); } $event->setData($data); }); }
/** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { /** @var $dataWrapper \Netgen\Bundle\EzFormsBundle\Form\DataWrapper */ $dataWrapper = $options["data"]; if (!$dataWrapper instanceof DataWrapper) { throw new RuntimeException("Data must be an instance of Netgen\\EzFormsBundle\\Form\\DataWrapper"); } /** @var InformationCollectionInterface $model */ $model = $dataWrapper->payload; if (!$model instanceof InformationCollectionInterface) { throw new RuntimeException("Data payload must be an instance of Netgen\\EzFormsBundle\\Model\\InformationCollectionInterface"); } /** @var ContentType $contentType */ $contentType = $dataWrapper->definition; if (!$contentType instanceof ContentType) { throw new RuntimeException("Data definition must be an instance of eZ\\Publish\\API\\Repository\\Values\\ContentType"); } /** @var Content $content */ $content = $dataWrapper->target; if (!$content instanceof Content) { throw new RuntimeException("Data target must be an instance of eZ\\Publish\\API\\Repository\\Values\\Content\\Content"); } if ($content->contentInfo->contentTypeId !== $contentType->id) { throw new RuntimeException("Data definition (ContentType) does not correspond to the data target (Content)"); } $builder->setDataMapper($this->dataMapper); foreach ($contentType->getFieldDefinitions() as $fieldDefinition) { // Users can't be used as Information collectors if ($fieldDefinition->fieldTypeIdentifier === "ezuser") { continue; } $handler = $this->fieldTypeHandlerRegistry->get($fieldDefinition->fieldTypeIdentifier); if (!$fieldDefinition->isInfoCollector) { $handler->buildFieldUpdateForm($builder, $fieldDefinition, $content, $contentType->mainLanguageCode, true); } else { $handler->buildFieldCreateForm($builder, $fieldDefinition, $contentType->mainLanguageCode); } } // Intentionally omitting submit buttons, set them manually as needed }
/** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { if ($options['expanded']) { $builder->setDataMapper($options['multiple'] ? new CheckboxListMapper($options['choice_list']) : new RadioListMapper($options['choice_list'])); // Initialize all choices before doing the index check below. // This helps in cases where index checks are optimized for non // initialized choice lists. For example, when using an SQL driver, // the index check would read in one SQL query and the initialization // requires another SQL query. When the initialization is done first, // one SQL query is sufficient. $choiceListView = $this->createChoiceListView($options['choice_list'], $options); $builder->setAttribute('choice_list_view', $choiceListView); // Check if the choices already contain the empty value // Only add the placeholder option if this is not the case if (null !== $options['placeholder'] && 0 === count($options['choice_list']->getChoicesForValues(array('')))) { $placeholderView = new ChoiceView(null, '', $options['placeholder']); // "placeholder" is a reserved name $this->addSubForm($builder, 'placeholder', $placeholderView, $options); } $this->addSubForms($builder, $choiceListView->preferredChoices, $options); $this->addSubForms($builder, $choiceListView->choices, $options); // Make sure that scalar, submitted values are converted to arrays // which can be submitted to the checkboxes/radio buttons $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) { $form = $event->getForm(); $data = $event->getData(); // Convert the submitted data to a string, if scalar, before // casting it to an array if (!is_array($data)) { $data = (array) (string) $data; } // A map from submitted values to integers $valueMap = array_flip($data); // Make a copy of the value map to determine whether any unknown // values were submitted $unknownValues = $valueMap; // Reconstruct the data as mapping from child names to values $data = array(); foreach ($form as $child) { $value = $child->getConfig()->getOption('value'); // Add the value to $data with the child's name as key if (isset($valueMap[$value])) { $data[$child->getName()] = $value; unset($unknownValues[$value]); continue; } } // The empty value is always known, independent of whether a // field exists for it or not unset($unknownValues['']); // Throw exception if unknown values were submitted if (count($unknownValues) > 0) { throw new TransformationFailedException(sprintf('The choices "%s" do not exist in the choice list.', implode('", "', array_keys($unknownValues)))); } $event->setData($data); }); if (!$options['multiple']) { // For radio lists, transform empty arrays to null // This is kind of a hack necessary because the RadioListMapper // is not invoked for forms without choices $builder->addEventListener(FormEvents::SUBMIT, function (FormEvent $event) { if (array() === $event->getData()) { $event->setData(null); } }); } } elseif ($options['multiple']) { // <select> tag with "multiple" option $builder->addViewTransformer(new ChoicesToValuesTransformer($options['choice_list'])); } else { // <select> tag without "multiple" option $builder->addViewTransformer(new ChoiceToValueTransformer($options['choice_list'])); } if ($options['multiple'] && $options['by_reference']) { // Make sure the collection created during the client->norm // transformation is merged back into the original collection $builder->addEventSubscriber(new MergeCollectionListener(true, true)); } }
public function buildForm(FormBuilderInterface $builder, array $options) { $builder->setDataMapper(new MultiplePaymentsDataMapper()); $builder->add("products", 'entity', array('choices' => $options['products'], 'expanded' => true, 'multiple' => true, 'class' => 'Cva\\GestionMembreBundle\\Entity\\Produit', 'constraints' => array(new Count(array('min' => 1, 'minMessage' => 'Vous devez selectionner au moins un produit à vendre')), new ProductSell()))); $builder->add('method', 'choice', array('choices' => array('CHQ' => 'Cheque', 'CB' => 'Carte Bancaire', 'ESP' => 'Espèces'), 'mapped' => true, 'required' => true, 'expanded' => true, 'label' => "Moyen de paiement")); }