/yii-form

The package helps with implementing data entry forms

Primary LanguagePHPBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

Yii Form


The package helps with implementing data entry forms.

Latest Stable Version Total Downloads Build status Scrutinizer Code Quality Code Coverage Mutation testing badge static analysis

Installation

The package could be installed via composer:

composer require yiisoft/form

Usage

You must create your form model by extending the abstract form class, defining all the private properties with their respective typehint.

Example: LoginForm.php

<?php

declare(strict_types=1);

namespace App\Form;

use Yiisoft\Form\FormModel;
use Yiisoft\Validator\Rule\Email;
use Yiisoft\Validator\Rule\Required;
use Yiisoft\Validator\Rule\HasLength;

class LoginForm extends FormModel
{
    /** Define propertys with TypeHint */
    private ?string $login = null;
    private ?string $password = null;
    private bool $rememberMe = false;

    /** Getters propertys */
    public function getLogin(): ?string
    {
        return $this->login;
    }

    public function getPassword(): ?string
    {
        return $this->password;
    }

    public function getRememberMe(): bool
    {
        return $this->rememberMe;
    }

    /** Setters propertys */
    public function login(string $value): void
    {
        $this->login = $value;
    }

    public function password(string $value): void
    {
        $this->password = $value;
    }

    public function rememberMe(bool $value): void
    {
        $this->rememberMe = $value;
    }

    /** Define labels */
    public function attributeLabels(): array
    {
        return [
            'login' => 'Login:',
            'password' => 'Password:',
            'rememberMe' => 'remember Me:'
        ];
    }

    /** Define formname */
    public function formName(): ?string
    {
        return 'LoginForm';
    }

    /** Add rules */
    protected function rules(): array
    {
        return [
            'login' => $this->loginRules()
        ];
    }

    /** Define login rules */
    private function loginRules(): array
    {
        return [
            new Required(),
            (new HasLength())
            ->min(4)
            ->max(40)
            ->tooShortMessage('Is too short.')
            ->tooLongMessage('Is too long.'),
            new Email()
        ];
    }
}

Unit testing

The package is tested with PHPUnit. To run tests:

./vendor/bin/phpunit

Mutation testing

The package tests are checked with Infection mutation framework. To run it:

./vendor/bin/infection

Static analysis

The code is statically analyzed with Phan. To run static analysis:

./vendor/bin/phan