Welcome to FoxLearn
In this video, I'll show you how to validate user input using the FluentValidation library in C# Windows Forms Application
Open your Visual Studio, then create a new Windows Forms Application project
Next, We'll create a Student class with some properties to practice how to use the FluentValidation library to perform validation in a Windows Forms Application.
Next, Rebuild your project, then add a DataSource to your form.
You can design a simple UI that allows you to enter student information
Next, Right-click on your project, then select Manage Nuget Packages
Next, Select the Browse Tab, then search and install the FluentValidation library to your project.
The Fluent Validation is a A .NET library for building strongly-typed validation rules.
Next, Right-click on your project, then add a StudentValidator class that allows you to define a set of validation rules for a particular object
To define a set of validation rules for a particular object,
you will need to create a class that inherits from AbstractValidator
The validation rules themselves should be defined in the validator class’s constructor.
To specify a validation rule for a particular property, call the RuleFor method,
passing a lambda expression that indicates the property that you wish to validate.
To run the validator, instantiate the validator object and call the Validate method, passing in the object to validate.
The Validate method returns a ValidationResult object.
This contains two properties
The IsValid property that says whether the validation suceeded.
The Errors property is a collection of ValidationFailure objects containing details about any validation failures.
Thank you for watching this video and don't forget to subscribe to my channel