Developer Guideline
  • Home
  • 🧠Must you know
    • Algorithm
    • Architecture
      • API
    • Comparison
      • ID Token vs Access Token
      • Lazy Loading vs Eager Loading
      • Morphs vs Foreign Key
      • UUID vs ULID
      • GraphQL vs REST
      • Cache vs CDN
      • Concurrency vs Parallelism
      • Null vs Not Null
    • Diagram
      • CI/CD Pipeline
      • High Performance Culture
  • ☂️Laravel
    • Template
      • Template System Check in Route on Laravel
      • Template Function in FormController on Laravel
      • Template Route call FormController on Laravel
      • Template Route Prefix Name on Laravel
      • Template Basic and Custom Pagination on Laravel
      • Template PHP Artisan Command
      • Template Route for App Environment
    • Feature
      • Data Type
      • Mailables
      • Rules
    • Package
    • Document
  • 🫖Collaboration Agreement
    • Naming Convention
      • Naming Convention for Git Branch
      • Naming Convention for Environment Variable
    • Rule
      • Rule of Commit Message
      • Semantic Versioning
  • 🦣Project Manager
    • Requirements
      • System Requirements
      • Technical Requirements
      • Functional Requirements
Powered by GitBook
On this page
  • 🐶 Email Private Rule
  • 🐺 Email Public Rule
  • 🐱 Expiration Rule
  • 🦁 Language English Rule
  • 🐯 Language Thai Rule
  • 🦊 Lowercase Rule
  • 🦝 Mobile Phone Rule
  • 🐮 Repeat Password Rule
  • 🐷 Strong Password Rule
  • 🐗 Student Code Rule
  • 🐹 Upload File Rule
  • 🐰 Uppercase Rule
  1. Laravel
  2. Feature

Rules

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

🐶 Email Private Rule

EmailPrivateRule.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.');
        }
    }
}

🐺 Email Public Rule

EmailPublicRule.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.');
        }
    }
}

🐱 Expiration Rule

ExpirationRule.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.');
        }
    }
}

🦁 Language English Rule

LanguageEnglishRule.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.');
        }
    }
}

🐯 Language Thai Rule

LanguageThaiRule.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.');
        }
    }
}

🦊 Lowercase Rule

LowercaseRule.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.');
        }
    }
}

🦝 Mobile Phone Rule

MobilePhoneRule.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.');
        }
    }
}

🐮 Repeat Password Rule

RepeatPasswordRule.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.');
        }
    }
}

🐷 Strong Password Rule

StrongPasswordRule.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.');
        }
    }
}

🐗 Student Code Rule

StudentCodeRule.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.');
        }
    }
}

🐹 Upload File Rule

UploadFileRule.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));
        }
    }
}

🐰 Uppercase Rule

UppercaseRule.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.');
        }
    }
}
PreviousMailablesNextPackage

Last updated 10 months ago

☂️