Model Object
valid()
Signature
Section titled “Signature”valid() — returns boolean
Available in: model
Category: Error Functions
Description
Section titled “Description”Runs the validation on the object and returns true if it passes it.
Wheels will run the validation process automatically whenever an object is saved to the database, but sometimes it’s useful to be able to run this method to see if the object is valid without saving it to the database.
Parameters
Section titled “Parameters”| Name | Type | Required | Default | Description |
|---|---|---|---|---|
callbacks | boolean | no | true | Set to false to disable callbacks for this method. |
validateAssociations | boolean | no | false |
Examples
Section titled “Examples”// 1. Check if a new user object passes validation before proceeding
user = model("User").new(params.user);
if (user.valid()) {
// object passed all validations, safe to proceed
redirectTo(action="dashboard");
} else {
renderView(action="new");
}
// 2. Validate without running before/after validation callbacks
user = model("User").new(params.user);
if (user.valid(callbacks=false)) {
user.save(callbacks=false);
}
// 3. Validate the object and any associated (nested) objects together
order = model("Order").new(params.order);
if (order.valid(validateAssociations=true)) {
order.save();
} else {
// errors may include issues from associated line items
writeOutput(order.errorsAsHTML());
}