About RequiredValidator

Hello Radzen Team!

First of all, there are many threads about this component and I've read them all, but due to the lack of documentation on the DefaultValue property I require assistance.

In my form there is a document to be selected, a document number, and an expiration date. The latter depends on the document.temporary property to be required (you only set expiration on temporary documents). Now I believe everything is about the DefaultValue property, that defines what is and what isn't considered valid by the validator.

This is how my expiration required validator default value looks like:

${companyDocument.SiDocument?.temporary ?? false ? null : (int?)0}

Above code does not work as expected and the expiration is always required no matter what. By the way, the expiration field is nullable and when debugging the document.temporary value loads correctly.

There is of course another option which is just hiding the components, but the user should not know if a document is temporary or not until he submits the form.

Maybe my (int?)0 is considered as null inside the validation? Other numbers did not change the outcome.

Hi @kim,

Here is the complete implementation of the RequiredValidator:

public class RadzenRequiredValidator : ValidatorBase
   public override string Text { get; set; } = "Required";

   protected override bool Validate(IRadzenFormComponent component)
     return component.HasValue && !object.Equals(DefaultValue, component.GetValue());

   public object DefaultValue { get; set; }

The DefaultValue is the value which the validated component value is compared against.

So I take that nullable properties are always required to have a value in order to be valid.
Thanks for the info, @korchev.