CMDBuild Forum

Validation rules for attributes

Hi there,

often asked but no satisfying answers here:
I’m trying to set up a validation rule for an integer-attribute. The requirement is quite simple: the value inserted shall range between 0 and 2. Nothing else.
I’ve tried thousands of code-pieces with same result: after updating the data management module I get a white input form without any chance to put data there.

Thanks for your reply.
MKL

Test this in the validation rules field. Simply replace ‘Number’ by the name of your field.

if (api.getValue(‘Number’) >=0 && api.getValue(‘Number’) <=2) return true;
return false;

The result, is that if the value not in the range, the Save Button is not active anymore, back to normal if value is in range
Oli

Hi,

thank you for sharing this.
Positive: the input form is not longer blank. But, sadly, there’s no effect on the Save Button.

Regards

MKL

Hi again,

checked this one more time - It works with double quotes perfectly:

if (api.getValue(Number) >=0 && api.getValue(Number) <=2) return true;
return false;

Thanks again

MKL

1 Like

Another example to complete this one:

  • You can also add reg expressions with the function api.testRegExp(<_expression>, <_value>)
  • You can return a String as error message

if (api.testRegExp(/^([0-2])$/g, api.getValue(“Number”))) return true;
return “Number must be between 0 and 2”;

1 Like