4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Constraints;
14 use Symfony\Component\Validator\Constraint;
15 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
18 * Validates that a value is a valid IP address.
21 * @Target({"PROPERTY", "METHOD", "ANNOTATION"})
23 * @author Bernhard Schussek <bschussek@gmail.com>
24 * @author Joseph Bielawski <stloyd@gmail.com>
26 class Ip extends Constraint
32 // adds FILTER_FLAG_NO_PRIV_RANGE flag (skip private ranges)
33 const V4_NO_PRIV = '4_no_priv';
34 const V6_NO_PRIV = '6_no_priv';
35 const ALL_NO_PRIV = 'all_no_priv';
37 // adds FILTER_FLAG_NO_RES_RANGE flag (skip reserved ranges)
38 const V4_NO_RES = '4_no_res';
39 const V6_NO_RES = '6_no_res';
40 const ALL_NO_RES = 'all_no_res';
42 // adds FILTER_FLAG_NO_PRIV_RANGE and FILTER_FLAG_NO_RES_RANGE flags (skip both)
43 const V4_ONLY_PUBLIC = '4_public';
44 const V6_ONLY_PUBLIC = '6_public';
45 const ALL_ONLY_PUBLIC = 'all_public';
47 const INVALID_IP_ERROR = 'b1b427ae-9f6f-41b0-aa9b-84511fbb3c5b';
49 protected static $versions = array(
64 self::ALL_ONLY_PUBLIC,
67 protected static $errorNames = array(
68 self::INVALID_IP_ERROR => 'INVALID_IP_ERROR',
71 public $version = self::V4;
73 public $message = 'This is not a valid IP address.';
78 public function __construct($options = null)
80 parent::__construct($options);
82 if (!in_array($this->version, self::$versions)) {
83 throw new ConstraintDefinitionException(sprintf('The option "version" must be one of "%s"', implode('", "', self::$versions)));