EasyUI Forum

General Category => EasyUI for React => Topic started by: chrwei on April 21, 2020, 08:04:20 AM



Title: Enhancement suggestion in Form validators
Post by: chrwei on April 21, 2020, 08:04:20 AM
I think it would be useful to have the option of the form validator "message" to be a function that the value and parameters are passed to so that transforms more complex than simple token substitution could be applied to the message.

example:

Code:
percentage: {
    validator: (value, param) => {
        if (parseFloat(value) < (param[0] * param[1])) {
            return false;
        }
        return true;
    },
    message: (value, param) => {
        return "Value (" + value + ") must be " + (param[0]*100) + "% of " + param[1] + " or more";
    }
}


const rules = { field: "percentage[0.25,50]" }

note this is a simplified example, the use case I'd like is actually quite a bit more complex.


Title: Re: Enhancement suggestion in Form validators
Post by: jarry on April 22, 2020, 12:31:09 AM
The 'param' can be converted before displaying the message. Please look at this code.
Code:
percentage: {
  validator: (value, param) => {
    if (parseFloat(value) < (param[0] * param[1])) {
      // reset the parameters to be used by 'message'
      param[0] *= 100;
      param.push(value)
      return false;
    }
    return true;
  },
  message: "Value ({2}) must be {0}% of {1} or more"
}


Title: Re: Enhancement suggestion in Form validators
Post by: chrwei on April 22, 2020, 06:19:20 AM
odd, i had tried that with no effect, but I just tried again and it worked