added new rules
This commit is contained in:
@@ -2,11 +2,15 @@
|
|||||||
|
|
||||||
namespace App\Providers;
|
namespace App\Providers;
|
||||||
|
|
||||||
|
use App\Rules\RequiredIfAny;
|
||||||
|
use App\Rules\Uniqueish;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
use PDOException;
|
use PDOException;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
use Illuminate\Support\Facades\Validator;
|
||||||
|
|
||||||
class AppServiceProvider extends ServiceProvider
|
class AppServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
@@ -31,5 +35,17 @@ class AppServiceProvider extends ServiceProvider
|
|||||||
$public = config("filesystems.disks.{$diskName}.public", false);
|
$public = config("filesystems.disks.{$diskName}.public", false);
|
||||||
return $public;
|
return $public;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Validator::extend('uniqueish', function ($attribute, $value, $parameters, $validator) {
|
||||||
|
$table = $parameters[0];
|
||||||
|
$column = isset($parameters[1]) === true ? $parameters[1] : null;
|
||||||
|
|
||||||
|
$rule = new Uniqueish($table, $column);
|
||||||
|
return $rule->passes($attribute, $value);
|
||||||
|
});
|
||||||
|
|
||||||
|
Rule::macro('requiredIfAny', function ($table, ...$columns) {
|
||||||
|
return new RequiredIfAny($table, ...$columns);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
71
app/Rules/RequiredIfAny.php
Normal file
71
app/Rules/RequiredIfAny.php
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Rules;
|
||||||
|
|
||||||
|
use Illuminate\Contracts\Validation\Rule;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
class RequiredIfAny implements Rule
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The table name to compare.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $table;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The column name to compare.
|
||||||
|
*
|
||||||
|
* @var string[]
|
||||||
|
*/
|
||||||
|
protected $columns;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new rule instance.
|
||||||
|
*
|
||||||
|
* @param string $table The table name to compare.
|
||||||
|
* @param string ...$columns The column name(s) to compare.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct(string $table, string ...$columns)
|
||||||
|
{
|
||||||
|
$this->table = $table;
|
||||||
|
$this->columns = $columns;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine if the validation rule passes.
|
||||||
|
*
|
||||||
|
* @param mixed $attribute Not used.
|
||||||
|
* @param mixed $value The value to compare.
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function passes(mixed $attribute, mixed $value)
|
||||||
|
{
|
||||||
|
foreach ($this->columns as $column) {
|
||||||
|
$result = DB::table($this->table)
|
||||||
|
->where($column, '!=', '')
|
||||||
|
->where($column, '!=', null)
|
||||||
|
->where($attribute, '=', '')
|
||||||
|
->exists();
|
||||||
|
|
||||||
|
if ($result !== null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation error message.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function message()
|
||||||
|
{
|
||||||
|
return 'The :attribute field is required if any of the following fields are not empty: ' . implode(', ', $this->columns);
|
||||||
|
}
|
||||||
|
}
|
||||||
90
app/Rules/Uniqueish.php
Normal file
90
app/Rules/Uniqueish.php
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Rules;
|
||||||
|
|
||||||
|
use Illuminate\Contracts\Validation\Rule;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
class Uniqueish implements Rule
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The table name to compare.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $table;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The column name to compare.
|
||||||
|
*
|
||||||
|
* @var string|null
|
||||||
|
*/
|
||||||
|
protected $column;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The ID of the record to be ignored.
|
||||||
|
*
|
||||||
|
* @var integer|null
|
||||||
|
*/
|
||||||
|
protected $ignoreId;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new rule instance.
|
||||||
|
*
|
||||||
|
* @param string $table The table name to compare.
|
||||||
|
* @param string $column The column name to compare.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct(string $table, string $column = null)
|
||||||
|
{
|
||||||
|
$this->table = $table;
|
||||||
|
$this->column = $column;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the ID of the record to be ignored.
|
||||||
|
*
|
||||||
|
* @param integer $id
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function ignore($id)
|
||||||
|
{
|
||||||
|
$this->ignoreId = $id;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine if the validation rule passes.
|
||||||
|
*
|
||||||
|
* @param mixed $attribute Not used.
|
||||||
|
* @param mixed $value The value to compare.
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function passes(mixed $attribute, mixed $value)
|
||||||
|
{
|
||||||
|
$columnName = ($this->column ?? $attribute);
|
||||||
|
$similarValue = preg_replace('/[^A-Za-z]/', '', strtolower($value));
|
||||||
|
|
||||||
|
$query = DB::table($this->table)
|
||||||
|
->whereRaw('LOWER(REGEXP_REPLACE(' . $columnName . ', \'[^A-Za-z]\', \'\')) = ?', [$similarValue]);
|
||||||
|
|
||||||
|
if ($this->ignoreId !== null) {
|
||||||
|
$query->where('id', '<>', $this->ignoreId);
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = $query->first();
|
||||||
|
|
||||||
|
return $result === null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation error message.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function message()
|
||||||
|
{
|
||||||
|
return 'The :attribute is similar to an existing value in the database.';
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user