Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Validator] Added a format option to the DateTime constraint. #17553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 1, 2016
Merged

[Validator] Added a format option to the DateTime constraint. #17553

merged 1 commit into from
Mar 1, 2016

Conversation

dosten
Copy link
Contributor

@dosten dosten commented Jan 27, 2016

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? yes
Tests pass? yes
Fixed tickets #14521
License MIT

This PR adds a format option to the DateTime constraint, this allows to validate dates in custom formats, for example:

use Symfony\Component\Validator\Constraints\DateTime;
use Symfony\Component\Validator\Validation;

$validator = Validation::createValidator();

$validator->validate('December 31, 1999', new DateTime(['format' => 'F d, Y']));
$validator->validate('01:02:03', new DateTime(['format' => 'H:i:s']));
$validator->validate('2010/01/01 01:02', new DateTime(['format' => 'Y/m/d H:i']));

As you can see this new option allows to use the DateTime constraint to validate dates and times, so, maybe the Date and Time constraints can be deprecated in this PR.

->setParameter('{{ value }}', $this->formatValue($value))
->setCode(DateTime::INVALID_DATE_ERROR)
->addViolation();
} else if ('The parsed time was invalid' === $warning) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

elseif

@fabpot
Copy link
Member

fabpot commented Jan 27, 2016

I'm 👍 for this addition, but I made several comments about the implementation.

@dosten
Copy link
Contributor Author

dosten commented Jan 27, 2016

@fabpot Comments addressed

@dosten
Copy link
Contributor Author

dosten commented Jan 27, 2016

The failing tests seems to not be related.

Fatal error: Call to undefined method Symfony\Component\DependencyInjection\Definition::getParent() in /home/travis/build/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php on line 535

3.1.0
-----

* deprecated `DateTimeValidator::PATTERN` constant
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

must be added to the upgrade file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@xabbuh
Copy link
Member

xabbuh commented Jan 30, 2016

Can we add a check to the constraint that ensures that the format string is something that is understood by DateTime::createFormFormat()?

@xabbuh
Copy link
Member

xabbuh commented Feb 10, 2016

@dosten Can you make the missing changes?

@dosten
Copy link
Contributor Author

dosten commented Feb 10, 2016

@xabbuh I'll work on this ASAP

@dosten
Copy link
Contributor Author

dosten commented Feb 15, 2016

@xabbuh The DateTime::createFromFormat() method can return false when the given string does not match the format, but this is already covered by this statement: https://github.com/dosten/symfony/blob/validator/add-datetime-format/src/Symfony/Component/Validator/Constraints/DateTimeValidator.php#L51-L58
Also, the format can be anything, for example this code is valid: DateTime::createFromFormat('foo', 'foo'), so, any string is understood by DateTime::createFromFormat()

@HeahDude
Copy link
Contributor

Thanks for that feature 👍

@dosten
Copy link
Contributor Author

dosten commented Feb 26, 2016

Some feedback? @xabbuh @webmozart @fabpot

@xabbuh
Copy link
Member

xabbuh commented Feb 27, 2016

@dosten But wouldn't be better to use a different error code if the format string cannot be parsed?

@dosten
Copy link
Contributor Author

dosten commented Feb 27, 2016

@xabbuh can you give an example of a "unparsable" format?

@xabbuh
Copy link
Member

xabbuh commented Feb 28, 2016

@dosten Take this example:

$date = \DateTime::createFromFormat('f.m.Y', '27.02.2016');

\DateTime::getLastErrors() will now contain something like this:

Array (
    [warning_count] => 0
    [warnings] => Array
        (
        )

    [error_count] => 3
    [errors] => Array
        (
            [0] => The format separator does not match
            [1] => The separation symbol could not be found
            [5] => Trailing data
        )

)

see https://3v4l.org/tTAvU

@fabpot
Copy link
Member

fabpot commented Feb 29, 2016

@dosten Any news on this one?

@dosten
Copy link
Contributor Author

dosten commented Feb 29, 2016

@fabpot @xabbuh The "f" format char does not exists, so your code is expecting a format like: "f.02.2016":

$date = \DateTime::createFromFormat('f.m.Y', 'f.02.2016');

\DateTime::getLastErrors() will now contain something like this:

Array
(
    [warning_count] => 0
    [warnings] => Array
        (
        )

    [error_count] => 0
    [errors] => Array
        (
        )

)

If you pass "27.02.2016" the validator will add a INVALID_FORMAT_ERROR violation, IMO this is correct.
I can add a test case for this to prove that this works correctly.

@xabbuh
Copy link
Member

xabbuh commented Mar 1, 2016

Ah yes, you are right. I missed that you have to use these characters if they are present in the user input. Sorry for the confusion.

👍

Status: Reviewed

@fabpot
Copy link
Member

fabpot commented Mar 1, 2016

Thank you @dosten.

@fabpot fabpot merged commit 9e94c9f into symfony:master Mar 1, 2016
fabpot added a commit that referenced this pull request Mar 1, 2016
…traint. (dosten)

This PR was merged into the 3.1-dev branch.

Discussion
----------

[Validator] Added a format option to the DateTime constraint.

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | #14521
| License       | MIT

This PR adds a `format` option to the `DateTime` constraint, this allows to validate dates in custom formats, for example:

```php
use Symfony\Component\Validator\Constraints\DateTime;
use Symfony\Component\Validator\Validation;

$validator = Validation::createValidator();

$validator->validate('December 31, 1999', new DateTime(['format' => 'F d, Y']));
$validator->validate('01:02:03', new DateTime(['format' => 'H:i:s']));
$validator->validate('2010/01/01 01:02', new DateTime(['format' => 'Y/m/d H:i']));
```

As you can see this new option allows to use the `DateTime` constraint to validate dates and times, so, maybe the `Date` and `Time` constraints can be deprecated in this PR.

Commits
-------

9e94c9f Added a format option to the DateTime constraint.
@fabpot
Copy link
Member

fabpot commented Mar 1, 2016

@dosten Can you submit a PR on symfony/symfony-docs to document this new feature? Thanks.

@xabbuh
Copy link
Member

xabbuh commented Mar 1, 2016

@dosten You can refer to symfony/symfony-docs#6313 (I created it so that we do not forget about this feature).

@dosten dosten deleted the validator/add-datetime-format branch March 1, 2016 16:25
wouterj added a commit to symfony/symfony-docs that referenced this pull request Mar 5, 2016
This PR was merged into the master branch.

Discussion
----------

[Validator] Added docs about the format option

| Q             | A
| ------------- | ---
| Doc fix?      | no
| New docs?     | yes, see symfony/symfony#17553
| Applies to    | 3.1+
| Fixed tickets | #6313

Commits
-------

f8d1e82 Added docs about the format option
@felipsmartins
Copy link

Nice feature, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants