Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

1.3 KB
Raw Blame
using FluentValidation;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Business.Core.Interfaces.Marketing.Contacts;
using Grand.Domain.Catalog;
using Grand.Infrastructure.Validators;
using Grand.Web.AdminShared.Models.Messages;

namespace Grand.Web.AdminShared.Validators.Messages;

public class ContactAttributeValueValidator : BaseGrandValidator<ContactAttributeValueModel>
{
    public ContactAttributeValueValidator(
        IEnumerable<IValidatorConsumer<ContactAttributeValueModel>> validators,
        IContactAttributeService contactAttributeService,
        ITranslationService translationService)
        : base(validators)
    {
        RuleFor(x => x.Name).NotEmpty()
            .WithMessage(
                translationService.GetResource(
                    "Admin.Catalog.Attributes.ContactAttributes.Values.Fields.Name.Required"));
        RuleFor(x => x).CustomAsync(async (x, context, _) =>
        {
            var contactAttribute = await contactAttributeService.GetContactAttributeById(x.ContactAttributeId);
            if (contactAttribute is { AttributeControlType: AttributeControlType.ColorSquares }
                && string.IsNullOrEmpty(x.ColorSquaresRgb))
                context.AddFailure("Color is required");
        });
    }
}