- [DEVEXPRESS] Hỗ trợ tìm kiếm highlight không dấu và không khoảng cách trên Gridview Filter
- [C#] Chia sẻ source code phần mềm Image Downloader tải hàng loạt hình ảnh từ danh sách link url
- [C#] Chụp hình và quay video từ camera trên winform
- [C#] Chia sẽ full source code tách file Pdf thành nhiều file với các tùy chọn
- Giới thiệu về Stock Tracker Widget - Công cụ theo dõi cổ phiếu và cảnh báo giá tăng giảm bằng C# và WPF
- [VB.NET] Chia sẻ công cụ nhập số tiền tự động định dạng tiền tệ Việt Nam
- [VB.NET] Hướng dẫn fill dữ liệu từ winform vào Microsoft word
- [VB.NET] Hướng dẫn chọn nhiều dòng trên Datagridview
- Hướng Dẫn Đăng Nhập Nhiều Tài Khoản Zalo Trên Máy Tính Cực Kỳ Đơn Giản
- [C#] Chia sẻ source code phần mềm đếm số trang tập tin file PDF
- [C#] Cách Sử Dụng DeviceId trong C# Để Tạo Khóa Cho Ứng Dụng
- [SQLSERVER] Loại bỏ Restricted User trên database MSSQL
- [C#] Hướng dẫn tạo mã QRcode Style trên winform
- [C#] Hướng dẫn sử dụng temp mail service api trên winform
- [C#] Hướng dẫn tạo mã thanh toán VietQR Pay không sử dụng API trên winform
- [C#] Hướng Dẫn Tạo Windows Service Đơn Giản Bằng Topshelf
- [C#] Chia sẻ source code đọc dữ liệu từ Google Sheet trên winform
- [C#] Chia sẻ source code tạo mã QR MOMO đa năng Winform
- [C#] Chia sẻ source code phần mềm lên lịch tự động chạy ứng dụng Scheduler Task Winform
- [C#] Hướng dẫn download file từ Minio Server Winform
[C#] Hướng dẫn sử dụng thư viện FluentValidation để kiểm tra Form nhập liệu winform
Xin chào các bạn, bài viết hôm nay mình sẻ hướng dẫn các bạn cách sử dụng thư viện FluentValidation để kiểm tra dữ liệu Form nhập liệu trên Winform C#.
[C#] Using Fluent Validation Form
Bất kỳ, khi các bạn thiết kế Form nào để nhập liệu, các bạn đều phải kiểm tra xem form đó người dùng nhập liệu vào có hợp liệu hay không.

Bình thường các bạn sẽ kiểm tra theo cách thông thường như sau:
if(String.IsNullOrEmpty(txtName.Text)
{
   MessageBox.Show("Vui lòng nhập tên của bạn.");
   return;
}Hôm nay, mình giới thiệu đến các bạn thư viện FluentValidation, dùng để thực hiện công việc kiểm tra nhập liệu form.
Các bạn có thể truy cập vào địa chỉ trang chủ https://fluentvalidation.net/ để xem hướng dẫn chi tiết hơn nhé.
Dưới đây là giao diện demo ứng dụng Validate Form C#:

Đầu tiện, các bạn tải Fluent Validation từ Nuget:
PM> Install-Package FluentValidation -Version 9.3.0Full source code c#:
using FluentValidation;
using FluentValidation.Results;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FluentValidateDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            var student = new Student();
            student.ID = txtID.Text;
            student.Name = txtName.Text;
            student.Email = txtEmail.Text;
            student.Address = txtAddress.Text;
            student.Phone = txtPhone.Text;
            var validator = new StudentValidator();
            ValidationResult results = validator.Validate(student);
            errorProvider.Clear();
            if (!results.IsValid)
            {
                foreach (var failure in results.Errors)
                {
                    switch (failure.PropertyName)
                    {
                        case "ID":
                            errorProvider.SetError(txtID, failure.ErrorMessage);
                            txtID.BorderColor = Color.Red;
                            break;
                        case "Name":
                            errorProvider.SetError(txtName, failure.ErrorMessage);
                            txtName.BorderColor = Color.Red;
                            break;
                        case "Email":
                            errorProvider.SetError(txtEmail, failure.ErrorMessage);
                            txtEmail.BorderColor = Color.Red;
                            break;
                        case "Address":
                            errorProvider.SetError(txtAddress, failure.ErrorMessage);
                            txtAddress.BorderColor = Color.Red;
                            break;
                        case "Phone":
                            errorProvider.SetError(txtPhone, failure.ErrorMessage);
                            txtPhone.BorderColor = Color.Red;
                            break;
                    }                   
                   
                }
                
                if(!results.Errors.ToList().Exists(x => x.PropertyName == "ID"))
                {
                    successProvider.SetError(txtID, "Pass");
                    txtID.BorderColor = Color.Green;
                }
                if (!results.Errors.ToList().Exists(x => x.PropertyName == "Name"))
                {
                    successProvider.SetError(txtName, "Pass");
                    txtName.BorderColor = Color.Green;
                }
                if (!results.Errors.ToList().Exists(x => x.PropertyName == "Email"))
                {
                    successProvider.SetError(txtEmail, "Pass");
                    txtEmail.BorderColor = Color.Green;
                }
                if (!results.Errors.ToList().Exists(x => x.PropertyName == "Address"))
                {
                    successProvider.SetError(txtAddress, "Pass");
                    txtAddress.BorderColor = Color.Green;
                }
                if (!results.Errors.ToList().Exists(x => x.PropertyName == "Phone"))
                {
                    successProvider.SetError(txtPhone, "Pass");
                    txtPhone.BorderColor = Color.Green;
                }
            }
            else
            {
                MessageBox.Show("Pass validate Form.");
            }
           
        }
       
        public class Student
        {
            public string ID { set; get; }
            public string Name { set; get; }
            public string Email { set; get; }
            public string Address { set; get; }
            public string Phone { set; get; }
        }
        public class StudentValidator : AbstractValidator<Student>
        {
            public StudentValidator()
            {
                RuleFor(x => x.ID)
                        .NotEmpty().WithMessage("Enter your ID.");
                RuleFor(x => x.Name)
                        .NotEmpty().WithMessage("Enter your Name.");
                RuleFor(x => x.Email)
                .NotEmpty().WithMessage("Email address is required")
                .EmailAddress().WithMessage("Email is not valid");              
                RuleFor(x => x.Address).Length(10, 100).WithMessage("Please specify a valid Address");
                RuleFor(x => x.Phone)
                       .NotEmpty().WithMessage("Phone is required")
                       .Length(10, 11).WithMessage("Please specify a valid Phone");
            }
        }
    }
}
Thanks for watching!

![[C#] Hướng dẫn sử dụng thư viện FluentValidation để kiểm tra Form nhập liệu winform](https://laptrinhvb.net/uploads/users/9a8cb514e4428e85fb4ca07588e9103f.png)

![[C#] Hướng dẫn viết ứng dụng Scan QR code, barcode trực tiếp qua Webcam](https://laptrinhvb.net/uploads/source/csharp/scan_qrcode_realtime_thumb.png)
![[C#] Source code phần mềm Food Order sử dụng FlowLayoutPanel và Usercontrol](https://laptrinhvb.net/uploads/source/vbnet/dat_mon_thumb.jpg)
![[C#] Hướng dẫn show Tooltip tại vị trí control đang active trên winform](https://laptrinhvb.net/uploads/source/vbnet/show_tooltip.gif)
![[C#] Hướng dẫn lưu tất cả hình ảnh từ File Excel vào thư mục window](https://laptrinhvb.net/uploads/source/new_image_baiviet/export_image_from_excel.png)
![[C#] Giới thiệu JSON Web Token và cách đọc chuỗi token](https://laptrinhvb.net/uploads/source/new_image_baiviet/jwt_thumb.png)
![[C#] Hướng dẫn chuyển đổi tập tin hình ảnh XPS sang Bitmap](https://laptrinhvb.net/uploads/source/new_image_baiviet/convert_xps_bitmap_csharp.png)
![[C#] Chia sẽ hàm bỏ dấu tiếng việt trong lập trình Csharp](https://laptrinhvb.net/uploads/source/image_baiviet/3b7f5fe03e368394ff8bd6a95d94ecbf.jpg)
![[C#] Hướng dẫn insert text vào vị trí dấu nháy chuột trong text box](https://laptrinhvb.net/uploads/source/csharp/insert_text_position_csharp_thumb.jpg)
![[C#] Hướng dẫn tạo Windows Services đơn giản Winform](https://laptrinhvb.net/uploads/source/vbnet/windows_services_thumb.jpg)
![[C#] Chia sẽ class convert DataTable to List<T> và convet List<T> sang DataTable](https://laptrinhvb.net/uploads/source/csharp/file-conversion.jpg)
![[C#] Hướng dẫn upload multifile lên google drive Api  csharp](https://laptrinhvb.net/uploads/source/image_baiviet/fcc891991c21fe9fc30a2a82f13989f9.jpg)
![[C#] Chia sẻ source code sử dụng Object Listview trên Winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/OBJECT_LISTVIEW_THUMB.png)
![[C#] Trigger DataRow thêm, sửa, xóa trên DataTable Winform](https://laptrinhvb.net/uploads/source/csharp/datatable_trigger_thumb.png)
![[C#] Đo băng thông, bandwidth, download, ip và upload traffic](https://laptrinhvb.net/uploads/source/image_baiviet/cc93229f1ae2066fcc2a3041c7c5fd92.png)
![[C#] Hướng dẫn đọc tất cả email qua giao thức IMAP Gmail](https://laptrinhvb.net/uploads/source/new_image_baiviet/gmail_thumb.jpg)
![[C#] Chia sẽ Tool Convert Json String to class csharp, vb, typescript](https://laptrinhvb.net/uploads/source/vbnet/json_tool_thumb.png)
![[C#] Hướng dẫn tạo checkbox Datagridview và truyền dữ liệu giữa hai Gridview](https://laptrinhvb.net/uploads/source/image_baiviet/82698fb5a7cd0b476d1c0f2dee94c9be.jpg)
![[C#] Rút gọn đường dẫn link url với TinyURL API](https://laptrinhvb.net/uploads/source/csharp/short_link_url_csharp.png)
![[C#] Hướng dẫn sử dụng Color Translator để đọc mã màu từ HTML và ngược lại](https://laptrinhvb.net/uploads/source/csharp/color_translator_thumb.png)

![[C#] Hướng dẫn tạo hiệu ứng Fancy Text Effect Winform](https://laptrinhvb.net/uploads/source/vbnet/fancy_text_effect_DEMO.jpg)
![[C#] Hướng dẫn download video Youtube trong lap trinh csharp](https://laptrinhvb.net/uploads/source/image_baiviet/05782e95ff7f6b2528efcf60c691985c.png)
![[C#] Invoke là gì? cách sử dụng phương thức Invoke()](https://laptrinhvb.net/uploads/source/csharp/animation_csharp_thumb.gif)
![[C#] Giới thiệu thư viện Siticone UI/UX thiết kế giao diện ứng dụng winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/SITICON_THUMB.png)
![[C#] Hướng dẫn sử dụng List<T>  trong lập trình csharp](https://laptrinhvb.net/uploads/source/csharp/list_t_csharp_thumb.jpg)
