Php Symfony2验证日期时间1应早于日期时间2
我正在查看Symfony2验证参考,但没有找到我需要的 我在班上的工作时间是开始日期和结束日期。 我想添加一个\@Assert(),用于验证StartDate始终在EndDate之前。 是否有比较类属性作为验证约束的标准方法,还是应该创建自定义验证约束Php Symfony2验证日期时间1应早于日期时间2,php,symfony,validation,doctrine-orm,assert,Php,Symfony,Validation,Doctrine Orm,Assert,我正在查看Symfony2验证参考,但没有找到我需要的 我在班上的工作时间是开始日期和结束日期。 我想添加一个\@Assert(),用于验证StartDate始终在EndDate之前。 是否有比较类属性作为验证约束的标准方法,还是应该创建自定义验证约束 class Employment { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO")
class Employment {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
* @Expose()
*/
protected $id;
/**
* @ORM\Column(type="datetime")
* @Expose()
* @Assert\DateTime()
*/
protected $startDate;
/**
* @ORM\Column(type="datetime", nullable=TRUE)
* @Expose()
* @Assert\DateTime()
*/
protected $endDate;
...
}
您可以编写自定义DateRangeValidator
class DateRange extends Constraint {
public $message = "daterange.violation.crossing";
public $emptyStartDate = "daterange.violation.startDate";
public $emptyEndDate = "daterange.violation.endDate";
public $hasEndDate = true;
public function getTargets() {
return self::CLASS_CONSTRAINT;
}
public function validatedBy() {
return 'daterange_validator';
}
}
class DateRangeValidator extends ConstraintValidator
{
public function isValid($entity, Constraint $constraint)
{
$hasEndDate = true;
if ($constraint->hasEndDate !== null) {
$hasEndDate = $constraint->hasEndDate;
}
if ($entity->getStartDate() !== null) {
if ($hasEndDate) {
if ($entity->getEndDate() !== null) {
if ($entity->getStartDate() > $entity->getEndDate()) {
$this->setMessage($constraint->message);
return false;
}
return true;
} else {
$this->setMessage($constraint->emptyEndDate);
return false;
}
} else {
if ($entity->getEndDate() !== null) {
if ($entity->getStartDate() > $entity->getEndDate()) {
$this->setMessage($constraint->message);
return false;
}
}
return true;
}
} else {
$this->setMessage($constraint->emptyStartDate);
return false;
}
}
将其注册为服务:
parameters:
register.daterange.validator.class: XXX\FormExtensionsBundle\Validator\Constraints\DateRangeValidator
services:
daterange.validator:
class: %register.daterange.validator.class%
tags:
- { name: validator.constraint_validator, alias: daterange_validator }
并在您的实体中使用它:
use XXX\FormExtensionsBundle\Validator\Constraints as FormAssert;
/**
*
* @FormAssert\DateRange()
*/
class Contact extends Entity
{
private $startDate;
private $endDate;
}
尽管对于这样一件简单的事情来说似乎有点过分,但经验表明,一个人需要一个日期范围验证器的次数要多于一次。您可以向实体添加一个验证getter- 在你的验证中
Acme\YourBundle\Entity\Employment:
getters:
datesValid:
- "True": { message: "The start date must be before the end date" }
然后在你的实体中
public function isDatesValid()
{
return ($this->startDate < $this->endDate);
}
公共函数isDatesValid()
{
返回($this->startDate<$this->endDate);
}
还有另一种解决方案:
使用Symfony\Component\Validator\Constraints作为断言;
/**
*@ORM\Column(type=“date”,nullable=true)
*@Assert\Date()
*/
私人$startDate;
/**
*@ORM\Column(type=“date”,nullable=true)
*@Assert\Date()
*@Assert\Expression(
*“this.getStartDate()
如果需要,您可以将此约束添加到
$startDate
。谢谢您提醒我此方法和可能性。由于可重用性,我接受了另一个答案。是的,我也更喜欢另一个。代码更少,维护更少。谢谢你的简单解决方案。@Simon,请向我解释一下你这里所说的可重用性是什么意思,非常感谢advance@whitelettersinblankpapers与公认答案相反,该答案只能在该实体中使用;如果要对其他实体使用相同的验证器,则必须为其他实体再次编写相同的代码。这是违反干燥原则的。接受的答案将验证器注册为一项服务,这样它就可以很容易地在其他实体之间重用和共享。我知道这已经很旧了,但我一直收到有关它的通知,因此这可能对某些人有所帮助。现在,PUGXExtraValidatorBundle
()中有了这个约束的预构建版本(可能以前有过,但我不记得了)。dev和symfony中新增了$constraint->hasEndDate?$constraint中是否有hasEndDate方法?或者我应该在我的实体中创建它?回信在哪里?感谢您提供此解决方案。@BenjaminLucas我在上面的代码中添加了缺少的字段。谢谢你的提示。我应该用什么来使用你建议的这门课?这个答案更简单,更适合这个要求。
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Column(type="date", nullable=true)
* @Assert\Date()
*/
private $startDate;
/**
* @ORM\Column(type="date", nullable=true)
* @Assert\Date()
* @Assert\Expression(
* "this.getStartDate() < this.getEndDate()",
* message="The end date must be after the start date"
* )
*/
private $endDate;