Model Configuration
validatesPresenceOf()
Signature
Section titled “Signature”validatesPresenceOf() — returns void
Available in: model
Category: Validation Functions
Description
Section titled “Description”Validates that the specified property exists and that its value is not blank.
Parameters
Section titled “Parameters”| Name | Type | Required | Default | Description |
|---|---|---|---|---|
properties | string | no | — | Name of property or list of property names to validate against (can also be called with the property argument). |
message | string | no | [property] can't be empty | Supply a custom error message here to override the built-in one. |
when | string | no | onSave | Pass in onCreate or onUpdate to limit when this validation occurs (by default validation will occur on both create and update, i.e. onSave). |
condition | string | no | — | String expression to be evaluated that decides if validation will be run (if the expression returns true validation will run). |
unless | string | no | — | String expression to be evaluated that decides if validation will be run (if the expression returns false validation will run). |
Examples
Section titled “Examples”// 1. Require a single property (must exist and not be blank)
validatesPresenceOf("emailAddress");
// 2. Require multiple properties at once
validatesPresenceOf(properties="firstName,lastName,emailAddress");
// 3. Supply a custom error message
validatesPresenceOf(properties="title", message="A title is required.");
// 4. Only validate on create (skip when updating an existing record)
validatesPresenceOf(properties="password", when="onCreate");
// 5. Conditionally require a property based on another property value
validatesPresenceOf(properties="companyName", condition="this.accountType eq 'business'");
// 6. Skip validation when a certain condition is true
validatesPresenceOf(properties="bio", unless="this.isGuest()");