2017 © Pedro Peláez
 

symfony-bundle form-bundle

Bundle for working with Symfony forms

image

imatic/form-bundle

Bundle for working with Symfony forms

  • Friday, July 13, 2018
  • by stepan.koci
  • Repository
  • 7 Watchers
  • 0 Stars
  • 217 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 27 Versions
  • 36 % Grown

The README.md

Build Status License: MIT, (*1)

ImaticFormBundle

Form types

Ajax entity choice

Implements choice of single entity or collection of entities using XHR., (*2)

Dependencies: jQuery, Select2, (*3)

``` php <?php, (*4)

use Imatic\Bundle\FormBundle\Form\Type\AjaxEntityChoiceType;, (*5)

class ExampleType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('author', AjaxEntityChoiceType::class, [ 'class' => 'MyExampleBundle:User', 'route' => 'app_example_example_autocomplete', ]); } }, (*6)


The widget will send a GET request to the specified route with the entered search term. The request format depends on the `request_type` option: - `filter` - ?filter\[search\]\[value\]=term - `simple` - ?search=term The application should reply with a JSON response, example: ``` json [ {"id": 1, "text": "First Item"}, {"id": 2, "text": "Second Item"} ]

Additional options:, (*7)

  • multiple - allow multiple items to be selected
  • placeholder - text displayed if no item is selected
  • query_builder - instance of QueryBuilder or Closure(EntityManager \$em, \$class): QueryBuilder
  • id_provider - callable(object \$entity): scalar (should return ID of the entity)
  • text_provider - callable(object \$item): string (should return text representation of the entity)
  • request_type - filter (default) or simple
  • route_attrs - custom route attributes
  • entity_manager - name of the entity manager to use

Datepicker

This example shows, how to change default date type format and modify moment configurations by form extension., (*8)

``` php <?php, (*9)

namespace App\Form\Extension;, (*10)

use Imatic\Bundle\FormBundle\Form\Extension\DatepickerExtension; use Symfony\Component\OptionsResolver\OptionsResolver;, (*11)

class DateTypeExtension extends DatepickerExtension { public function configureOptions(OptionsResolver $resolver) { parent::configureOptions($resolver);, (*12)

    $resolver->setDefaults([
        'format' => 'dd.MM.yyyy',
        'date_format' => 'DD.MM.YYYY',
        'config_locale' => [
            'en' => [
                'week' => ['dow' => 1],
            ],
        ],
    ]);
}

public static function getExtendedTypes(): iterable
{
    return [DateType::class];
}

}, (*13)


## Collections This bundle provides JS functionality needed to add, edit and delete elements of the collection. By default, newly added items are prefixed with `new_` and random number e.g. `new_16782845986841`. But this can cause mistakes in server-side validation. From version **5.2.** is possible define `data_index` with `true` or `numeric` value in collection definition to count items in collection from 0...N. If collection can be loaded with existing data, number of items should be specified in `data_index` value. ``` php <?php public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('collection', CollectionType::class, [ 'data_index' => $entity->getCollection()->count() ]); }

Form extensions

Form theme

This extensions allows you to set form theme through the type\'s options., (*14)

``` php <?php, (*15)

class ExampleType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { // example: setting template of child form (field) $builder->add('example', null, [ // override form theme template 'template' => 'MyBundle:Form:example_theme.html.twig',, (*16)

        // pass extra variables to the theme templates when this field is rendered
        'template_parameters' => [
            'foo' => 'bar',
        ],
    ]);
}

public function configureOptions(OptionsResolver $resolver)
{
    // example: setting template of the form type itself
    $resolver->setDefaults([
        'template' => 'MyBundle:Form:example_theme.html.twig',
        'template_parameters' => ['foo' => 'bar'],
    ]);
}

}, (*17)


## Validator constraints ### Number - ensures that number have correct precision and scale #### options - precision - scale ### Latitude ### Longtitude ### NotNullGroup This class-level constraint ensures that all given properties are either set or null. Valid states: all properties are null, all properties are NOT null. ### NotNullOneOf This class-level constraint ensures that at least one of the given properties is NOT null. Valid states: at least one property is NOT null ### Example ``` php <?php use Imatic\Bundle\FormBundle\Validator\Constraints as ImaticAssert; /** * Evidence * * @ORM\Entity * @ImaticAssert\NotNullGroup(properties={"sitterFirstName", "sitterLastName", "sitterId", "sitterPhone", "sitterRelation"}) * @ImaticAssert\NotNullOneOf(properties={"mother", "father"}, message="Either the mother or the father information must be specified.") */ class Evidence { // ... }

Data transformers

EmptyEntityToNullTransformer

This transformers converts an entity object to null, if it is considered empty. The check is performed based on list of properties that are to be verified., (*18)

If strict mode is disabled (default), both nulls and empty strings are considered empty., (*19)

If strict mode is enabled, only nulls are considered empty., (*20)

``` php <?php, (*21)

use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Imatic\Bundle\FormBundle\Form\DataTransformer\EmptyEntityToNullTransformer;, (*22)

/** * Address type */ class AddressType extends AbstractType { /** * @param FormBuilderInterface $builder * @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('street') ->add('number') ->add('city') ->add('postalCode', 'text') ;, (*23)

    $builder->addModelTransformer(new EmptyEntityToNullTransformer(
        array_keys($builder->all())
    ));
}

// ...

} ```, (*24)

The Versions

17/03 2016

3.0.6

3.0.6.0

Imatic Form Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

15/03 2016

3.0.5

3.0.5.0

Imatic Form Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

04/03 2016

3.0.4

3.0.4.0

Imatic Form Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

28/02 2016

3.0.3

3.0.3.0

Imatic Form Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

28/02 2016

3.0.2

3.0.2.0

Imatic Form Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires

28/02 2016

3.0.1

3.0.1.0

Imatic Form Bundle

  Sources   Download

proprietary

The Requires

 

The Development Requires