We are developing complex forms with many complex business rules such as "FieldY" should be required if FieldX is true and FieldZ is equal to 1 or 2.
From what I understand, validation should be applied on DTO's on the application layer and the cshtml should implement specific attribute on the HTML inputs to render the validation...However in the case of more complex validation rules I cannot figure out how to render the error per field on the view.
Currently the validation is displayed in a modal window after submission of my modal but I cannot show the validation error on the field itself (with a red border)
What are my options ? Should I add unobtrusive validation and create custom data annotation that implement the IClientValidatable? If so where should I place them to follow the best practices?
Thanks in advance
8 Answer(s)
-
0
Hi,
You can think it as two seperate parts. If client validation is passed somehow, the server validation handles it.
So, you need to implement your custom logic both on client and server. For server side I think you have already manged it, this is how we do it <a class="postlink" href="http://aspnetboilerplate.com/Pages/Documents/Validating-Data-Transfer-Objects#DocCustomValidation">http://aspnetboilerplate.com/Pages/Docu ... Validation</a>.
For client side, I suggest you to define custom rules for your client side validation engine. We have used in one of our projects jqueryvalidation library, and you can define custom rules for this library <a class="postlink" href="https://jqueryvalidation.org/rules/">https://jqueryvalidation.org/rules/</a>
If you are using a SPA framework, I think most of them supports custom validation.
-
0
Would you recommend creating custom validation attribute (inherits from the IClientValidatable and ValidationAttribute) ?
Basically, we would place these custom attributes over properties in the application layer on the DTOs. These attribute would point to a javascript file on the Web layer that implements the rule via jquery.validator.unobtrusive.adapters.
If we go through with this, where would you recommend we place these custom annotations class? They will both validate information on the application layer and on the web layer.
Thanks in advance
-
0
Hi,
One more thing, are you sending your request to MVC Controller or to App service ?
-
0
Hello I'm using some fields on the modals that needs to be validated before going to the appservice, after the click on the modal, then the JS associated with the modal is in charge to send the data to the appservice.
But I have a dynamic need for validation, for example for a user1 the field TEXT1 is mandatory and 4 chars length, for another user the validation for TEXT1 should be optional and 10 characters length for example.
On server side: I do not think that injecting a repository containing the configuration validations rules form the database, and then creating a dynamic custom validation at DTO level is something recommended.?
found this, seems to be the same scenario I have. #2583@20821281-340d-4c97-a56d-a284ecae926f.
Yes I was thinking to leave the DTO the higher level DB validation. And then add some logic inside the Application Service class and throw an User friendly error message regarding the validations rules specific for each user.
But I dot like that approach I would like to have she same rules and mark the fields with problems on the form, instead showing an user friendly error message coming from the AppService Class.
I think I will have to duplicate that validation rules coming from DB, as well at client level using JQuery at Modal Form level...
Do you have any recommendation?
-
0
Hi,
I think your approach is good to go. You should not do complex validations in DTOs. Doing that in app or domain service is better.
It is good to return validation rules for current user to client and handle with a client side validation library.
-
0
Hello,
I am working on the same team as @cjannel. Thank you for your responses; they're insightful and really helping me grasp the larger picture.
To follow up your earlier question, we are currently posting the form info through the app service; we would like to add attributes to the Dto definitions in the application layer. So, the questions would be, a: is it feasible to define a custom attribute to use on the Dtos, (which in addition to being based on ComponentModel.DataAnnotations.ValidationAttribute, implements System.Web.Mvc.IClientValidatable to provide the metadata required for our unobtrusive jQuery to work)?; and b: where should the annotation be defined in the project?
Thanks again in advance!
-
0
Hi,
-
is it feasible to define a custom attribute to use on the Dtos
Yes, Your domain might need defining such validation attributes because defualt validation attributes might not ne enough for you. 2. It is better to define them in Application layer because whey are related to your Dtos.
-
-
0
Perfect; thank you for your help!