Skip to content

Model Object

valid()

valid() — returns boolean

Available in: model Category: Error Functions

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.

NameTypeRequiredDefaultDescription
callbacksbooleannotrueSet to false to disable callbacks for this method.
validateAssociationsbooleannofalse
// 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());
}