How to validate dynamically generated fields server side

后端 未结 2 775
深忆病人
深忆病人 2020-12-04 00:01

I have developed a web application using Spring 3.1

In one of the module I need to save one Operation object having many OperationParameter objects. So in the view I

相关标签:
2条回答
  • 2020-12-04 00:36

    You could create a helper class that validates your OperationParameters based on its data type. In case of strings, check for null or empty strings and so on. The verification class, could also have some context verification like valid e-mail address.

    Having this in hands, before saving a Operations object, you would iterate through Operations list of OperationParameters, checking the data type and/or a field holding the context of the information(e.g. e-mail, weekdays) and calling the the proper validation on your helper class.

    If you do catch an inconsistency, just throw a custom exception describing it. The part of your code that treats it can return a json to your page with the offending field, which in turn will give the feedback to the user (i.e. a red text under the field with your error message).

    0 讨论(0)
  • 2020-12-04 00:44

    After a week of brain storming I've found the solution myself. I am really happy that finally I have achieved this. :)

    For the purpose to validate the dynamically generated fields you need to depend on custom validations on server side. Because if we use the Annotation based validation for dynamically generated fields then after validation it will not come in the controller function.

    So whenever you do custom validations then the validation will be called from the controller function. And there you can generate error on dynamically generated fields in the list.

    If validation is not successful then you need to get back to the jsp page and display errors on the form. You need to display the dynamically generated field values along with their errors using .

    I hope this helps others who also want the same thing to work.

    0 讨论(0)
提交回复
热议问题