ice framework documentation ice doc v1.11.0
  1. namespace Ice\Validation\Validator;
  2.  
  3. use Ice\Validation;
  4. use Ice\Validation\Validator;
  5.  
  6. /**
  7. * Email validator.
  8. *
  9. * @package Ice/Validation
  10. * @category Security
  11. * @author Ice Team
  12. * @copyright (c) 2014-2025 Ice Team
  13. * @license http://iceframework.org/license
  14. *
  15. *
  16.  *  $validation = new Ice\Validation();
  17.  *
  18.  *  $validation->rules([
  19.  *      'e_mail' => 'email'
  20.  *  ]);
  21.  *
  22.  *  $valid = $validation->validate($_POST);
  23.  *
  24.  *  if (!$valid) {
  25.  *      $messages = $validation->getMessages();
  26.  *  }
  27.  * 
  28. */
  29. class Email extends Validator
  30. {
  31. /**
  32. * Validate the validator
  33. * Options: label, message
  34. *
  35. * @param Validation validation
  36. * @param string field
  37. * @return boolean
  38. */
  39. public function validate( validation, string! field) -> boolean
  40. {
  41. var value, label, message, i18n, replace;
  42. let value = validation->getValue(field);
  43. if value === "" || value === null {
  44. return true;
  45. }
  46. if !filter_var(value, FILTER_VALIDATE_EMAIL) {
  47. if this->has("label") {
  48. let label = this->get("label");
  49. } else {
  50. let label = validation->getLabel(field);
  51. }
  52. if this->has("message") {
  53. let message = this->get("message");
  54. } else {
  55. let message = validation->getDefaultMessage("email");
  56. }
  57. // Translate strings
  58. if validation->getTranslate() === true && validation->getDi()->has("i18n") {
  59. let i18n = validation->getDi()->get("i18n"),
  60. label = i18n->translate(label),
  61. message = i18n->translate(message);
  62. }
  63. let replace = [":field": label];
  64. validation->addMessage(field, strtr(message, replace));
  65. return false;
  66. }
  67. return true;
  68. }
  69. }