Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony 一个接一个地验证日期_Symfony - Fatal编程技术网

Symfony 一个接一个地验证日期

Symfony 一个接一个地验证日期,symfony,Symfony,我有一个表单,用户输入3个日期时间,我想检查第二个日期是否在第一个之后,第三个日期是否在第二个之后。怎么做?应该在以后做 if ($form->isValid()) { (就在数据库更新之前)或以某种方式使用验证组(我不熟悉symfony2)?如果表单是通过映射要验证的三个日期字段的对象填充的, 然后你可以考虑使用a, 首先,添加一个类约束,如下所示: namespace Namespace\BundleName\Validator\Constraints; use Symfony\C

我有一个表单,用户输入3个日期时间,我想检查第二个日期是否在第一个之后,第三个日期是否在第二个之后。怎么做?应该在以后做

if ($form->isValid()) {

(就在数据库更新之前)或以某种方式使用验证组(我不熟悉symfony2)?

如果表单是通过映射要验证的三个日期字段的对象填充的, 然后你可以考虑使用a,

首先,添加一个类约束,如下所示:

namespace Namespace\BundleName\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
 * @Annotation
 */
class DateCoherence extends Constraint
{
    public $message = 'The error message you want to show when the validation fails.';

    public function getTargets()
    {
        return self::CLASS_CONSTRAINT;
    }

    public function validatedBy()
    {
        return get_class($this).'Validator'; // You can also use an alias here
    }
}
验证器

namespace Namespace\BundleName\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class DateCoherenceValidatorValidator extends ConstraintValidator
{
    public function validate($object, Constraint $constraint)
    {
        if ($object->getFirstDate() > $object->getSecondDate() || $object->getSecondDate() > $object->getThirdDate()) {
            $this->context->addViolation($constraint->message);
        }
    }
}
然后,应通过

use Namespace\BundleName\Validator\Constraints as YourAssert;

/**
 * @YourAssert\DateCoherence(groups={"dates"})
 */
class YourClass
{
在此处添加验证组只允许您在任何地方根据约束日期检查验证。(例如,在控制器内)


如果表单是通过映射要验证的三个日期字段的对象填充的, 然后你可以考虑使用a,

首先,添加一个类约束,如下所示:

namespace Namespace\BundleName\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
 * @Annotation
 */
class DateCoherence extends Constraint
{
    public $message = 'The error message you want to show when the validation fails.';

    public function getTargets()
    {
        return self::CLASS_CONSTRAINT;
    }

    public function validatedBy()
    {
        return get_class($this).'Validator'; // You can also use an alias here
    }
}
验证器

namespace Namespace\BundleName\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class DateCoherenceValidatorValidator extends ConstraintValidator
{
    public function validate($object, Constraint $constraint)
    {
        if ($object->getFirstDate() > $object->getSecondDate() || $object->getSecondDate() > $object->getThirdDate()) {
            $this->context->addViolation($constraint->message);
        }
    }
}
然后,应通过

use Namespace\BundleName\Validator\Constraints as YourAssert;

/**
 * @YourAssert\DateCoherence(groups={"dates"})
 */
class YourClass
{
在此处添加验证组只允许您在任何地方根据约束日期检查验证。(例如,在控制器内)


如果表单是通过映射要验证的三个日期字段的对象填充的, 然后你可以考虑使用a,

首先,添加一个类约束,如下所示:

namespace Namespace\BundleName\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
 * @Annotation
 */
class DateCoherence extends Constraint
{
    public $message = 'The error message you want to show when the validation fails.';

    public function getTargets()
    {
        return self::CLASS_CONSTRAINT;
    }

    public function validatedBy()
    {
        return get_class($this).'Validator'; // You can also use an alias here
    }
}
验证器

namespace Namespace\BundleName\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class DateCoherenceValidatorValidator extends ConstraintValidator
{
    public function validate($object, Constraint $constraint)
    {
        if ($object->getFirstDate() > $object->getSecondDate() || $object->getSecondDate() > $object->getThirdDate()) {
            $this->context->addViolation($constraint->message);
        }
    }
}
然后,应通过

use Namespace\BundleName\Validator\Constraints as YourAssert;

/**
 * @YourAssert\DateCoherence(groups={"dates"})
 */
class YourClass
{
在此处添加验证组只允许您在任何地方根据约束日期检查验证。(例如,在控制器内)


如果表单是通过映射要验证的三个日期字段的对象填充的, 然后你可以考虑使用a,

首先,添加一个类约束,如下所示:

namespace Namespace\BundleName\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
 * @Annotation
 */
class DateCoherence extends Constraint
{
    public $message = 'The error message you want to show when the validation fails.';

    public function getTargets()
    {
        return self::CLASS_CONSTRAINT;
    }

    public function validatedBy()
    {
        return get_class($this).'Validator'; // You can also use an alias here
    }
}
验证器

namespace Namespace\BundleName\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class DateCoherenceValidatorValidator extends ConstraintValidator
{
    public function validate($object, Constraint $constraint)
    {
        if ($object->getFirstDate() > $object->getSecondDate() || $object->getSecondDate() > $object->getThirdDate()) {
            $this->context->addViolation($constraint->message);
        }
    }
}
然后,应通过

use Namespace\BundleName\Validator\Constraints as YourAssert;

/**
 * @YourAssert\DateCoherence(groups={"dates"})
 */
class YourClass
{
在此处添加验证组只允许您在任何地方根据约束日期检查验证。(例如,在控制器内)


构建自己的约束是一个很好的实践,因为验证器约束可以重用。如果您只需要在其中一个实体中进行此类验证,也可以执行以下操作:

yml:

在您的实体中:

public function isDatesOrderValid()
{
    // Compare dates (probably in datetime) here and return true if they are in valid order, otherwise return false
}

构建自己的约束是一个很好的实践,因为验证器约束可以重用。如果您只需要在其中一个实体中进行此类验证,也可以执行以下操作:

yml:

在您的实体中:

public function isDatesOrderValid()
{
    // Compare dates (probably in datetime) here and return true if they are in valid order, otherwise return false
}

构建自己的约束是一个很好的实践,因为验证器约束可以重用。如果您只需要在其中一个实体中进行此类验证,也可以执行以下操作:

yml:

在您的实体中:

public function isDatesOrderValid()
{
    // Compare dates (probably in datetime) here and return true if they are in valid order, otherwise return false
}

构建自己的约束是一个很好的实践,因为验证器约束可以重用。如果您只需要在其中一个实体中进行此类验证,也可以执行以下操作:

yml:

在您的实体中:

public function isDatesOrderValid()
{
    // Compare dates (probably in datetime) here and return true if they are in valid order, otherwise return false
}

谢谢,这正是我需要的!谢谢,这正是我需要的!谢谢,这正是我需要的!谢谢,这正是我需要的!