• Home
  • Knowledge Base
    • KB Category
    • Single Page
    • Private Content
      • Private – Category
      • Private – Article
      • Private – Attached Files
      • Password Protected Article
    • Page – features
      • One Click Article Feedback
      • Attached Files
      • Print Article
      • Custom header
  • Search
  • Home
  • Knowledge Base
    • KB Category
    • Single Page
    • Private Content
      • Private – Category
      • Private – Article
      • Private – Attached Files
      • Password Protected Article
    • Page – features
      • One Click Article Feedback
      • Attached Files
      • Print Article
      • Custom header
  • Search
home/Knowledge Base/Emma Validations/Validation Usage Example

Validation Usage Example

26 views 0

Written by admin
May 21, 2022

Code Application Example: Validating an Object

The Validator class is a PHP class that implements the ValidationInterface and provides methods for validating object properties.
 <?php

class LoginAttempt
{
   /**
    * @var int|null
    */
   #[Min(1)]
   protected ?int $id = null;

   /**
    * @var string|null
    */
   #[Required]
   #[AlphaNumeric]
   protected ?string $username = null;
   
   /**
    * @var string|null
    */
   #[Required]
   #[Email]
   protected ?string $email = null;

   /**
    * @var string|null
    */
   #[Required]
   #[AlphaNumeric]
   protected ?string $computername = null;

   /**
    * @var string|null
    */
   #[Required]
   #[IpAddress]
   protected ?string $ipaddress = null;

   /**
    * @var \DateTime|null
    */
   #[Required]
   #[DateTimeFormat]
   protected ?\DateTime $time_created = null;
   
}

//Let's Validate this object
$loginAttempt = new LoginAttempt();

$violations = ValidationFactory::validate($loginAttempt);
if ($violations->valid()) {
   var_dump($violations->getArrayCopy());
}

That’s easy right! No complication, no stress, just use PHP 8 Attribute to validate your object easily without any hassle. Enjoy!

Was this helpful?

Yes  No
Related Articles
  • Validators
  • Converters

Didn't find your answer? Contact Us

Leave A Comment Cancel reply

Validators  

  • Copyright 2023 Emma Technologies LLC. All Rights Reserved