feat(menu): enhance validation for content_type and static_content_schema in Menu model
This commit is contained in:
@@ -8,13 +8,22 @@ use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class Menu extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
public const CONTENT_TYPE_DYNAMIC = 'dynamic';
|
||||
|
||||
public const CONTENT_TYPE_STATIC = 'static';
|
||||
|
||||
protected $table = 'menues';
|
||||
|
||||
protected $attributes = [
|
||||
'content_type' => self::CONTENT_TYPE_DYNAMIC,
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'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
|
||||
{
|
||||
return $this->belongsTo(self::class, 'parent_menu_code', 'code');
|
||||
|
||||
Reference in New Issue
Block a user