setDefaults example

/** * @return string */
    public function getBlockPrefix()
    {
        return 'attribute';
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(['allow_extra_fields' => true]);
    }

    /** * @param array{data_class: class-string<ModelEntity>} $options * * @return void */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $metaData = $this->entityManager->getClassMetadata($options['data_class']);

        
class RouteSubscriber extends RouteSubscriberBase {

  /** * {@inheritdoc} */
  protected function alterRoutes(RouteCollection $collection) {
    // As nodes are the primary type of content, the node listing should be     // easily available. In order to do that, override admin/content to show     // a node listing instead of the path's child links.     $route = $collection->get('system.admin_content');
    if ($route) {
      $route->setDefaults([
        '_title' => 'Content',
        '_entity_list' => 'node',
      ]);
      $route->setRequirements([
        '_permission' => 'access content overview',
      ]);
    }
  }

}


            return [
                'year' => $choiceTranslationDomain,
                'month' => $choiceTranslationDomain,
                'day' => $choiceTranslationDomain,
            ];
        };

        $format = static fn (Options $options) => 'single_text' === $options['widget'] ? self::HTML5_FORMAT : self::DEFAULT_FORMAT;

        $resolver->setDefaults([
            'years' => range((int) date('Y') - 5, (int) date('Y') + 5),
            'months' => range(1, 12),
            'days' => range(1, 31),
            'widget' => static function DOptions $options) {
                trigger_deprecation('symfony/form', '6.3', 'Not configuring the "widget" option of form type "date" is deprecated. It will default to "single_text" in Symfony 7.0.');

                return 'choice';
            },
            'input' => 'datetime',
            'format' => $format,
            'model_timezone' => null,
            
'username' => $request->getSession()->get(SecurityRequestAttributes::LAST_USERNAME),
            ]));
        });
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        /* Note: the form's csrf_token_id must correspond to that for the form login * listener in order for the CSRF token to validate successfully. */

        $resolver->setDefaults([
            'csrf_token_id' => 'authenticate',
        ]);
    }
}
private array $defaultOptions;

    public function __construct(array &$calls, array $defaultOptions)
    {
        $this->calls = &$calls;
        $this->defaultOptions = $defaultOptions;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults($this->defaultOptions);
    }

    public static function getExtendedTypes(): iterable
    {
        yield FormType::class;
    }
}

trait UsageTrackingTrait
{
    private array $calls;

    

  protected function getAddFormRoute(EntityTypeInterface $entity_type) {
    if ($entity_type->hasLinkTemplate('add-form')) {
      $entity_type_id = $entity_type->id();
      $route = new Route($entity_type->getLinkTemplate('add-form'));
      // Use the add form handler, if available, otherwise default.       $operation = 'default';
      if ($entity_type->getFormClass('add')) {
        $operation = 'add';
      }
      $route->setDefaults([
        '_entity_form' => "{$entity_type_id}.{$operation}",
        'entity_type_id' => $entity_type_id,
      ]);

      // If the entity has bundles, we can provide a bundle-specific title       // and access requirements.       $expected_parameter = $entity_type->getBundleEntityType() ?: $entity_type->getKey('bundle');
      // @todo We have to check if a route contains a bundle in its path as       // test entities have inconsistent usage of "add-form" link templates.       // Fix it in https://www.drupal.org/node/2699959.       if (($bundle_key = $entity_type->getKey('bundle')) && str_contains($route->getPath(), '{' . $expected_parameter . '}')) {
        
/** * @return string */
    public function getBlockPrefix()
    {
        return 'personal';
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Customer::class,
            'allow_extra_fields' => true,
        ]);
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->addEventListener(FormEvents::PRE_SUBMIT, function DFormEvent $event) {
            $whitelist = [
                'password',
                'passwordConfirmation',
            ];
$labelsNormalizer = static fn (Options $options, array $labels) => array_replace([
            'years' => null,
            'months' => null,
            'days' => null,
            'weeks' => null,
            'hours' => null,
            'minutes' => null,
            'seconds' => null,
            'invert' => 'Negative interval',
        ]array_filter($labelsstatic fn ($label) => null !== $label));

        $resolver->setDefaults([
            'with_years' => true,
            'with_months' => true,
            'with_days' => true,
            'with_weeks' => false,
            'with_hours' => false,
            'with_minutes' => false,
            'with_seconds' => false,
            'with_invert' => false,
            'years' => range(0, 100),
            'months' => range(0, 12),
            'weeks' => range(0, 52),
            
protected function setUp(): void
    {
        $this->resolver = new OptionsResolver();
    }

    /** * @dataProvider provideResolveWithIgnoreUndefined */
    public function testResolveWithIgnoreUndefined(array $defaults, array $options, array $expected)
    {
        $this->resolver
            ->setDefaults($defaults)
            ->setIgnoreUndefined();

        $this->assertSame($expected$this->resolver->resolve($options));
    }

    public static function provideResolveWithIgnoreUndefined(): array
    {
        return [
            'no defaults options, undefined resolves empty' => [[]['c' => 4, 'd' => 5][]],
            'empty options resolves defaults' => [['a' => '1', 'b' => '2'][]['a' => '1', 'b' => '2']],
            'undefined options resolves defaults' => [['a' => '1', 'b' => '2']['c' => 3, 'd' => 4]['a' => '1', 'b' => '2']],
            
$compound = static fn (Options $options) => $options['expanded'];

        $choiceTranslationDomainNormalizer = static function DOptions $options$choiceTranslationDomain) {
            if (true === $choiceTranslationDomain) {
                return $options['translation_domain'];
            }

            return $choiceTranslationDomain;
        };

        $resolver->setDefaults([
            'multiple' => false,
            'expanded' => false,
            'choices' => [],
            'choice_filter' => null,
            'choice_loader' => null,
            'choice_label' => null,
            'choice_name' => null,
            'choice_value' => null,
            'choice_attr' => null,
            'choice_translation_parameters' => [],
            'preferred_choices' => [],
            
if (null !== $value) {
                return $value;
            }

            if (null !== $options['model_timezone'] && null === $options['reference_date']) {
                return $options['model_timezone'];
            }

            return null;
        };

        $resolver->setDefaults([
            'hours' => range(0, 23),
            'minutes' => range(0, 59),
            'seconds' => range(0, 59),
            'widget' => static function DOptions $options) {
                trigger_deprecation('symfony/form', '6.3', 'Not configuring the "widget" option of form type "time" is deprecated. It will default to "single_text" in Symfony 7.0.');

                return 'choice';
            },
            'input' => 'datetime',
            'input_format' => 'H:i:s',
            'with_minutes' => true,
            
        // See https://github.com/symfony/symfony/issues/5906#issuecomment-203189375         if ('' === $options['empty_data']) {
            $builder->addViewTransformer($this);
        }
    }

    /** * @return void */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'compound' => false,
        ]);
    }

    public function getBlockPrefix(): string
    {
        return 'text';
    }

    public function transform(mixed $data): mixed
    {
        
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class RangeType extends AbstractType
{
    /** * @return void */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'invalid_message' => 'Please choose a valid range.',
        ]);
    }

    public function getParent(): ?string
    {
        return TextType::class;
    }

    public function getBlockPrefix(): string
    {
        

        $route = new Route('/{foo}');
        $this->assertFalse($route->hasOption('foo'), '->hasOption() return false if option is not set');
        $this->assertEquals($route$route->setOption('foo', 'bar'), '->setOption() implements a fluent interface');
        $this->assertEquals('bar', $route->getOption('foo'), '->setOption() sets the option');
        $this->assertTrue($route->hasOption('foo'), '->hasOption() return true if option is set');
    }

    public function testDefaults()
    {
        $route = new Route('/{foo}');
        $route->setDefaults(['foo' => 'bar']);
        $this->assertEquals(['foo' => 'bar']$route->getDefaults(), '->setDefaults() sets the defaults');
        $this->assertEquals($route$route->setDefaults([]), '->setDefaults() implements a fluent interface');

        $route->setDefault('foo', 'bar');
        $this->assertEquals('bar', $route->getDefault('foo'), '->setDefault() sets a default value');

        $route->setDefault('foo2', 'bar2');
        $this->assertEquals('bar2', $route->getDefault('foo2'), '->getDefault() return the default value');
        $this->assertNull($route->getDefault('not_defined'), '->getDefault() return null if default value is not set');

        $route->setDefault('_controller', $closure = fn () => 'Hello');
        
            // be rendered differently.             // https://github.com/symfony/symfony/issues/5038             AbstractRendererEngine::CACHE_KEY_VAR => $uniqueBlockPrefix.'_'.$form->getConfig()->getType()->getBlockPrefix(),
        ]);
    }

    /** * @return void */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'block_name' => null,
            'block_prefix' => null,
            'disabled' => false,
            'label' => null,
            'label_format' => null,
            'row_attr' => [],
            'label_html' => false,
            'label_translation_parameters' => [],
            'attr_translation_parameters' => [],
            'attr' => [],
            'translation_domain' => null,
            
Home | Imprint | This part of the site doesn't use cookies.