Data Annotation in Grid


13 Replies

SK Sujith Kumar Rajkumar Syncfusion Team June 21, 2021 08:45 AM UTC

Hi Pio, 
 
Greetings from Syncfusion support. 
 
Based on the mentioned title, we suspect that your requirement is to know whether Grid edit validation can be performed using data annotation. If so, we would like to let you know that it is possible to perform the validation using data annotations and we have prepared a sample to demonstrate this case for your reference. 
 
Please find the sample below, 
 
 
If we misunderstood your query or if you require any further assistance, then please get back to us. 
 
Regards, 
Sujith R 



PL Pio Luca Valvona replied to Sujith Kumar Rajkumar June 21, 2021 09:45 AM UTC

Hi Sujith,

thank you for your support.

I can't figure out where the problem is, because the code in the sample is almost identical to mine, but field validation doesn't work for me.



PL Pio Luca Valvona replied to Pio Luca Valvona June 21, 2021 03:15 PM UTC

I think I understand the problem, to use validation I add a Metadata class to the class generated by entity framework, this works for page markup, but not for Syncfusion components. I tried to create a view-model class with data-annotations and by switching this type to the grid, required validation worked, but it displayed a different message to what I had imposed on the Required attribute.


 [MetadataType(typeof(ImpostazioneMetadata))]

    public partial class Impostazione

    {

    }

    public class ImpostazioneMetadata

    {

        [Key]

        public Guid IdImpostazione { get; set; }


        [Display(Name = "Nome", ResourceType = typeof(Resources.Impostazioni))]

        public string Nome { get; set; }


        [Display(Name = "Valore", ResourceType = typeof(Resources.Impostazioni))]

        [Required(ErrorMessageResourceType = typeof(Resources.Impostazioni), ErrorMessageResourceName = "ValoreObbligatorio")]

        public string Valore { get; set; }

    }


In all this, you can solve the problem, without using a view-model, and why is the error message different from that of the attribute?



SK Sujith Kumar Rajkumar Syncfusion Team June 22, 2021 09:17 AM UTC

Hi Pio, 
 
We checked the reported problem but unfortunately were unable to reproduce it from our end as the validation message was displayed according to the specified error message resource name. Please check the below modified sample and provided information for reference, 
 
 
Error resource used: 
 
[Required(ErrorMessageResourceName = "ValidationErrorRequiredField")] 
public string CustomerID { get; set; } 
 
Validation error displayed: 
 
 
 
So please confirm us the following information to validate further on this, 
 
  • Let us know if you are able to reproduce/replicate your problem scenario in the above provided sample. It would be helpful to identify your exact problem case and provide the solution based on that.
  • Have you set validation rules in the Grid columns? If so, it might be causing to display the default validation message set in the Grid. Try removing that validation and ensure if the proper message is displayed.
  • Let us know the current validation message displayed and the error message that you need to be displayed.
  • Syncfusion package version used.
  • Share us the Grid code file to ensure with your Grid settings.
 
Regards, 
Sujith R 



PL Pio Luca Valvona replied to Sujith Kumar Rajkumar June 22, 2021 10:36 AM UTC

Hi Sujith,

Thank you for your support.

I will check whether the proposed amendment works.

But, it's not clear to me if I can use a metadatatype class to "tie" data annotations or I'm forced to use a view-model.



SK Sujith Kumar Rajkumar Syncfusion Team June 23, 2021 11:23 AM UTC

Hi Pio,


The data annotations support is only provided using the view-model approach for the Grid control. We apologize for not making it clear in our previous update. So we suggest you to use the view-model approach if you need to define data annotations.

Let us know if you have any concerns

Regards,
Sujith R




PL Pio Luca Valvona replied to Sujith Kumar Rajkumar June 23, 2021 05:54 PM UTC

Hi Sujith,

Thank you for your support.

But I can't translate the error message, as predicted by the model.

This is my Data-annotation:

[Display(Name = "Valore", ResourceType = typeof(Resources.Impostazioni))][Required(ErrorMessageResourceType = typeof(Resources.Impostazioni), ErrorMessageResourceName = "ValoreObbligatorio")]        public string Valore { get; set; }


This shows the following message: "Il campo "Valore" è obbligatorio"


But in the template dialog it appears as follows:

Immagine 2021-06-23 195321.jpg


 




PL Pio Luca Valvona replied to Pio Luca Valvona June 23, 2021 10:38 PM UTC

To confirm what was indicated in the previous message, in another view-model class I entered the EmailAddress attribute, but in the grid dialog, there is no error message if you enter an incorrect email address, for example without @.


It seems that data-annotations are not read or not in the correct way.



SK Sujith Kumar Rajkumar Syncfusion Team June 24, 2021 12:43 PM UTC

Hi Pio, 
 
Sorry for the inconvenience. For custom translated validation texts, we suggest you to set them directly to the ErrorMessage property of the data annotation attribute as demonstrated in the below code snippet in order for them to be displayed on Grid validation, 
 
[Required(ErrorMessage = "Il campo Valore è obbligatorio")] 
public string CustomerID { get; set; } 
  
Also the email validation was working property on using the ‘EmailAddress’ data annotation when defined like below, 
 
[EmailAddress] 
public string Email { get; set; } 
 
You can check the below updated sample for your reference, 
 
 
So regarding the email validation problem, please ensure your application with the above shared sample and if problem persists then share us the code snippet(of the email validation) and if possible a replication sample to validate further on this. 
 
Regards, 
Sujith R 



PL Pio Luca Valvona June 24, 2021 01:43 PM UTC

Hi  Sujith,

I modified the example you sent, adding a resource and a view-model, and the error message continues to be the standard one and not the expected one.


For the email issue, this is the portion of code used in the grid editing template:


        <div class="col-sm-6 mb-3 mb-sm-0">

            @Html.LabelFor(m => m.Email, new { @class = "text-xs font-weight-bold text-uppercase mb-1" })

            @Html.EditorFor(m => m.Email, new { htmlattributes = new { @class = "form-control-grid" } })

            @Html.ValidationMessageFor(m => m.Email, "", new { @class = "small text-danger" })

        </div>



Attachment: GridUpdated1568965575_772b2c48.zip


SK Sujith Kumar Rajkumar Syncfusion Team June 25, 2021 12:12 PM UTC

Hi Pio, 
 
Based on the modified sample shared, we could see that you are using custom translated validation text from resource model for the required and email validation. So as mentioned in our previous update, for this case we suggest you to provide the custom validation text directly to the ErrorMessage data annotation attribute(as demonstrated in the previous update and shared sample). 
 
[Required(ErrorMessage = "Il campo Valore è obbligatorio")]  
public string CustomerID { get; set; }  
 
If we misunderstood the problem case that you are reporting or if you have any further queries, then please get back to us. 
 
Regards, 
Sujith R 



PL Pio Luca Valvona June 25, 2021 12:35 PM UTC

Hi Sujith,

So can't I use resx to translate messages?



SK Sujith Kumar Rajkumar Syncfusion Team July 5, 2021 12:47 PM UTC

Hi Pio, 

Sorry for the delay in providing the response. 

Currently the Grid does not support using custom translated messages from resx for validation. So we suggest you to use the approach provided in our previous update for defining custom validation error messages. 

Regards, 
Sujith R 


Loader.
Up arrow icon