Skip to content

Miscellaneous

validatesLengthOf()

validatesLengthOf() — returns any

Validates that the value of the specified property matches the length requirements supplied. Use the exactly, maximum, minimum and within arguments to specify the length requirements.

NameTypeRequiredDefaultDescription
propertiesstringyesName of property or list of property names to validate against (can also be called with the property argument).
messagestringyes[property] is the wrong lengthSupply a custom error message here to override the built-in one.
whenstringyesonSavePass in onCreate or onUpdate to limit when this validation occurs (by default validation will occur on both create and update, i.e. onSave).
allowBlankbooleanyesfalseIf set to true, validation will be skipped if the property value is an empty string or doesn’t exist at all. This is useful if you only want to run this validation after it passes the validatesPresenceOf test, thus avoiding duplicate error messages if it doesn’t.
exactlynumericyes0The exact length that the property value must be.
maximumnumericyes0The maximum length that the property value can be.
minimumnumericyes0The minimum length that the property value can be.
withinstringyesA list of two values (minimum and maximum) that the length of the property value must fall within.
conditionstringyesString expression to be evaluated that decides if validation will be run (if the expression returns true validation will run).
unlessstringyesString expression to be evaluated that decides if validation will be run (if the expression returns false validation will run).
// Make sure that the `firstname` and `lastName` properties are not more than
// 50 characters and use square brackets to dynamically insert the property
// name when the error message is displayed to the user. (The `firstName`
// property will be displayed as "first name".)
validatesLengthOf(
    properties="firstName,lastName",
    maximum=50,
    message="Please shorten your [property] please (50 characters max)."
);

// Make sure that the `password` property is between 4 and 15 characters
validatesLengthOf(
    property="password",
    within="4,20",
    message="The password length must be between 4 and 20 characters."
);