Skip to content

Model Configuration

validatesLengthOf()

validatesLengthOf() — returns void

Available in: model Category: Validation Functions

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
propertiesstringnoName of property or list of property names to validate against (can also be called with the property argument).
messagestringno[property] is the wrong lengthSupply a custom error message here to override the built-in one.
whenstringnoonSavePass in onCreate or onUpdate to limit when this validation occurs (by default validation will occur on both create and update, i.e. onSave).
allowBlankbooleannofalseIf 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.
exactlynumericno0The exact length that the property value must be.
maximumnumericno0The maximum length that the property value can be.
minimumnumericno0The minimum length that the property value can be.
withinstringnoA list of two values (minimum and maximum) that the length of the property value must fall within.
conditionstringnoString expression to be evaluated that decides if validation will be run (if the expression returns true validation will run).
unlessstringnoString expression to be evaluated that decides if validation will be run (if the expression returns false validation will run).
1. Ensure that `firstName` and `lastName` are no more than 50 characters
validatesLengthOf(
    properties="firstName,lastName",
    maximum=50,
    message="Please shorten your [property] please (50 characters max)."
);

2. Ensure `password` is between 4 and 20 characters
validatesLengthOf(
    property="password",
    within="4,20",
    message="The password length must be between 4 and 20 characters."
);

3. Ensure `username` is exactly 8 characters
validatesLengthOf(
    property="username",
    exactly=8,
    message="Username must be exactly 8 characters."
);

4. Only validate if `region` is 'US'
validatesLengthOf(
    property="zipCode",
    exactly=5,
    condition="this.region eq 'US'",
    message="US zip codes must be exactly 5 digits."
);

5. Skip validation if property is blank
validatesLengthOf(
    property="nickname",
    maximum=15,
    allowBlank=true
);