Last active
December 3, 2020 10:35
-
-
Save ManojKiranA/211fb8a799a5a7fd8fa47e73fcf31986 to your computer and use it in GitHub Desktop.
Laravel Validation Rules for limiting the minimum and maximum number of fileds in given array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Rules; | |
use Illuminate\Support\Str; | |
use Illuminate\Contracts\Validation\Rule; | |
class LimitNumberOfItemsRule implements Rule | |
{ | |
/** @var array */ | |
public $fieldsToBeChecked = []; | |
/** @var int */ | |
public $minimumNumberOfFiledsToBeFilled; | |
/** @var int */ | |
public $maximumNumberOfFiledsToBeFilled; | |
/** @var bool */ | |
public $isLesserThanMinimumNumberOfFiledsToBeFilled = false; | |
/** @var bool */ | |
public $isGreaterThanMaximumNumberOfFiledsToBeFilled = false; | |
/** @var string */ | |
public $minimumNumberOfFiledsValidationMessage; | |
/** @var string */ | |
public $maximumNumberOfFiledsValidationMessage; | |
/** @var \Illuminate\Http\Request */ | |
public $request; | |
/** | |
* Create a new rule instance. | |
* | |
* @return void | |
*/ | |
public function __construct(array $fieldsToBeChecked = [],int $minimumNumberOfFiledsToBeFilled, int $maximumNumberOfFiledsToBeFilled,string $minimumNumberOfFiledsValidationMessage = null, string $maximumNumberOfFiledsValidationMessage = null) | |
{ | |
$this->fieldsToBeChecked = $fieldsToBeChecked; | |
$this->minimumNumberOfFiledsToBeFilled = $minimumNumberOfFiledsToBeFilled; | |
$this->maximumNumberOfFiledsToBeFilled = $maximumNumberOfFiledsToBeFilled; | |
$this->minimumNumberOfFiledsValidationMessage = $minimumNumberOfFiledsValidationMessage; | |
$this->maximumNumberOfFiledsValidationMessage = $maximumNumberOfFiledsValidationMessage; | |
$this->request = request(); | |
} | |
/** | |
* Determine if the validation rule passes. | |
* | |
* @param string $attribute | |
* @param mixed $value | |
* @return bool | |
*/ | |
public function passes($attribute, $value) | |
{ | |
$fromInputCount = collect($this->request->only($this->fieldsToBeChecked)) | |
->filter() | |
->count(); | |
if($fromInputCount < $this->minimumNumberOfFiledsToBeFilled){ | |
$this->isLesserThanMinimumNumberOfFiledsToBeFilled = true; | |
return false; | |
} | |
if($fromInputCount > $this->maximumNumberOfFiledsToBeFilled){ | |
$this->isGreaterThanMaximumNumberOfFiledsToBeFilled = true; | |
return false; | |
} | |
return true; | |
} | |
/** | |
* Get the validation error message. | |
* | |
* @return string | |
*/ | |
public function message() | |
{ | |
if($this->isLesserThanMinimumNumberOfFiledsToBeFilled){ | |
if($this->minimumNumberOfFiledsValidationMessage){ | |
return $this->minimumNumberOfFiledsValidationMessage; | |
} | |
$string = 'Atleast '; | |
$string .= $this->minimumNumberOfFiledsToBeFilled; | |
$string .= ' '; | |
$string .= Str::plural('Field',$this->minimumNumberOfFiledsToBeFilled); | |
$string .= ' needs to be Filled From '; | |
$string .= implode(',',$this->fieldsToBeChecked); | |
return $string; | |
} | |
if($this->isGreaterThanMaximumNumberOfFiledsToBeFilled){ | |
if($this->maximumNumberOfFiledsValidationMessage){ | |
return $this->maximumNumberOfFiledsValidationMessage; | |
} | |
$string = 'Only '; | |
$string .= $this->maximumNumberOfFiledsToBeFilled; | |
$string .= ' '; | |
$string .= Str::plural('Field',$this->maximumNumberOfFiledsToBeFilled); | |
$string .= ' needs to be Filled From '; | |
$string .= implode(',',$this->fieldsToBeChecked); | |
return $string; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment