> For the complete documentation index, see [llms.txt](https://idt-nida.gitbook.io/developer-guideline/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://idt-nida.gitbook.io/developer-guideline/laravel/feature/rules.md).

# Rules

ตัวอย่าง Code ในการทำ Validation โดยใช้ Rules

## 🐶 Email Private Rule

{% code title="EmailPrivateRule.php" %}

```php
<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class EmailPrivateRule implements ValidationRule
{
    /**
     * Run the validation rule.
     *
     * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
     */
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        $domains = [
            'nida.ac.th',
            'stu.nida.ac.th',
            'guest.nida.ac.th',
        ];

        $email = substr(strrchr($value, "@"), 1);

        if (!in_array($email, $domains)) {
            $fail('The :attribute must be a private email domain.');
        }
    }
}
```

{% endcode %}

## 🐺 Email Public Rule

{% code title="EmailPublicRule.php" %}

```php
<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class EmailPublicRule implements ValidationRule
{
    /**
     * Run the validation rule.
     *
     * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
     */
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        $domains = [
            'gmail.com',
            'yahoo.com',
            'hotmail.com',
            'outlook.com',
            'example.com',
        ];

        $email = substr(strrchr($value, "@"), 1);

        if (!in_array($email, $domains)) {
            $fail('The :attribute must be a public email domain.');
        }
    }
}
```

{% endcode %}

## 🐱 Expiration Rule

{% code title="ExpirationRule.php" %}

```php
<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Carbon\Carbon;

class ExpirationRule implements ValidationRule
{
    /**
     * Run the validation rule.
     *
     * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
     */
    protected $current_date;
    public function __construct()
    {
        $this->current_date = Carbon::now()->format('Y-m-d');
    }
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        if ($this->current_date > $value) {
            $fail('The :attribute is expired or not a valid date.');
        }
    }
}
```

{% endcode %}

## 🦁 Language English Rule

{% code title="LanguageEnglishRule.php" %}

```php
<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class LanguageEnglishRule implements ValidationRule
{
    /**
     * Run the validation rule.
     *
     * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
     */
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        if (!preg_match('/^[a-zA-Z\s]+$/', $value)) {
            $fail('The :attribute must contain only English text.');
        }
    }
}
```

{% endcode %}

## 🐯 Language Thai Rule

{% code title="LanguageThaiRule.php" %}

```php
<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class LanguageThaiRule implements ValidationRule
{
    /**
     * Run the validation rule.
     *
     * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
     */
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        if (!preg_match('/^[\p{Thai} ]+$/u', $value)) {
            $fail('The :attribute must contain only Thai text.');
        }
    }
}
```

{% endcode %}

## 🦊 Lowercase Rule

{% code title="LowercaseRule.php" %}

```php
<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class LowercaseRule implements ValidationRule
{
    /**
     * Run the validation rule.
     *
     * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
     */
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        if (strtolower($value) !== $value) {
            $fail('The :attribute must be lowercase.');
        }
    }
}
```

{% endcode %}

## 🦝 Mobile Phone Rule

{% code title="MobilePhoneRule.php" %}

```php
<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class MobilePhoneRule implements ValidationRule
{
    /**
     * Run the validation rule.
     *
     * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
     */
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        if (!is_numeric($value) && strlen($value) !== 10) {
            $fail('The :attribute must be a 10-digit number.');
        }
    }
}
```

{% endcode %}

## 🐮 Repeat Password Rule

{% code title="RepeatPasswordRule.php" %}

```php
<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class RepeatPasswordRule implements ValidationRule
{
    /**
     * Run the validation rule.
     *
     * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
     */
    public function __construct($password)
    {
        $this->password = $password;
    }
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        if ($value !== $this->password) {
            $fail('The password confirmation does not match.');
        }
    }
}
```

{% endcode %}

## 🐷 Strong Password Rule

{% code title="StrongPasswordRule.php" %}

```php
<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class StrongPasswordRule implements ValidationRule
{
    /**
     * Run the validation rule.
     *
     * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
     */
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        if (!preg_match('/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/', $value)) {
            $fail('The :attribute must be at least 8 characters long and contain at least one uppercase letter, one lowercase letter, one number, and one special character.');
        }
    }
}
```

{% endcode %}

## 🐗 Student Code Rule

{% code title="StudentCodeRule.php" %}

```php
<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class StudentCodeRule implements ValidationRule
{
    /**
     * Run the validation rule.
     *
     * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
     */
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        if (!is_numeric($value) && strlen($value) !== 10) {
            $fail('The :attribute must be a 10-digit number.');
        }
    }
}
```

{% endcode %}

## 🐹 Upload File Rule

{% code title="UploadFileRule.php" %}

```php
<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class UploadFileRule implements ValidationRule
{
    /**
     * Run the validation rule.
     *
     * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
     */
    public function __construct($maxsize, $allow_filetype)
    {
        $this->maxsize = $maxsize;
        $this->allow_filetype = $allow_filetype;
    }
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        if ($value->getSize() > $this->maxsize * 1024) {
            $fail('The file must not exceed ' . $this->maxsize . ' KB.');
        } elseif (!in_array($value->getClientOriginalExtension(), $this->allow_filetype)) {
            $fail('The file must be one of the following types: ' . implode(', ', $this->allow_filetype));
        }
    }
}
```

{% endcode %}

## 🐰 Uppercase Rule

{% code title="UppercaseRule.php" %}

```php
<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class UppercaseRule implements ValidationRule
{
    /**
     * Run the validation rule.
     *
     * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
     */
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        if (strtoupper($value) !== $value) {
            $fail('The :attribute must be uppercase.');
        }
    }
}
```

{% endcode %}
