Example #1
0
        protected void GridView_RowValidating(object sender, DevExpress.Web.Data.ASPxDataValidationEventArgs e)
        {
            CompanyVM item = new CompanyVM();

            if (GridView.IsNewRowEditing)
            {
                //foreach (var itemKey in e.NewValues.Keys)
                foreach (GridViewColumn column in GridView.Columns)
                {
                    GridViewDataColumn dataColumn = column as GridViewDataColumn;
                    if (dataColumn == null)
                    {
                        continue;
                    }

                    PropertyInfo myFieldInfo = typeof(CompanyVM).GetProperty(dataColumn.FieldName);
                    if (myFieldInfo == null)
                    {
                        continue;
                    }

                    myFieldInfo.SetValue(item, e.NewValues[dataColumn.FieldName]);

                    ValidationContext context = new ValidationContext(item)
                    {
                        MemberName = dataColumn.FieldName
                    };
                    var errors = new List <ValidationResult>();

                    var myProperty = item.GetType().GetProperty(dataColumn.FieldName).GetValue(item, null);

                    bool isValid = Validator.TryValidateProperty(myProperty, context, errors);

                    if (!isValid)
                    {
                        //e.Errors[dataColumn] = errors[0].ErrorMessage;
                        AddError(e.Errors, dataColumn, errors[0].ErrorMessage);
                    }
                    //var myPropertyInfo = item.GetType().GetProperty(dataColumn.FieldName);
                    //myFieldInfo.SetValue(item, e.NewValues[itemKey.ToString()]);
                }
            }
            else
            {
                //foreach (var itemKey in e.NewValues.Keys)
                foreach (GridViewColumn column in GridView.Columns)
                {
                    GridViewDataColumn dataColumn = column as GridViewDataColumn;
                    if (dataColumn == null)
                    {
                        continue;
                    }

                    PropertyInfo myFieldInfo = typeof(CompanyVM).GetProperty(dataColumn.FieldName);
                    if (myFieldInfo == null)
                    {
                        continue;
                    }

                    myFieldInfo.SetValue(item, e.NewValues[dataColumn.FieldName]);

                    ValidationContext context = new ValidationContext(item)
                    {
                        MemberName = dataColumn.FieldName
                    };
                    var errors = new List <ValidationResult>();

                    var myProperty = item.GetType().GetProperty(dataColumn.FieldName).GetValue(item, null);

                    bool isValid = Validator.TryValidateProperty(myProperty, context, errors);

                    if (!isValid)
                    {
                        if (dataColumn.FieldName != "Id" && dataColumn.FieldName != "UserName" && dataColumn.FieldName != "Password" && dataColumn.FieldName != "ConfirmPassword")
                        {
                            //e.Errors[dataColumn] = errors[0].ErrorMessage;
                            AddError(e.Errors, dataColumn, errors[0].ErrorMessage);
                        }
                    }
                    //var myPropertyInfo = item.GetType().GetProperty(dataColumn.FieldName);
                    //myFieldInfo.SetValue(item, e.NewValues[itemKey.ToString()]);
                }
            }
            if (e.Errors.Count > 0)
            {
                e.RowError = "Vui lòng nhập đầy đủ thông tin theo yêu cầu.";
            }
        }
Example #2
0
        protected void GridView_RowValidating(object sender, DevExpress.Web.Data.ASPxDataValidationEventArgs e)
        {
            CompanyVM item = new CompanyVM();

            //foreach (var itemKey in e.NewValues.Keys)
            foreach (GridViewColumn column in GridView.Columns)
            {
                GridViewDataColumn dataColumn = column as GridViewDataColumn;
                if (dataColumn == null)
                {
                    continue;
                }

                PropertyInfo myFieldInfo = typeof(CompanyVM).GetProperty(dataColumn.FieldName);
                if (myFieldInfo == null)
                {
                    continue;
                }

                myFieldInfo.SetValue(item, e.NewValues[dataColumn.FieldName]);

                ValidationContext context = new ValidationContext(item)
                {
                    MemberName = dataColumn.FieldName
                };
                var errors = new List <ValidationResult>();

                var myProperty = item.GetType().GetProperty(dataColumn.FieldName).GetValue(item, null);

                bool isValid = Validator.TryValidateProperty(myProperty, context, errors);

                if (!isValid)
                {
                    //e.Errors[dataColumn] = errors[0].ErrorMessage;
                    AddError(e.Errors, dataColumn, errors[0].ErrorMessage);
                }
                //var myPropertyInfo = item.GetType().GetProperty(dataColumn.FieldName);
                //myFieldInfo.SetValue(item, e.NewValues[itemKey.ToString()]);
            }


            //foreach (GridViewColumn column in GridView.Columns)
            //{
            //    GridViewDataColumn dataColumn = column as GridViewDataColumn;
            //    if (dataColumn == null) continue;

            //    PropertyInfo myProperty = item.GetType().GetProperty(dataColumn.FieldName);

            //    var ctx = new ValidationContext(myProperty);
            //    var errors = new List<ValidationResult>();
            //    var myPropertyInfo = item.GetType().GetProperty(dataColumn.FieldName);

            //    isValid = Validator.TryValidateProperty(myPropertyInfo, ctx, errors);

            //    if (!isValid)
            //    {
            //        e.Errors[dataColumn] = errors.ToString();
            //    }
            //}

            // Annotation.GetAttribute(typeof(CompanyVM));
            // Checks for null values.

            //foreach (GridViewColumn column in GridView.Columns)
            //{
            //    GridViewDataColumn dataColumn = column as GridViewDataColumn;
            //    if (dataColumn == null) continue;
            //    if (e.NewValues[dataColumn.FieldName] == null)
            //    {
            //        // in an array of System.Reflection.MemberInfo objects.
            //        PropertyInfo MyPropertyInfo = typeof(CompanyVM).GetProperty(dataColumn.FieldName);

            //        RequiredAttribute att = (RequiredAttribute)Attribute.GetCustomAttribute(MyPropertyInfo, typeof(RequiredAttribute));
            //        if (att != null)
            //        {
            //            e.Errors[dataColumn] = att.ErrorMessage;
            //        }
            //    }
            //}



            //            if (e.NewValues["ContactName"] != null &&
            //                e.NewValues["ContactName"].ToString().Length< 2)
            //            {
            //                AddError(e.Errors, GridView.Columns["ContactName"],
            //                "Contact Name must be at least two characters long.");
            //    }
            //            if (e.NewValues["CompanyName"] != null &&
            //            e.NewValues["CompanyName"].ToString().Length< 2)
            //            {
            //                AddError(e.Errors, GridView.Columns["CompanyName"],
            //                "Company Name must be at least two characters long.");
            //}

            //            if (e.NewValues["FirstName"] != null && e.NewValues["FirstName"].ToString().Length< 2)
            //            {
            //                AddError(e.Errors, GridView.Columns["FirstName"], "First Name must be at least two characters long.");
            //            }
            //            if (e.NewValues["LastName"] != null && e.NewValues["LastName"].ToString().Length< 2)
            //            {
            //                AddError(e.Errors, GridView.Columns["LastName"], "Last Name must be at least two characters long.");
            //            }
            //            if (e.NewValues["Email"] != null && !e.NewValues["Email"].ToString().Contains("@"))
            //            {
            //                AddError(e.Errors, GridView.Columns["Email"], "Invalid e-mail.");
            //            }

            //            int age = e.NewValues["Age"] != null ? (int)e.NewValues["Age"] : 0;
            //            if (age< 18)
            //            {
            //                AddError(e.Errors, GridView.Columns["Age"], "Age must be greater than or equal 18.");
            //            }
            //            //DateTime arrival = DateTime.MinValue;
            //            //DateTime.TryParse(e.NewValues["ArrivalDate"] == null ? string.Empty : e.NewValues["ArrivalDate"].ToString(), out arrival);
            //            //if (DateTime.Today.Year != arrival.Year || DateTime.Today.Month != arrival.Month)
            //            //{
            //            //    AddError(e.Errors, GridView.Columns["ArrivalDate"], "Arrival date is required and must belong to the current month.");
            //            //}

            //            if (string.IsNullOrEmpty(e.RowError) && e.Errors.Count > 0)
            //                e.RowError = "Please, correct all errors.";



            // Displays the error row if there is at least one error.
            //isValid = Validator.TryValidateObject(item, ctx, errors, true);

            if (e.Errors.Count > 0)
            {
                e.RowError = "Vui lòng nhập đầy đủ thông tin theo yêu cầu.";
            }
        }