I have a custom validation attribute class deriving from ValidationAttribute. The error thrown from this class is correctly seen when I edit the form field. I want the error to be also seen when user submits the form/or changes one of the form fields. The attribute is applied on the Zipcode field.
Below is my form:
<TelerikForm Model="@customerDto" Orientation="FormOrientation.Vertical" Width="1000px" Columns="2" ValidationMessageType="@FormValidationMessageType.Tooltip" @ref="customer"><FormValidation><DataAnnotationsValidator></DataAnnotationsValidator></FormValidation><FormItems><FormItem Field="@nameof(customerDto.Country)" LabelText="Country"><Template><div class="k-form-field-wrap"><TelerikDropDownList @bind-Value="@customerDto.Country" DefaultText="Select Country" TItem="@countryDto" TValue="@string" TextField="@nameof(countryDto.Name)" ValueField="@nameof(countryDto.CountryAbb)" Data="@countries" Width="250px" Id="countriesDDL" OnChange="@((object countryId) => OnCountryChanged(countryId))"</TelerikDropDownList></div></Template></FormItem><FormItem Field="@nameof(crfCustomerDto.ZipCode)" LabelText="Zip Code"></FormItem></FormItems><FormButtons></FormButtons></TelerikForm>
I am trying to validate the zip code field based on country selected. Ideally the error should be seen when the country is changed and the zip code doesn't match the country's allowed length for it's value. The error is seen only when I edit the field. I have tried the code below when the country is changed but the error is not seen in the UI.
customer.EditContext.Validate(); var fieldIdentifier = customer.EditContext.Field("Zipcode"); string errors = customer.EditContext.GetValidationMessages().FirstOrDefault(error => error.Contains("Zip")); customerValidationMessageStore.Add(fieldIdentifier, errors); customer.EditContext.NotifyValidationStateChanged();