Class Phalcon\Mvc\Model\Validator\Ip

extends abstract class Phalcon\Mvc\Model\Validator

implements Phalcon\Mvc\Model\ValidatorInterface

Source on GitHub

Phalcon\Mvc\Model\Validator\IP

Validates that a value is ipv4 address in valid range

This validator is only for use with Phalcon\Mvc\Collection. If you are using Phalcon\Mvc\Model, please use the validators provided by Phalcon\Validation.

  1. <?php
  2. use Phalcon\Mvc\Model\Validator\Ip;
  3. class Data extends \Phalcon\Mvc\Collection
  4. {
  5. public function validation()
  6. {
  7. // Any pubic IP
  8. $this->validate(
  9. new IP(
  10. [
  11. "field" => "server_ip",
  12. "version" => IP::VERSION_4 | IP::VERSION_6, // v6 and v4. The same if not specified
  13. "allowReserved" => false, // False if not specified. Ignored for v6
  14. "allowPrivate" => false, // False if not specified
  15. "message" => "IP address has to be correct",
  16. ]
  17. )
  18. );
  19. // Any public v4 address
  20. $this->validate(
  21. new IP(
  22. [
  23. "field" => "ip_4",
  24. "version" => IP::VERSION_4,
  25. "message" => "IP address has to be correct",
  26. ]
  27. )
  28. );
  29. // Any v6 address
  30. $this->validate(
  31. new IP(
  32. [
  33. "field" => "ip6",
  34. "version" => IP::VERSION_6,
  35. "allowPrivate" => true,
  36. "message" => "IP address has to be correct",
  37. ]
  38. )
  39. );
  40. if ($this->validationHasFailed() === true) {
  41. return false;
  42. }
  43. }
  44. }

Constants

integer VERSION_4

integer VERSION_6

Methods

public validate (Phalcon\Mvc\EntityInterface $record)

Executes the validator

public __construct (array $options) inherited from Phalcon\Mvc\Model\Validator

Phalcon\Mvc\Model\Validator constructor

protected appendMessage (string $message, [string | array $field], [string $type]) inherited from Phalcon\Mvc\Model\Validator

Appends a message to the validator

public getMessages () inherited from Phalcon\Mvc\Model\Validator

Returns messages generated by the validator

public array getOptions () inherited from Phalcon\Mvc\Model\Validator

Returns all the options from the validator

public getOption (mixed $option, [mixed $defaultValue]) inherited from Phalcon\Mvc\Model\Validator

Returns an option

public isSetOption (mixed $option) inherited from Phalcon\Mvc\Model\Validator

Check whether an option has been defined in the validator options