Exemplo n.º 1
0
        private void btnValidate_Click(object sender, EventArgs e)
        {
            ClearValidationLabels();

            var j = new JsonDataClass
            {
                FullName     = txtFullName.Text,
                CityName     = txtCity.Text,
                PhoneNumber  = txtPhone.Text,
                EmailAddress = txtEmail.Text
            };

            JsonDataClassValidator validator = new JsonDataClassValidator();

            var results = validator.Validate(j);

            if (!results.IsValid)
            {
                foreach (var failure in results.Errors)
                {
                    switch (failure.PropertyName)
                    {
                    case "FullName":
                        lblValFullName.Text = @"Failed validation. Error was: " + failure.ErrorMessage;
                        break;

                    case "CityName":
                        lblValCityName.Text = @"Failed validation. Error was: " + failure.ErrorMessage;
                        break;

                    case "PhoneNumber":
                        lblVarPhoneNumber.Text = @"Failed validation. Error was: " + failure.ErrorMessage;
                        break;

                    case "EmailAddress":
                        lblVarEmailAddress.Text = @"Failed validation. Error was: " + failure.ErrorMessage;
                        break;
                    }
                }
            }
            else
            {
                var myClass = dataGridView1.DataSource as List <JsonDataClass>;
                if (myClass != null)
                {
                    myClass.Add(j);
                    dataGridView1.DataSource = myClass.ToList();
                }

                _jsonLoopCt   += 1;
                lblLoopID.Text = (_jsonLoopCt + 1).ToString();

                MessageBox.Show(@"Passed Validation", @"Data Inserted Successfully", MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
                ClearTextBoxes();
                bttnLoopBegin.PerformClick();
            }
        }
Exemplo n.º 2
0
        List <RawJsonStepData> GetJsonStepData()
        {
            var myRawList   = JsonConvert.DeserializeObject <List <RawJsonData> >(textBox1.Text);
            var myStep1List = new List <RawJsonStepData>();
            JsonDataClassValidator validator = new JsonDataClassValidator();

            foreach (var r in myRawList)
            {
                var jd = new RawJsonStepData();
                var j  = new JsonDataClass
                {
                    FullName     = r.FullName,
                    CityName     = r.CityName,
                    PhoneNumber  = r.PhoneNumber,
                    EmailAddress = r.EmailAddress
                };
                jd.FullName     = j.FullName;
                jd.CityName     = j.CityName;
                jd.EmailAddress = j.EmailAddress;
                jd.PhoneNumber  = j.PhoneNumber;

                var results = validator.Validate(j);
                if (!results.IsValid)
                {
                    jd.HasErrors  = true;
                    jd.ErrorCount = results.Errors.Count;
                    foreach (var failure in results.Errors)
                    {
                        switch (failure.PropertyName)
                        {
                        case "PhoneNumber":
                            jd.PhoneError = @"Email is invalid";     // + failure.ErrorMessage;
                            break;

                        case "EmailAddress":
                            jd.EmailError = @"Phone is invalid";     // + failure.ErrorMessage;
                            break;
                        }
                    }
                }
                else
                {
                    jd.ErrorCount = 0;
                }

                if (jd.HasErrors)
                {
                    if (string.IsNullOrEmpty(jd.PhoneError) && !string.IsNullOrEmpty(jd.EmailError))
                    {
                        jd.DisplayOutput = jd.EmailError;
                    }
                    if (!string.IsNullOrEmpty(jd.PhoneError) && string.IsNullOrEmpty(jd.EmailError))
                    {
                        jd.DisplayOutput = jd.PhoneError;
                    }
                    if (jd.ErrorCount > 1)
                    {
                        jd.DisplayOutput = @"Email and Phone are invalid.";
                    }
                }
                else
                {
                    jd.DisplayOutput = @"Valid";
                }

                myStep1List.Add(jd);
            }

            return(myStep1List);
        }