2

How we can create a custom model validation with both client side and server side validation in asp.net core. My requirement is to check the value of a property and if it satisfies the value, the required field should be enabled. other wise disable. The annotation can also be applied on object properties.Like this.

public class TradeModel
{   
    public bool TradingObjectives { get; set; }

    [RequiredIf("TradingObjectives","true",ErrorMessage="required")]
    public int Hedge{ get; set; }

    [RequiredIf("TradingObjectives","true",ErrorMessage="required")]
    public AddressModel Address{ get; set; }

}

public class AddressModel 
{
    public long AddressId { get; set; }

    [Required]
    public string Address1 { get; set; }

    public string Address2 { get; set; }
}
The_Outsider
  • 1,875
  • 2
  • 24
  • 42
  • Try to refer [How to reset the formatted ErrorMessages of the validation attributes in ASP.NET MVC?](https://stackoverflow.com/questions/53636422/how-to-reset-the-formatted-errormessages-of-the-validation-attributes-in-asp-net/53645586#53645586) – Edward May 13 '19 at 06:58

1 Answers1

0

I think you should use Fluent Validation for custom validators. And you can add client-side support to Fluent Validation with FormHelper.

You can write custom validators, even database queries. It's an amazing solution for you.

Sinan Bozkuş
  • 317
  • 4
  • 9