Ticket 1095 : mini-crud validate images + add mini-crud tooltip

This commit is contained in:
Alexis Gousseau 2022-10-11 15:52:04 +02:00
parent 3f8e194e5c
commit 6b096945cb
19 changed files with 519 additions and 216 deletions

View file

@ -19,6 +19,7 @@ use Kwaadpepper\Enum\BaseEnumRoutable;
* html autorisé
* @method static self videoVimeo()
* @method static self number()
* @method static self email()
*/
final class InputType extends BaseEnumRoutable
{
@ -40,7 +41,8 @@ final class InputType extends BaseEnumRoutable
'dropdownArray' => 7,
'miniblocArray' => 8,
'videoVimeo' => 9,
'number' => 10
'number' => 10,
'email' => 11
];
}
}

View file

@ -2,7 +2,7 @@
namespace App\Http\Middleware;
use App\Lib\ToolboxHelper;
use App\Lib\Helpers\ToolboxHelper;
use Illuminate\Http\Request;
class HtmlSanitizer

View file

@ -17,26 +17,17 @@ class UpdateEngagementsRequest extends UpdateMiniCrudRequest
*/
protected $modelName = 'engagement';
/**
* Determine if the user is authorized to make this request.
*
* @return boolean
*/
public function authorize(): bool
{
return auth('backend')->check();
}
/**
* Prepare the data for validation.
*
* @return void
*/
protected function prepareForValidation()
public function prepareForValidation(): void
{
$title = strip_tags($this->title);
$this->merge(['slug' => Str::slug($title)]);
$this->mergePicture('banner');
parent::prepareForValidation();
}
/**

View file

@ -33,9 +33,10 @@ class RangeModelTitleRequest extends FormRequest
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed,Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassAfterLastUsed
function ($attribute, $value, $fail) {
$table = (new $value())->getTable();
Validator::validate(['model' => $this->id], [
'model' => "required|exists:{$table},id",
]);
Validator::validate(
['model' => $this->id],
['model' => "required|exists:{$table},id"]
);
}
]
];

View file

@ -3,8 +3,9 @@
namespace App\Http\Requests\Bo;
use App\Enums\InputType;
use App\Lib\ToolboxHelper;
use App\Lib\Helpers\ToolboxHelper;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
@ -27,6 +28,29 @@ abstract class UpdateMiniCrudRequest extends FormRequest
return auth('backend')->check();
}
/**
* Prepare the data for validation.
*
* @return void
*/
public function prepareForValidation(): void
{
foreach ($this->{$this->modelName}->data as $iptName => $input) {
$input = \json_decode(\json_encode($input), true);
switch ($input['inputType']['value']) {
case InputType::boolean()->value:
$all = $this->all();
$nested = Arr::undot([
"data.$iptName.value" =>
$this->input("data.$iptName.value") ? true : false
]);
$all = array_merge_recursive_distinct($all, $nested);
$this->merge($all);
break;
} //end switch
} //end foreach
}
/**
* Get the validation rules that apply to the request.
*
@ -47,6 +71,7 @@ abstract class UpdateMiniCrudRequest extends FormRequest
]);
break;
case InputType::string()->value:
case InputType::email()->value:
$rules = array_merge($rules, [
"data.$iptName.value" => $this->compileValidate('required|string|min:1', $input)
]);
@ -79,9 +104,10 @@ abstract class UpdateMiniCrudRequest extends FormRequest
[function ($attribute, $value, $fail) use ($max, $dim) {
try {
if (\is_object($value)) {
Validator::validate(
Validator::make(
[$attribute => $value],
[$attribute => [
// Replace dots since laravel framework 8.76.
[ToolboxHelper::mbReplace('.', '\.', $attribute) => [
'file',
'mimes:png,jpg,jpeg',
sprintf(
@ -92,11 +118,11 @@ abstract class UpdateMiniCrudRequest extends FormRequest
$dim['largeur']
)
]]
);
}
)->validate();
} //end if
} catch (ValidationException $e) {
$fail(collect($e->errors())->first()[0]);
}
} //end try
}]
),
"data.$iptName.alt" => $this->compileValidate('required|string|min:5', $input),
@ -120,15 +146,8 @@ abstract class UpdateMiniCrudRequest extends FormRequest
]);
break;
case InputType::boolean()->value:
$all = $this->all();
$nested = ToolboxHelper::dotStringToNestedArray(
"data.$iptName.value",
$this->input("data.$iptName.value") ? true : false
);
$all = array_merge_recursive_distinct($all, $nested);
$this->merge($all);
$rules = array_merge($rules, [
"data.$iptName.value" => 'boolean'
"data.$iptName.value" => $this->compileValidate('boolean', $input)
]);
break;
} //end switch

View file

@ -1,6 +1,6 @@
<?php
namespace App\Lib;
namespace App\Lib\Helpers;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\File;

View file

@ -1,6 +1,6 @@
<?php
namespace App\Lib;
namespace App\Lib\Helpers;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
@ -113,24 +113,6 @@ class ToolboxHelper
return $text;
}
/**
* Generates an array from a dot separated string
*
* @param string $path
* @param mixed $value
* @param string $separator
* @return array
*/
public static function dotStringToNestedArray(string $path, $value, string $separator = '.'): array
{
$arr = $value;
$keys = array_reverse(explode($separator, $path));
foreach ($keys as $key) {
$arr = [$key => $arr];
}
return $arr;
}
/**
* Mutltibyte string replace
* @param mixed $search
@ -190,9 +172,10 @@ class ToolboxHelper
) {
if (!$query) {
Validator::make([$field => $value], [
$field => Rule::unique($table, $field)->ignore($id),
self::mbReplace('.', '\.', $field) => Rule::unique($table, $field)->ignore($id),
], [
$field . '.unique' => trans('La valeur :value pour le champ :attribute est déjà utilisé', [
self::mbReplace('.', '\.', "{$field}.unique")
=> trans('La valeur :value pour le champ :attribute est déjà utilisé', [
'attribute' => $field,
'value' => $value
])

View file

@ -3,8 +3,8 @@
namespace App\Models;
use App\Enums\Engagements\EngagementType;
use App\Lib\FileStorageHelper;
use App\Lib\ToolboxHelper;
use App\Lib\Helpers\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
use App\Traits\Models\MiniCrudModel;
use Illuminate\Database\Eloquent\Model;
use Kwaadpepper\Enum\Traits\CastsEnums;

View file

@ -3,8 +3,8 @@
namespace App\Models;
use App\Enums\PublishedStatus;
use App\Lib\ToolboxHelper;
use App\Lib\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
use App\Lib\Helpers\FileStorageHelper;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

View file

@ -3,8 +3,8 @@
namespace App\Models;
use App\Enums\Products\ProductLabelEnum;
use App\Lib\ToolboxHelper;
use App\Lib\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
use App\Lib\Helpers\FileStorageHelper;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Validation\ValidationException;

View file

@ -3,8 +3,8 @@
namespace App\Models;
use App\Enums\Ranges\RangeCategoryEnum;
use App\Lib\ToolboxHelper;
use App\Lib\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
use App\Lib\Helpers\FileStorageHelper;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Session;

View file

@ -2,7 +2,7 @@
namespace App\Models;
use App\Lib\ToolboxHelper;
use App\Lib\Helpers\ToolboxHelper;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

View file

@ -5,8 +5,8 @@ namespace App\Models;
use App\Casts\RgbaColorNullable;
use App\Enums\Sliders\SliderElementPosition;
use App\Enums\Sliders\SliderType;
use App\Lib\ToolboxHelper;
use App\Lib\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
use App\Lib\Helpers\FileStorageHelper;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Session;

View file

@ -2,7 +2,7 @@
namespace App\Models;
use App\Lib\ToolboxHelper;
use App\Lib\Helpers\ToolboxHelper;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

View file

@ -2,7 +2,7 @@
namespace App\Models;
use App\Lib\ToolboxHelper;
use App\Lib\Helpers\ToolboxHelper;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

View file

@ -2,7 +2,7 @@
namespace App\Providers;
use App\Lib\ToolboxHelper;
use App\Lib\Helpers\ToolboxHelper;
use App\Models\News;
use App\Models\Product;
use App\Models\Range;

View file

@ -3,8 +3,8 @@
namespace App\Traits\Models;
use App\Enums\InputType;
use App\Lib\ToolboxHelper;
use App\Lib\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
use App\Lib\Helpers\FileStorageHelper;
trait MiniCrudModel
{

View file

@ -10,7 +10,14 @@
<div class="row">
<div class="col">
<fieldset class="border p-3 mb-3">
<legend>{{ $input['label'] }}</legend>
<div class="d-flex flex-row justify-content-start align-items-center mb-2">
<legend class="w-auto m-0">{{ $input['label'] }}</legend>
@if(!empty($input['tooltip']))
<span class="ms-1" data-bs-toggle="tooltip" data-bs-html="true" title="{{ $input['tooltip'] }}">
<i class="fa fa-question-circle"></i>
</span>
@endif
</div>
<div class="row mb-3">
<div class="form-group">
<div class="word-counter" data-json='@json(['id' => $k])'></div>
@ -32,7 +39,14 @@
<div class="row">
<div class="col">
<fieldset class="border p-3 mb-3">
<legend>{{ $input['label'] }}</legend>
<div class="d-flex flex-row justify-content-start align-items-center mb-2">
<legend class="w-auto m-0">{{ $input['label'] }}</legend>
@if(!empty($input['tooltip']))
<span class="ms-1" data-bs-toggle="tooltip" data-bs-html="true" title="{{ $input['tooltip'] }}">
<i class="fa fa-question-circle"></i>
</span>
@endif
</div>
<div class="row mb-3">
<div class="form-group">
<input id="{{$k}}" type="number" step="0.01" class="form-control"
@ -54,7 +68,14 @@
<div class="row">
<div class="col">
<fieldset class="border p-3 mb-3">
<legend>{{ $input['label'] }}</legend>
<div class="d-flex flex-row justify-content-start align-items-center mb-2">
<legend class="w-auto m-0">{{ $input['label'] }}</legend>
@if(!empty($input['tooltip']))
<span class="ms-1" data-bs-toggle="tooltip" data-bs-html="true" title="{{ $input['tooltip'] }}">
<i class="fa fa-question-circle"></i>
</span>
@endif
</div>
<div class="row mb-3">
<div class="form-group vueComponent">
@php
@ -81,7 +102,14 @@
<div class="row">
<div class="col">
<fieldset class="border p-3 mb-3">
<legend>{{ $input['label'] }}</legend>
<div class="d-flex flex-row justify-content-start align-items-center mb-2">
<legend class="w-auto m-0">{{ $input['label'] }}</legend>
@if(!empty($input['tooltip']))
<span class="ms-1" data-bs-toggle="tooltip" data-bs-html="true" title="{{ $input['tooltip'] }}">
<i class="fa fa-question-circle"></i>
</span>
@endif
</div>
<div class="row mb-3">
<div class="form-group">
<label class="user-select-none">
@ -105,7 +133,14 @@
<div class="row">
<div class="col">
<fieldset class="border p-3 mb-3">
<legend>{{ __('Image') }}</legend>
<div class="d-flex flex-row justify-content-start align-items-center mb-2">
<legend class="w-auto m-0">{{ $input['label'] }}</legend>
@if(!empty($input['tooltip']))
<span class="ms-1" data-bs-toggle="tooltip" data-bs-html="true" title="{{ $input['tooltip'] }}">
<i class="fa fa-question-circle"></i>
</span>
@endif
</div>
<div class="row mb-3">
<div class="col-12 col-md-6 form-group">
<label for="{{$k}}alt" class="col-form-label">