How to add data check function in web-flash
This article is to share with you about how to add data verification function in web-flash, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Entity class
Add validation comments to the corresponding fields in the entity class, taking: cn.enilu.flash.bean.entity.message.MessageTemplate as an example
@ Data@Entity (name= "t_message_template") @ Table (appliesTo = "t_message_template", comment = "message template") public class MessageTemplate extends BaseEntity {@ Column (name= "code", columnDefinition = "VARCHAR (32) COMMENT 'number") @ NotBlank (message = "number cannot be empty") private String code; @ NotBlank (message = "content and can be empty") @ Column (name= "content", columnDefinition = "TEXT COMMENT' content'") private String content @ Column (name= "id_message_sender", columnDefinition = "BIGINT COMMENT 'sender id'") @ NotNull (message = "sender cannot be empty") private Long idMessageSender;....}
For string type data in the above, we use the validation @ NotBlank that cannot be empty, which indicates that the field cannot be a null or an empty string.
For idMessageSender, @ NotNull is used for verification. Since @ NotBlank is used for string verification, it cannot be used for idMessageSender, otherwise an exception will be reported. Here are more common notes:
@ AssertFalse false@AssertTrue check true@DecimalMax (value=,inclusive=) is less than or equal to value,inclusive=true, is less than or equal to @ DecimalMin (value=,inclusive=) and similar above @ Max (value=) less than or equal to value@Min (value=) greater than or equal to value@NotNull check Null@Past check date @ Pattern (regex=,flag=) regular @ Size (min=, max=) string, set, map limit size @ Validate check po entity class controller
In the method of submitting data, you need to use @ Valid to identify and validate the validity of the field input in this class.
Take the cn.enilu.flash.api.controller.message.MessagetemplateController.save method as an example:
@ RequestMapping (method = RequestMethod.POST) @ BussinessLog (value = "Edit message template", key = "name", dict = CommonDict.class) @ RequiresPermissions (value = {Permission.MSG_TPL_EDIT}) public Object save (@ ModelAttribute @ Valid MessageTemplate tMessageTemplate) {messagetemplateService.saveOrUpdate (tMessageTemplate); return Rets.success ();} the front end displays the error message
In order to display the error message more amicably at the front end, the error message is handled uniformly in flash-vue-admin/src/utils/request.js:
If (error.response & & error.response.data.errors) {Message ({message: error.response.data.errors [0] .defaultMessage, type: 'error', duration: 5 * 1000})} above is how to add data verification function in web-flash. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.