Skip to content

Instantly share code, notes, and snippets.

@ismaail
Last active March 31, 2019 02:10
Show Gist options
  • Save ismaail/6e0dd2e13a517ac6a1cd59aaafc7aef3 to your computer and use it in GitHub Desktop.
Save ismaail/6e0dd2e13a517ac6a1cd59aaafc7aef3 to your computer and use it in GitHub Desktop.
Array not empty - Laravel Validation Rule
<?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