Skip to content

Commit

Permalink
feature #29887 [Form] Add input_format option to DateType and DateTim…
Browse files Browse the repository at this point in the history
…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 5c73900
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Expand Up @@ -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
-----
Expand Down
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -317,6 +318,8 @@ public function configureOptions(OptionsResolver $resolver)

return '';
});

$resolver->setAllowedTypes('input_format', 'string');
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Component/Form/Extension/Core/Type/DateType.php
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand All @@ -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');
}

/**
Expand Down
Expand Up @@ -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());
}
}
Expand Up @@ -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.