Minicrud fix

This commit is contained in:
Jérémy Munsch 2023-06-13 13:10:57 +02:00
parent 8d83cd3b4d
commit c9ca0b17bf
No known key found for this signature in database
GPG key ID: 18361AF3BFEC0FEC
3 changed files with 52 additions and 40 deletions

View file

@ -4,6 +4,8 @@ namespace App\Http\Requests\Bo;
use App\Enums\InputType;
use App\Lib\Helpers\ToolboxHelper;
use App\Models\Concept;
use App\Models\News;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\ValidationException;
@ -37,7 +39,16 @@ abstract class UpdateMiniCrudRequest extends FormRequest
{
// phpcs:enable
$rules = [];
foreach ($this->{$this->modelName}->data as $iptName => $input) {
$data = $this->{$this->modelName}->data ?? null;
if (!$data and $this->route()->getName() === 'bo.concepts.store') {
$data = Concept::getEmptyData();
}
if (
!$data and $this->route()->getName() === 'bo.news.store'
) {
$data = News::getEmptyData();
}
foreach ($data as $iptName => $input) {
$input = \json_decode(\json_encode($input), true);
switch ($input['inputType']) {
case InputType::number->value:
@ -80,7 +91,7 @@ abstract class UpdateMiniCrudRequest extends FormRequest
};
}
return $rule;
});
})->all();
$rules = array_merge($rules, [
"data.$iptName.value" => $mergeRules
]);
@ -107,26 +118,17 @@ abstract class UpdateMiniCrudRequest extends FormRequest
'maxWith' => $dim['width'],
];
$rules = array_merge($rules, [
"data.$iptName.value" => [
function (string $attribute, $value, callable $fail) use ($sizes) {
return ToolboxHelper::validatePicture($attribute, $value, $fail, $sizes);
}
],
$this->compileValidate(
implode('|', [
$required,
'image',
'mimes:png,jpg,jpeg',
sprintf(
'dimensions:%s=%d,%s=%d',
$max ? 'max_height' : 'height',
$dim['height'],
$max ? 'max_width' : 'width',
$dim['width']
)
]),
"data.$iptName.value" => $this->compileValidate(
$required,
$input
),
"data.$iptName.value" => [
function (string $attribute, $value, callable $fail) use ($sizes) {
if ($value) {
return ToolboxHelper::validatePicture($attribute, $value, $fail, $sizes);
}
}
],
"data.$iptName.alt" => $this->compileValidate("$required|string|min:5", $input),
"data.$iptName.title" => $this->compileValidate("$required|string|min:5", $input)
]);
@ -185,36 +187,46 @@ abstract class UpdateMiniCrudRequest extends FormRequest
}
}
$test = \collect($inputTypeValidate)->all();
$test = \collect($inputTypeValidate)->sort(function ($valueA, $valueB) {
$valueA = $this->ruleHasToComeFirst($valueA) ? 0 : 2;
$valueB = $this->ruleHasToComeFirst($valueB) ? 1 : 2;
$rules = \collect($inputTypeValidate)->unique()->sort(function ($valueA, $valueB) {
$valueA = $this->ruleHasToComeFirst($valueA);
$valueB = $this->ruleHasToComeFirst($valueB);
return $valueA - $valueB;
})->unique()->all();
});
return \collect($inputTypeValidate)->unique()->sort(function ($valueA, $valueB) {
$valueA = $this->ruleHasToComeFirst($valueA) ? 0 : 2;
$valueB = $this->ruleHasToComeFirst($valueB) ? 1 : 2;
return $valueA - $valueB;
})->all();
$hasRequire = $rules->first(fn ($rule) => $rule === 'required') ? true : false;
$hasRequireCond = $rules->first(fn ($rule) => \strpos($rule, 'required_') !== false) ? true : false;
$hasNullable = $rules->first(fn ($rule) => \strpos($rule, 'nullable') !== false) ? true : false;
$hasSometime = $rules->first(fn ($rule) => \strpos($rule, 'sometimes') !== false) ? true : false;
$hasPresent = $rules->first(fn ($rule) => \strpos($rule, 'present') !== false) ? true : false;
if ($hasRequire and ($hasRequireCond or $hasNullable or $hasSometime or $hasPresent)) {
$rules = $rules->reject(fn ($rule) => $rule === 'required');
}
return $rules->all();
}
/**
* Does the rulle need to come first ?
*
* @param string $rule
* @return boolean
* @return integer
*/
private function ruleHasToComeFirst(string $rule): bool
private function ruleHasToComeFirst(string $rule): int
{
$rule = \collect(\explode(':', $rule, 1))->filter()->first(null, '');
return \collect([
'nullable', 'present', 'required', 'required_if',
$rule = \collect(\explode(':', $rule, 2))->filter()->first(null, '');
$rules = \collect([
'required_if',
'required_unless', 'required_with', 'required_with_all',
'required_without', 'required_without_all', 'required_with_all',
'missing', 'missing_if', 'missing_unless',
'missing_with', 'missing_with_all',
])->first(fn (string $firstRule) => \strpos($rule, $firstRule) === 0) ? true : false;
'nullable', 'sometimes', 'present', 'required'
]);
$itemKey = $rules->search(fn (string $firstRule) => \strpos($rule, $firstRule) === 0);
if (\is_bool($itemKey)) {
return $rules->count() + 1;
}
return $itemKey;
}
/**

View file

@ -166,7 +166,7 @@ trait MiniCrudModel
case InputType::string->value:
case InputType::text->value:
case InputType::url->value:
ToolboxHelper::assertNoInternalBoUrls('content', $input['value']);
ToolboxHelper::assertNoInternalBoUrls('content', $input['value'] ?? '');
break;
default:
throw new \ErrorException(sprintf(

View file

@ -160,7 +160,7 @@
'hasWordCounter' => true,
'name' => 'data['.$k.'][alt]',
'value' => old("data.{$k}.alt", $input['alt'] ?? null),
'required' => true,
'required' => is_bool($input['required'] ?? null) ? $input['required'] : true,
'model' => $model ?? null,
'helper' => __('crud.minicrud.picture_alt_helper') . '. ' . __('validation.min.string', [
'attribute' => __('crud.minicrud.picture_alt_label'),
@ -179,7 +179,7 @@
'hasWordCounter' => true,
'name' => 'data['.$k.'][title]',
'value' => old("data.{$k}.title", $input['title'] ?? null),
'required' => true,
'required' => is_bool($input['required'] ?? null) ? $input['required'] : true,
'model' => $model ?? null,
'helper' => __('crud.minicrud.picture_title_helper') . '. ' . __('validation.min.string', [
'attribute' => __('crud.minicrud.picture_title_label'),
@ -203,7 +203,7 @@
'width' => $input['dimensions']['width'],
'height' => $input['dimensions']['height'],
]),
'required' => true,
'required' => is_bool($input['required'] ?? null) ? $input['required'] : true,
'preview' => true,
])
@include('modules.input-error', [