User login

CFWheels taking action on a particular error

http://cfwheels.org/docs/1-1/function/errorson

If you have more than one error on a property (aka column in a database table, as defined in your model) you need to create custom errors so you can name them

For instance, in your User.cfc model in your custom error method you have:

        this.addError(property="email", name="uniqueEmail", message="That e-mail address is already in use.");

(You call this error handler with validate() and the name of the function, see http://cfwheels.org/docs/1-1/function/validate)

The key part for us here is the name.

This allows you to use errorsOn like this, in your Users.cfc controller:

        var emailExistsError = user.errorsOn(property="email", name="uniqueEmail");
        if (!ArrayIsEmpty(emailExistsError)) {
          // Take action on this error...
          // In this case we were doing a writeLog() and a redirectTo().
        }

The User.cfc code in context, with most unrelated code removed:

<cfcomponent extends="Model" output="false">
  <cfscript>
    public function init() {
      hasOne(name="UserProfile", dependent="delete");
      hasMany("Venues");
      automaticValidations(false);
      validatesFormatOf(property="email", type="email", message="The e-mail address is not valid.");
      validatesConfirmationOf(property="password",message="Passwords do not match.");
      validate(methods="uniqueEmail");

    } //end init()

    // Check before updating a user record that e-mail is unique (if editing a user, ignore current user).
    // We are not using validatesUniquenessOf(property="email", ...) because built-in validators cannot name their errors.
    function uniqueEmail() {
    // Only add an ID if we're editing a user.
    if (structKeyExists(this, "id")) {
        var idConditon = " AND id <> #this.id#";
    }
    else {
      var idCondition = "";
    }
      uniquee = model("user").findOne(where="email = '#this.email#'" & idCondition);
      if (isObject(uniquee)) {
        // In Users.cfc we'll use this named error to, if not editing a user, redirect to log in.
        this.addError(property="email", name="uniqueEmail", message="That e-mail address is already in use.");
      }
    }
  </cfscript>

</cfcomponent>

Searched words: 
cfwheels log validation fail cfwheels showing action for particular error

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.