I have a attribute in a form which the attribute is required. I need this required in all my requests except for one request where because of an exception I don't want the attribute to be not required. Is this even possible? If yes, how can I do it?
Asked
Active
Viewed 186 times
0
-
2Use a *different* form or DTO. Even with the same form you probably have different validation rules for the `New` and `Edit` forms. – Panagiotis Kanavos Aug 16 '19 at 10:49
-
can you specify on what case you need to make your required attribute optional? – ebk Aug 16 '19 at 11:08
-
It is for a post request – Gion Rubitschung Aug 16 '19 at 11:42
-
As above - you just need a separate form and separate rules for server side validation – ADyson Aug 16 '19 at 12:17
1 Answers
0
- I absolutely agree with Panagiotis Kanavos that you need to use an another DTO object in this case. This would make your code much easier to read for new developer.
- You can implement IValidatableObject interface on your DTO object. See example here. This seems very similar to what your are searching for.
- Another approach (although, I don't recommend it in this case) is to use some library that do similar validation but you can add some logic in validation. For instance, take a look at FLUENT VALIDATION

Roman Oira-Oira
- 94
- 4