Custom Validaton

(vers 20.2.0.40)

have used grid in my application and I use a edit dialog to edit rows.

The dialog show another grid that allow edit the detail.  In the detail grid, both columns (Merma and Bonificacion) are mutually exclusive. If you set a value in one column, the other column should be null.


26-09-2022 15-36-46.png

I create a custom validation, but unfortunatelly the message is not shown as I expected.

The model:

 public class HumedadGranoItemModel

    {

        public HumedadGranoItemModel()

        {

        }

        public HumedadGranoItemModel(int id, decimal porcentaje, decimal? merma =null, decimal? bonificacion= null)

        {

            Id = id;

            Porcentaje = porcentaje;

            Merma = merma;

            Bonificacion = bonificacion;

        }

        public int Id { get; set; }

        public HumedadGranoModel HumedadGrano { get; set; } = null!;

        public decimal Porcentaje { get; set; }

        [CustomValidationMermas]

        public decimal? Merma { get; set; }

        [CustomValidationMermas]

        public decimal? Bonificacion { get; set; }

    }


And the custom validation :


    [AttributeUsage(AttributeTargets.Property)]

    public class CustomValidationMermas : ValidationAttribute

    {

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)

        {

            if (value != null)

            {

                var item = (HumedadGranoItemModel)validationContext.ObjectInstance;


                if (item.Bonificacion > 0 && item.Merma > 0)

                {

                    return new ValidationResult("La merma y a bonificación son excluyentes");

                }

            }

            return ValidationResult.Success;

        }

    }


I've attached a sample project. 

Please, let me know what is wrong.



Attachment: General_162c2dbb.rar

1 Reply

SP Sarveswaran Palani Syncfusion Team September 27, 2022 04:54 PM UTC

Hi Marcelo,


Greetings from Syncfusion support.


We have checked your query and we would like to inform that validation will not work properly when using it inside form(i.e. nested level of forms). So we suggest you to create a custom component for Edit dialog and render a grid inside the component to perform validation. We have documented an topic using Custom Validator component inside the Template tag of GridEditSettings.


Refer the below UG documentation for your reference


https://blazor.syncfusion.com/documentation/datagrid/column-validation#custom-validator-component


Please follow ticket for further follow up, kindly get back to if you have any further queries


Regards,

Sarveswaran PK


Loader.
Up arrow icon