EasyUI Forum
April 27, 2024, 08:35:03 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Enhancement suggestion in Form validators  (Read 4078 times)
chrwei
Full Member
***
Posts: 219


View Profile Email
« 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.
Logged
jarry
Administrator
Hero Member
*****
Posts: 2262


View Profile Email
« Reply #1 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"
}
Logged
chrwei
Full Member
***
Posts: 219


View Profile Email
« Reply #2 on: April 22, 2020, 06:19:20 AM »

odd, i had tried that with no effect, but I just tried again and it worked
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!