Skip to content

Entity validation

Form class instanciation

Small Swoole Entity Manager is coupled with Small Forms.

Just do :

$userManager = $this->entityManagerFactory
    ->get(UserManager::class);
$form = $userManager->getForm();

To get form of your entity.

Adding validation rules

You can add validation rules as attributes in order to test entry data :

use Small\Forms\ValidationRule\ValidateNotEmpty;
use Small\Forms\ValidationRule\ValidateNumberCharsBetween;
use \Small\Collection\Collection\StringCollection;

class UserEntity extends AbstractEntity
    implements BeforeDeleteInterface, AfterDeleteInterface
{

    public static int $numUsers = 0;

    #[PrimaryKey]
    #[ValidateNotEmpty]
    public ?int $id;

    #[Field(FieldValueType::string)]
    #[ValidateNumberCharsBetween(3, 255)]
    public ?string $username = null;

    #[Field(FieldValueType::dateTime)]
    public ?\DateTime $updatedAt = null;

    #[ToMany(ProjectManager::class, ['id' => 'userId'])]
    public ?ProjectCollection $projects = null;
}

$userManager = $this->entityManagerFactory
    ->get(UserManager::class);
$form = $userManager->getForm();

$form->fillWithArray([
    'id' => 3,
    'username' => 'seb',
    null,
])

$form->validate($messages = new (StringCollection))
    ->hydrate($entity = $userManager->newEntity())
;

$entity->persist();

With that entity, if username is 'ab', an exception will be thrown.

Small forms documentation

Visit Small Form repo for complete documentation

Next chapter : Database layers