Skip to content
Permalink
Browse files

feature #29887 [Form] Add input_format option to DateType and DateTim…

…eType (fancyweb)

This PR was merged into the 4.3-dev branch.

Discussion
----------

[Form] Add input_format option to DateType and DateTimeType

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #29883
| License       | MIT
| Doc PR        | symfony/symfony-docs#10882

Add a new option to specify the date format when using the `string` input type.

Commits
-------

c8240a0 [Form] Add input_format option to DateType and DateTimeType
  • Loading branch information...
xabbuh committed Feb 20, 2019
2 parents c3cf08e + c8240a0 commit 5c7390006b650ed847ebd7780a2fd18ce9163ad0
@@ -32,6 +32,7 @@ CHANGELOG
}
}
```
* added new option `input_format` to `DateType` and `DateTimeType` to specify the date format when using the `string` input.

4.2.0
-----
@@ -176,7 +176,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$builder->addModelTransformer(new DateTimeImmutableToDateTimeTransformer());
} elseif ('string' === $options['input']) {
$builder->addModelTransformer(new ReversedTransformer(
new DateTimeToStringTransformer($options['model_timezone'], $options['model_timezone'])
new DateTimeToStringTransformer($options['model_timezone'], $options['model_timezone'], $options['input_format'])
));
} elseif ('timestamp' === $options['input']) {
$builder->addModelTransformer(new ReversedTransformer(
@@ -251,6 +251,7 @@ public function configureOptions(OptionsResolver $resolver)
'empty_data' => function (Options $options) {
return $options['compound'] ? [] : '';
},
'input_format' => 'Y-m-d H:i:s',
]);
// Don't add some defaults in order to preserve the defaults
@@ -317,6 +318,8 @@ public function configureOptions(OptionsResolver $resolver)
return '';
});
$resolver->setAllowedTypes('input_format', 'string');
}
/**
@@ -154,7 +154,7 @@ class_exists('IntlTimeZone', false) ? \IntlTimeZone::createDefault() : null,
$builder->addModelTransformer(new DateTimeImmutableToDateTimeTransformer());
} elseif ('string' === $options['input']) {
$builder->addModelTransformer(new ReversedTransformer(
new DateTimeToStringTransformer($options['model_timezone'], $options['model_timezone'], 'Y-m-d')
new DateTimeToStringTransformer($options['model_timezone'], $options['model_timezone'], $options['input_format'])
));
} elseif ('timestamp' === $options['input']) {
$builder->addModelTransformer(new ReversedTransformer(
@@ -283,6 +283,7 @@ public function configureOptions(OptionsResolver $resolver)
return $options['compound'] ? [] : '';
},
'choice_translation_domain' => false,
'input_format' => 'Y-m-d',
]);
$resolver->setNormalizer('placeholder', $placeholderNormalizer);
@@ -305,6 +306,8 @@ public function configureOptions(OptionsResolver $resolver)
$resolver->setAllowedTypes('years', 'array');
$resolver->setAllowedTypes('months', 'array');
$resolver->setAllowedTypes('days', 'array');
$resolver->setAllowedTypes('input_format', 'string');
}
/**
@@ -683,4 +683,19 @@ public function provideEmptyData()
'Compound choice field' => ['choice', ['date' => ['year' => '2018', 'month' => '11', 'day' => '11'], 'time' => ['hour' => '21', 'minute' => '23']], $expectedData],
];
}
public function testSubmitStringWithCustomInputFormat(): void
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
'model_timezone' => 'UTC',
'view_timezone' => 'UTC',
'input' => 'string',
'widget' => 'single_text',
'input_format' => 'd/m/Y H:i:s P',
]);
$form->submit('2018-01-14T21:29:00');
$this->assertSame('14/01/2018 21:29:00 +00:00', $form->getData());
}
}
@@ -1037,4 +1037,19 @@ public function provideEmptyData()
'Compound choice fields' => ['choice', ['year' => '2018', 'month' => '11', 'day' => '11'], $expectedData],
];
}
public function testSubmitStringWithCustomInputFormat(): void
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
'model_timezone' => 'UTC',
'view_timezone' => 'UTC',
'widget' => 'single_text',
'input' => 'string',
'input_format' => 'd/m/Y',
]);
$form->submit('2018-01-14');
$this->assertSame('14/01/2018', $form->getData());
}
}

0 comments on commit 5c73900

Please sign in to comment.
You can’t perform that action at this time.