Skip to content
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