SoFunction
Updated on 2025-03-08

Questions about validator and validate usage in el-form form validation

Problems with validator and validate usage in el-form form validation

When the ElementUI el-form component uses custom form verification rules, it is necessary to ensure that each layer of the custom verification rules calls the callback method, otherwise the validate method of the el-form component cannot enter the callback function.

//This way you can enter the validate callbackcertfNo: [
          { required: true, message: "The ID number cannot be empty", trigger: "blur" },
          {
            trigger: "blur",
            validator: function(rule, value, callback) {
              if (!validIdCard(value)) {
                return callback(new Error("Please enter the correct ID number"));
              }
               //The callback must be called on the outside layer.              return callback();
            }
          }
        ]
//If this is the case, you cannot validate the callbackcertfNo: [
          { required: true, message: "The ID number cannot be empty", trigger: "blur" },
          {
            trigger: "blur",
            validator: function(rule, value, callback) {
              if (!validIdCard(value)) {
               callback(new Error("Please enter the correct ID number"));
              }
            }
          }
        ]

Record the major problems caused

Problem scenario

Class A inherits class B, class A, and B have the same fields, and both use the @NotBlank annotation.

When all parameters are set correctly, if the parameters of the check class A are used to comply with the rules, the error parameter does not comply with the rules.

The code is as follows

//Entity Class@Data
public class A extends B {
    @NotBlank
    private String name;
    private String score;
    @Valid
    private C c;
}
@Data
public class B {
    @NotBlank
    private String name;
    private String score;
}
@Data
public class C {
    @NotBlank
    private String age;
    private String sex;
}
//Set the valuepublic .... pottBean(){
     A a=new A();
     ("Xiao Ming");
    check(a);
}
//Calibration methodpublic .....check(Object obj){
    Validator validator =  ().getValidator();
Set<ConstraintViolation<Object>> constraintViolations = (obj);
  //What is checked in this place is the parameter name that does not conform to the rules. I found that the name is also included. I also set the value. I was puzzled.}

Later, I searched for information from multiple parties and learned that Validator's verification group concept.

An important concept in the Bean Validation specification is group and group sequence. Groups define a subset of constraints.

For a given Object Graph structure, with the concept of a group, there is no need to verify all the constraints in the ObjectGraph, but only a subset of the definition of the group is required.

Complete group verification requires group declarations when the constraint declaration is completed, otherwise the default group is used. Groups also have inherited attributes.

When a certain group is subject to constraint verification, the base class it inherits will also be verified. So when I check the A parameter, I will check the base class B

I was so miserable that I fell in this place again. When I need to verify the objects contained in the entity class, I need to add @Valid annotation to the object, so I'll be careful~

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.