hoppinger/validation

为您的Symfony2 (Validator)项目提供多个验证约束

dev-master 2012-12-17 19:33 UTC

This package is not auto-updated.

Last update: 2024-09-14 13:21:21 UTC


README

为您的Symfony2 (Validator)项目提供多个验证约束。

目前只提供一种约束 Compare,允许您比较两个属性。

Build Status

需求

目前仅与Symfony 2.1+验证器兼容。

安装

推荐使用Composer安装。只需在运行

$ php composer.phar require hoppinger/validation

使用

您可以使用约束,就像使用其他Symfony约束一样。

约束

Compare

Compare约束允许您比较对象的两个属性。

  • 使用注解
// src/Acme/DemoBundle/Entity/Foo.php

/**
 * @Hop\Validation\Constraint\Compare(field="bar",compare_with="crux",operator="gt",message="Bar should be greater than crux")
 */
class Foo
{
	protected $bar;
	
	protected $crux;
	
	public function getBar()
	{
		return $this->bar;
	}
	
	public function getCrux()
	{
		return $this->crux;
	}
	
}
  • 使用YML
# src/Acme/DemoBundle/Resources/config/validation.yml
Acme\DemoBundle\Entity\Foo:
    constraints:
        - Hop\Validation\Constraint\Compare:
            field: bar
            compare_with: crux
            operator: gt
            message: Bar should be greater than crux
            

参考

选项

Compare约束接受4个参数,所有这些参数都是必需的。

  • 字段

要比较的对象的字段名。

  • compare_with

field进行比较的对象的字段名。

  • 操作符

要使用的操作符。请参阅以下内容。

  • 消息

如果约束未通过验证,将显示的消息。

有效的操作符

  • Compare::OP_GT (gt)

如果field大于compare_with,则约束匹配

  • Compare::OP_GTE (gte)

如果field大于或等于compare_with,则约束匹配

  • Compare::OP_LT (lt)

如果field小于compare_with,则约束匹配

  • Compare::OP_LTE (lte)

如果field小于或等于compare_with,则约束匹配

  • Compare::OP_EQ (eq)

如果field等于compare_with,则约束匹配

  • Compare::OP_NEQ (neq)

如果field不等于compare_with,则约束匹配

许可证

hoppinger/validation遵循MIT许可证。