Validating Field with FieldReflector

208 Views Asked by At

I have a property that due to a previous question I disable it.

.Field(new FieldReflector<UploadDocumentForm>(nameof(UploadDocumentForm.StatusContent))
    .SetActive((state) => false))

But nevertheless it is possible that through another option I need to do a validation on that field, and send a message to the user depending on what he selects. Would it be possible to do it? I do not know if a field can be valid having used the "FieldReflector".

thanks in advance

1

There are 1 best solutions below

0
On BEST ANSWER

Yes, you can leverage SetValidate() of FieldReflector Class to implement your requirement.

If you need to prompt user when he failed the valiadation, you can leverage the Feedback property of ValidateResult. If you want to always send message to your ueser, you can leverage Message() of FormBuilder.

Here is the code snippet:

...
private static string msg = "default";
...
...
.Field(new FieldReflector<Classification>(nameof(Choice))
                        .SetValidate(async (state, value) => {
                            var selection = (<ClassName>)value;
                            bool isValid = (int)selection > 2;
                            msg = $"Select Value is {selection}";
                            ValidateResult result = new ValidateResult
                            {
                                IsValid = isValid,
                                Value = selection,
                            };
                            if (!isValid)
                            {
                                result.Feedback = msg;
                            }
                            return result;
                        })
                       )
                   .Message(msg)