Skip to content

[Validator] DateRange class validator. #11681

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

Closed
wants to merge 3 commits into from

Conversation

bezhermoso
Copy link

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets NA
License MIT
Doc PR (symfony/symfony-docs#4123)

A validator that checks two DateTime values in a class and validates that its a valid range (start date is lesser than/equal to end date)

  • 2.3 API
  • 2.5 API
  • Add min and max interval constraints

Use-case

/**
 * @Assert\DateRange(start="startDate", end="endDate")
 */
class Event
{
      /**
       * @var DateTime
       */
      private $startDate;

      /**
       * @var DateTime
       */
      private $endDate;
}

This validator asserts that startDate and endDate must constitute to a valid date-range. This validator ignores if one of them is null (add NotBlankto the related fields if required)

Enforce that dates are at least 1 month apart:

/**
 * @Assert\DateRange(start="startDate", end="endDate", min="1 month")
 */

Enforce that dates can only be 1 week apart or less:

/**
 * @Assert\DateRange(start="startDate", end="endDate", max="1 week")
 */

@bezhermoso bezhermoso changed the title [Validator] Date range class validator. [Validator] DateRange class validator. Aug 15, 2014
@xabbuh
Copy link
Member

xabbuh commented Aug 15, 2014

What is the advantage of this over #11673? Isn't it basically the same use-case?

@bezhermoso
Copy link
Author

@xabbuh This validates against two date fields that are both user supplied. I just looked at #11673 right now and don't see a way to make the date to check against dependent on another field, unless I missed it?

I added a use-case in the description.

@webmozart
Copy link
Contributor

Hi @bezhermoso, thank you for submitting that PR! :)

You can already use the Expression constraint to solve your use case:

/**
 * @Assert\Expression("this.startDate < this.endDate")
 */
class Event
{
      private $startDate;
      private $endDate;
}

Hope this helps! :)

@webmozart webmozart closed this Aug 15, 2014
@bezhermoso
Copy link
Author

@webmozart Wow its amazing what the ExpressionLanguage component can do! :)

I'm curious: how about enforcing the interval between dates?

@webmozart
Copy link
Contributor

Using the appropriate DateTime methods should work:

/**
 * @Assert\Expression("this.startDate.modify('+1 month') < this.endDate")
 */

@bezhermoso
Copy link
Author

@webmozart I would assume ExpressionLanguage would not actually alter the value within the object? AFAIK DateTime#modify will change the object, not return a new one.

@webmozart
Copy link
Contributor

@bezhermoso Ah right, you'd need a DateTimeImmutable for that. Well then you can also use a Callback:

/**
 * @Assert\Callback
 */
public function validateDateRange(ExecutionContextInterface $context)
{
    $startDate = clone $this->startDate;

    if ($this->endDate < $startDate->modify('+1 month')) {
        $context->addViolation('The end date is too soon!');
    }
}

@bezhermoso
Copy link
Author

@webmozart Gotcha! That works, too. Thanks!

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

Successfully merging this pull request may close these issues.

3 participants