Esempio n. 1
0
        private static void Main(string[] args)
        {
            // !!! Don't delete this line0
            // Print UTF8 character in console
            Console.OutputEncoding = Encoding.UTF8;
            Info info = new Info("112312312312312Ac", "12/12/2019");

            ClassValidator autoValidator = new ClassValidator(typeof(Info));

            autoValidator.Validate(info).ForEach(
                x => Console.WriteLine(x.IsValid + " " + x.ErrorMessage)
                );

            Console.WriteLine("----------------------------------------");

            autoValidator.ValidateByPropertyName(info, nameof(Info.Name)).ForEach(
                x => Console.WriteLine(x.IsValid + " " + x.ErrorMessage)
                );

            Console.WriteLine("----------------------------------------");

            UserValidator infoValidate = new UserValidator("2", 3);

            infoValidate.Validate().ForEach(
                x => Console.WriteLine(x.IsValid + " " + x.ErrorMessage)
                );

            Console.WriteLine("----------------------------------------");

            infoValidate.ValidateByPropertyName(nameof(UserValidator.Email)).ForEach(
                x => Console.WriteLine(x.IsValid + " " + x.ErrorMessage)
                );

            Console.WriteLine("----------------------------------------");

            FieldValidator validator = RuleBuilder.Create()
                                       .AddRule(new MinLength(8))
                                       .AddRule(new HasUpperCase())
                                       .Build();

            string str = "string need to validate";

            validator.Validate(str).ForEach(
                x => Console.WriteLine(x.IsValid + " " + x.ErrorMessage)
                );

            Console.WriteLine("----------------------------------------");
        }
Esempio n. 2
0
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            bool isTrueName     = true;
            bool isTrueEmail    = true;
            bool isTruePassword = true;
            bool isTruePhone    = true;
            bool isTrueDate     = true;
            //bool isPopUpShowed = false;

            string CheckBoxErrorString = "";

            lbPhone.Visible     = false;
            lbName.Visible      = false;
            lbPassword.Visible  = false;
            lbBirthdate.Visible = false;
            lbEmail.Visible     = false;

            if (isFirstTimeClickEmail)
            {
                txtEmail.Text = "";
            }
            if (isFirstTimeClickDate)
            {
                txtBirthdate.Text = "";
            }
            if (isFirstTimeClickName)
            {
                txtName.Text = "";
            }
            if (isFirstTimeClickPassword)
            {
                txtPassword.Text = "";
            }
            if (isFirstTimeClickPhone)
            {
                txtPhone.Text = "";
            }

            Info info = new Info(txtEmail.Text, txtPassword.Text, txtName.Text, txtPhone.Text, txtBirthdate.Text);

            ClassValidator autoValidator = new ClassValidator(typeof(Info));

            //Email
            #region
            autoValidator.ValidateByPropertyName(info, nameof(Info.Email)).ForEach(
                x =>
            {
                if (!x.IsValid)
                {
                    if (chbPopup.Checked)
                    {
                        //if (!isPopUpShowed)
                        //{
                        //    MessageBox.Show("Email: " + x.ErrorMessage, "Error", MessageBoxButtons.OK);
                        //    isPopUpShowed = true;
                        //}

                        CheckBoxErrorString += "Email: " + x.ErrorMessage + "\n";
                    }
                    else
                    {
                        isTrueEmail     = false;
                        lbEmail.Text    = x.ErrorMessage;
                        lbEmail.Visible = true;
                    }
                }
            }
                );
            if (isTrueEmail)
            {
                lbEmail.Visible = false;
            }
            #endregion

            //Password
            #region
            autoValidator.ValidateByPropertyName(info, nameof(Info.Password)).ForEach(
                x =>
            {
                if (!x.IsValid)
                {
                    if (chbPopup.Checked)
                    {
                        //if (!isPopUpShowed)
                        //{
                        //    MessageBox.Show("Password: "******"Error", MessageBoxButtons.OK);
                        //    isPopUpShowed = true;
                        //}

                        CheckBoxErrorString += "Password: "******"\n";
                    }
                    else
                    {
                        isTruePassword     = false;
                        lbPassword.Text    = x.ErrorMessage;
                        lbPassword.Visible = true;
                    }
                }
            }
                );
            if (isTruePassword)
            {
                lbPassword.Visible = false;
            }

            #endregion

            //Name
            #region
            autoValidator.ValidateByPropertyName(info, nameof(Info.Name)).ForEach(
                x =>
            {
                if (!x.IsValid)
                {
                    if (chbPopup.Checked)
                    {
                        //if (!isPopUpShowed)
                        //{
                        //    MessageBox.Show("Name: " + x.ErrorMessage, "Error", MessageBoxButtons.OK);
                        //    isPopUpShowed = true;
                        //}

                        CheckBoxErrorString += "Name: " + x.ErrorMessage + "\n";
                    }
                    else
                    {
                        isTrueName     = false;
                        lbName.Text    = x.ErrorMessage;
                        lbName.Visible = true;
                    }
                }
            }
                );
            if (isTrueName)
            {
                lbName.Visible = false;
            }
            #endregion

            //Phone
            #region
            autoValidator.ValidateByPropertyName(info, nameof(Info.PhoneNumber)).ForEach(
                x =>
            {
                if (!x.IsValid)
                {
                    if (chbPopup.Checked)
                    {
                        //if (!isPopUpShowed)
                        //{
                        //    MessageBox.Show("Phone number: " + x.ErrorMessage, "Error", MessageBoxButtons.OK);
                        //    isPopUpShowed = true;
                        //}

                        CheckBoxErrorString += "Phone number: " + x.ErrorMessage + "\n";
                    }
                    else
                    {
                        isTruePhone     = false;
                        lbPhone.Text    = x.ErrorMessage;
                        lbPhone.Visible = true;
                    }
                }
            }
                );
            if (isTruePhone)
            {
                lbPhone.Visible = false;
            }
            #endregion

            //Date
            #region
            autoValidator.ValidateByPropertyName(info, nameof(Info.BirthDate)).ForEach(
                x =>
            {
                if (!x.IsValid)
                {
                    if (chbPopup.Checked)
                    {
                        //if (!isPopUpShowed)
                        //{
                        //    MessageBox.Show("Date: " + x.ErrorMessage, "Error", MessageBoxButtons.OK);
                        //    isPopUpShowed = true;
                        //}

                        CheckBoxErrorString += "Date: " + x.ErrorMessage + "\n";
                    }
                    else
                    {
                        isTrueDate          = false;
                        lbBirthdate.Text    = x.ErrorMessage;
                        lbBirthdate.Visible = true;
                    }
                }
            }
                );
            if (isTrueDate)
            {
                lbBirthdate.Visible = false;
            }
            #endregion

            if (chbPopup.Checked)
            {
                MessageBox.Show(CheckBoxErrorString, "Error", MessageBoxButtons.OK);
            }
        }