feat(menu): enhance validation for content_type and static_content_schema in Menu model
This commit is contained in:
@@ -27,8 +27,11 @@ class MenuController extends Controller
|
|||||||
Rule::exists('menues', 'code'),
|
Rule::exists('menues', 'code'),
|
||||||
'different:code',
|
'different:code',
|
||||||
],
|
],
|
||||||
'content_type' => ['sometimes', Rule::in(['static', 'dynamic'])],
|
'content_type' => [
|
||||||
'static_content_schema' => 'nullable|array',
|
'sometimes',
|
||||||
|
Rule::in([Menu::CONTENT_TYPE_STATIC, Menu::CONTENT_TYPE_DYNAMIC]),
|
||||||
|
],
|
||||||
|
'static_content_schema' => 'required_if:content_type,static|nullable|array',
|
||||||
'route' => 'required|string',
|
'route' => 'required|string',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@@ -52,8 +55,11 @@ class MenuController extends Controller
|
|||||||
Rule::exists('menues', 'code'),
|
Rule::exists('menues', 'code'),
|
||||||
Rule::notIn([$menu->code]),
|
Rule::notIn([$menu->code]),
|
||||||
],
|
],
|
||||||
'content_type' => ['sometimes', Rule::in(['static', 'dynamic'])],
|
'content_type' => [
|
||||||
'static_content_schema' => 'nullable|array',
|
'sometimes',
|
||||||
|
Rule::in([Menu::CONTENT_TYPE_STATIC, Menu::CONTENT_TYPE_DYNAMIC]),
|
||||||
|
],
|
||||||
|
'static_content_schema' => 'required_if:content_type,static|nullable|array',
|
||||||
'route' => 'sometimes|required|string',
|
'route' => 'sometimes|required|string',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|||||||
@@ -8,13 +8,22 @@ use Illuminate\Database\Eloquent\Model;
|
|||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
use Illuminate\Validation\ValidationException;
|
||||||
|
|
||||||
class Menu extends Model
|
class Menu extends Model
|
||||||
{
|
{
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
|
|
||||||
|
public const CONTENT_TYPE_DYNAMIC = 'dynamic';
|
||||||
|
|
||||||
|
public const CONTENT_TYPE_STATIC = 'static';
|
||||||
|
|
||||||
protected $table = 'menues';
|
protected $table = 'menues';
|
||||||
|
|
||||||
|
protected $attributes = [
|
||||||
|
'content_type' => self::CONTENT_TYPE_DYNAMIC,
|
||||||
|
];
|
||||||
|
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'code',
|
'code',
|
||||||
'parent_menu_code',
|
'parent_menu_code',
|
||||||
@@ -30,6 +39,20 @@ class Menu extends Model
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected static function booted(): void
|
||||||
|
{
|
||||||
|
static::saving(function (self $menu): void {
|
||||||
|
if (
|
||||||
|
$menu->content_type === self::CONTENT_TYPE_STATIC
|
||||||
|
&& empty($menu->static_content_schema)
|
||||||
|
) {
|
||||||
|
throw ValidationException::withMessages([
|
||||||
|
'static_content_schema' => 'El schema es obligatorio para los menús estáticos.',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public function parent(): BelongsTo
|
public function parent(): BelongsTo
|
||||||
{
|
{
|
||||||
return $this->belongsTo(self::class, 'parent_menu_code', 'code');
|
return $this->belongsTo(self::class, 'parent_menu_code', 'code');
|
||||||
|
|||||||
Reference in New Issue
Block a user