-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Conversation
What is the advantage of this over #11673? Isn't it basically the same use-case? |
Hi @bezhermoso, thank you for submitting that PR! :) You can already use the /**
* @Assert\Expression("this.startDate < this.endDate")
*/
class Event
{
private $startDate;
private $endDate;
} Hope this helps! :) |
@webmozart Wow its amazing what the I'm curious: how about enforcing the interval between dates? |
Using the appropriate /**
* @Assert\Expression("this.startDate.modify('+1 month') < this.endDate")
*/ |
@webmozart I would assume |
@bezhermoso Ah right, you'd need a /**
* @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!');
}
} |
@webmozart Gotcha! That works, too. Thanks! |
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)min
andmax
interval constraintsUse-case
This validator asserts that
startDate
andendDate
must constitute to a valid date-range. This validator ignores if one of them isnull
(addNotBlank
to the related fields if required)Enforce that dates are at least 1 month apart:
Enforce that dates can only be 1 week apart or less: