Last active
March 31, 2019 02:10
-
-
Save ismaail/6e0dd2e13a517ac6a1cd59aaafc7aef3 to your computer and use it in GitHub Desktop.
Array not empty - Laravel Validation Rule
This file contains 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; | |
/** | |
* Class ArrayNotEmpty | |
* | |
* @package App\Rules | |
* | |
* To register the rule, add in \App\Providers\AppServiceProvider | |
* \Validator::extend('array_not_empty', '\App\Rules\ArrayNotEmpty@validate'); | |
*/ | |
class ArrayNotEmpty | |
{ | |
/** | |
* Determine if the validation rule passes. | |
* | |
* @param string $attribute | |
* @param array $value | |
* @param array $parameters | |
* @param \Illuminate\Validation\Validator $validator | |
* | |
* @return bool | |
* | |
* @SuppressWarnings(unused) | |
*/ | |
public function validate($attribute, $value, array $parameters, $validator): bool | |
{ | |
$value = array_map('floatval', $value); | |
$value = array_values(array_unique($value)); | |
return [0.0] !== $value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment