PHP code example of hsnfirdaus / class-validator
1. Go to this page and download the library: Download hsnfirdaus/class-validator library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hsnfirdaus / class-validator example snippets
declare(strict_types=1);
namespace MyApp\Contract\User;
use Hsnfirdaus\ClassValidator\Attribute\IsEnum;
use Hsnfirdaus\ClassValidator\Attribute\IsNotEmpty;
use Hsnfirdaus\ClassValidator\Attribute\IsOptional;
use Hsnfirdaus\ClassValidator\Validator;
use MyApp\Entity\Enum\UserRole;
use MyApp\Entity\Enum\UserType;
class AddUserContract
{
#[Name(name: 'User Type')]
#[IsNotEmpty]
#[IsEnum(enum: UserType::class)]
public string $type = 'Can\'t be empty';
#[IsNotEmpty]
#[IsEnum(enum: UserRole::class)]
public string $role = 'ValidEnumValue';
#[IsOptional]
public string $identifier;
public function validate()
{
Validator::validate($this);
}
}
declare(strict_types=1);
alidator;
Validator::setLang('id');
// Then you can call Validator::validate method
Validator::setLangDir(__DIR__.'/locale');
Validator::setLang('kr');