Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add length rule: "The :attribute must must have an exact length of :length" #150

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/Rules/Length.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rakit\Validation\Rules;

use Rakit\Validation\Rule;

class Length extends Rule
{
use Traits\SizeTrait;

/** @var string */
protected $message = "The :attribute must must have an exact length of :length";

/** @var array */
protected $fillableParams = ['length'];

/**
* Check the $value is valid
*
* @param mixed $value
* @return bool
*/
public function check($value): bool
{
$this->requireParameters($this->fillableParams);

$this->requireParameters($this->fillableParams);

$length = (int) $this->parameter('length');

return strlen((string) $value) == $length;
}
}
1 change: 1 addition & 0 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ protected function registerBaseValidators()
'defaults' => new Rules\Defaults,
'default' => new Rules\Defaults, // alias of defaults
'nullable' => new Rules\Nullable,
'length' => new Rules\Length,
];

foreach ($baseValidator as $key => $validator) {
Expand Down