• 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/Validators

Validators

15 views 0

Written by admin
May 21, 2022
A PHP 8.2+ Attribute Based Valdiation For Properties of Object/Entity/DTO. Comes with in-built data-type converter that can be implmented to make validation easy without compromising your object. You can create your own custom Validators and/or Converter by implmenting a simple interface.

The Validators are  PHP attribute that validates object properties:
Validator Parameters Definition Example
AbstractValidator
  • errorMessage – [default:[fieldName] can not be null!]
An Validator Abstract class that can be used to implement other validator object.  

Alpha

  • errorMessage  – default to: “For [fieldName] – Only characters A through Z are allowed!”
  • $regex – default to: ‘/^[a-z]*$/i’
  • allowEmpty – default to: [false]
validates if a string contains only alphabetic characters.
 #[Alpha]
 protected ?string $name;
AlphaNumeric
  • errorMessage  – default to: “For [fieldName] – Only a-z, 0-9, -, ., _, @ and space character allowed!”
  • $regex – default to: ‘/^[a-z0-9\-\.\_\,\@]*$/i’
  • allowEmpty – default to [false]
validates if a string contains only alphanumeric
characters, hyphens, periods, underscores, commas, at symbols, and spaces.
#[AlphaNumeric]
protected ?string $username;
Boolean
  • errorMessage – [default:[fieldName] is not a valid boolean!]
validator that checks if a value is a valid boolean
#[Boolean]
protected $isEnabled = null;
ContainsLowerCase
  • errorMessage  – default to: ”
    [fieldName] should contain at least a lower case letter (e.g: a…z)!“
  • allowEmpty – default to [false]
validates if a string contains at least one lowercase letter

#[ContainsLowerCase]
protected ?string $password;

ContainsNumeric
  • errorMessage  – default to: ”
    [fieldName] should contain at least 1 number (e.g: 0 – 9)!“
  • allowEmpty – default to [false]
checks if the value contains at least one numeric character
#[ContainsNumeric]
protected ?string $password;
ContainsSpecialCharacter
  • errorMessage  – default to: ”
    [fieldName] should contain at least a one of the special character (!@#$%^&*()\-_=+{};:,<.>)!“
  • $regex – default to: ‘
    /[!@#$%^&*()\-_=+{};:,<.>]/‘
  • allowEmpty – default to [false]
validate if a string contains at least one special character.
#[ContainsSpecialCharacter]
protected ?string $password;
ContainsUpperCase
  • errorMessage  – default to: “[fieldName] should contain at least 1 upper case letter (e.g: A…Z)!“
  • allowEmpty – default to [false]
validates if a string contains at least one uppercase letter.
#[ContainsUpperCase]
protected ?string $password;
DateValidator
  • errorMessage  – default to: ”
    [fieldName] is not a valid date value!“
validates if a given value is a valid date.
#[DateValidator]
protected ?\DateTime $createdAt;
Double
  • errorMessage  – default to: ”
    [fieldName] is not a valid double!“
validator that checks if a value is of type double.
#[Double]
protected $price;
Email
  • errorMessage  – default to: ”
    [fieldName] is not a valid email address!“
validator that checks if a given value is a valid email address.
#[Email]
protected $emailAddress;
FloatValidator
  • errorMessage  – default to: ”
    [fieldName] is not a valid float value!“
validates whether a given value is a valid float.
#[FloatValidator]
protected $price;
InArray
  • errorMessage  – default to: ”
    [fieldName] is not a valid array!“
  • container – default to: []
  • caseSensitive – default to [true]
validator that checks if a given value is present in a specified array.
#[InArray('RICE', 'BEANS')]
protected array $food = [];
IntegerValidator
  • errorMessage  – default to: “[fieldName] is not a valid integer value!”
validates whether a given value is an integer.
#[IntegerValidator]
protected $position;
IpAddress
  • errorMessage  – default to: “[fieldName] is not a valid ip address!”
validator that checks if a given value is a valid IP address.
#[IpAddress]
protected ?string $ipAddress;
IsBase64
  • errorMessage  – default to: ”
    [fieldName] is not a valid base 64 string!“
validator that checks if a given value is a valid base64 string.
#[IsBase64]
protected ?string $jsonValue;
Length
  • minLength
  • maxLength
  • errorMessage  – default to: “Min Length of [minLength] and Max Length of [maxLength] for [fieldName] is required!“
validate the length of a property value, with a minimum and maximum length specified.
#[Length(1, 10)]
protected ?int $value;
Max
  • maxLength
  • errorMessage  – default to: “
    Maximum size for [fieldName] is [size]. Supplied value size is not a valid!“
validator that checks if a value is less than or equal to a specified maximum value.
#[Max(120)]
protected ?int $age;
MaxLength
  • maxLength
  • errorMessage  – default to: “
    Maximum size for [fieldName] is [size]. Supplied value size is not a valid!“
validator that checks if a given value’s length exceeds a specified maximum length.
#[MaxLength(255)]
protected ?string $text;
Min
  • maxLength
  • errorMessage  – default to: “
    Minimum size for [fieldName] is [size]. Supplied value size is not a valid!“
validator that checks if a value is greater than or equal to a specified minimum value. #[Min(18)]
protected ?int $age;
MinLength
  • maxLength
  • errorMessage  – default to: “
    Minimum Length of [minLength] for [fieldName] is required!”
    “
validator that checks if a given value has a minimum length.
#[MaxLength(255)]
protected ?string $text;
NotBlank
  • errorMessage  – default to: “
    [fieldName] can not be blank!“
validator that checks if a given value is not empty or blank.
#[NotBlank]
protected ?string $username;
NotEmpty
  • errorMessage  – default to: “
    [fieldName] can not be empty!“
validator that checks if a property is not empty.
#[NotEmpty]
protected ?string $name;
NotNull
  • errorMessage – default to: “[fieldName] can not be null!”
validator that checks if a property is not null.
#[NotNull]
protected $name = null;
Numeric
  • errorMessage  – default to: “
    [fieldName] is not a valid numeric value!“
validate if a given value is numeric
#[Numeric]
protected $phoneNumber;
Password
  • errorMessage  – Depends on which part of the password criteria failed.
validates whether a given value meets certain password requirements.
#[Password]
protected ?string $password;
Pattern
  • errorMessage  – default to: “
    [fieldName] is not a valid!“
validator that checks if a given value matches a
specified regular expression pattern.
#[Pattern(regex=/[0-9]/, false)]
protected $phoneNumber;
Required
  • errorMessage  – default to: “
    [fieldName] is required!“
validates that the
value is not empty/null/blank/0
#[Required]
protected $firstname;
Size
  • errorMessage  – default to: “
    Length of [fieldName] is [size] required. Supplied value size is not a valid!“
validator that checks if the length of a value is equal to a specified size.
#[Size(120)]
protected $firstname;
StringValidator
  • errorMessage  – default to: “
    [fieldName] is not a valid string value!“
validates if a given value is a string. #[StringValidator]
protected $firstname;
UrlValidator
  • errorMessage  – default to: “
    [fieldName] is not a valid url!“
validates if a given value is a valid URL. #[UrlValidator]
protected $url;

 

Was this helpful?

Yes  No
Related Articles
  • Validation Usage Example
  • Converters

Didn't find your answer? Contact Us

Leave A Comment Cancel reply

  Validation Usage Example

Converters  

  • Copyright 2023 Emma Technologies LLC. All Rights Reserved