This is a migrated thread and some comments may be shown as answers.

Custom validation

1 Answer 231 Views
Form
This is a migrated thread and some comments may be shown as answers.
n/a
Top achievements
Rank 1
n/a asked on 16 Sep 2020, 10:09 PM

Hi,

What is the method for defining custom validation in a Form?  For example, in the Kendo UI for jQuery Form validation demo (https://demos.telerik.com/kendo-ui/form/validation) there is custom validation performed on the RetireDate to ensure that it's after the HireDate. However, the ASP.NET Core validation demo (https://demos.telerik.com/aspnet-core/form/validation)  contains no such validation.  Would it be possible to provide an example on how to perform this validation using ASP.NET Core?

Thanks,
Scott

1 Answer, 1 is accepted

Sort by
0
Ivan Danchev
Telerik team
answered on 21 Sep 2020, 02:56 PM

Hello Scott,

The ASP.NET Core Form does not have the validation configuration of the jQuery Form. Custom validation in the Core Form can be done on the server. In the Validation demo, both LastName and RetireDate are validated in the "Validation" action:

[HttpPost]
public ActionResult Validation(UserViewModel model)
{
    int compareDates = Nullable.Compare<DateTime>(model.HireDate, model.RetireDate);

    if (model.FirstName == model.LastName)
    {
        ModelState.AddModelError("LastName", "LastName must be different than First Name.");
    }
    else if (compareDates >= 0)
    {
        ModelState.AddModelError("RetireDate", "Retire Date must be after Hire Date.");
    }

    if (!ModelState.IsValid)
    {
        return View(model);
    }
    else
    {
        TempData["View"] = "Validation";
        return View("Success");
    }
}

The logic used the demo's view and controller can be seen by selecting the "View Source" tab in the demo.

Regards,
Ivan Danchev
Progress Telerik

Five days of Blazor, Angular, React, and Xamarin experts live-coding on twitch.tv/CodeItLive, special prizes, and more, for FREE?! Register now for DevReach 2.0(20).

Tags
Form
Asked by
n/a
Top achievements
Rank 1
Answers by
Ivan Danchev
Telerik team
Share this question
or