Build a blog with Areto Node.js framework

Sign up form

To handle form data use classes that inherit the areto/base/Model class.

models/SignUpForm.js

'use strict';
const Base = require('areto/base/Model');
module.exports = class SignUpForm extends Base {
    // place methods here
};
module.exports.init(module);
const User = require('./User');

Model validation

The RULES array keep the rules of the model validation. Each array element is a separate rule. It applies to the listed attributes of the model. The rule also is an array consisting of several elements.

  • The first element is the name of attribute with the assigned rule. It may be a string or an array of strings in the case of multiple attributes.
  • The second element is the type of the validator. It can be built-in validator of the Areto framework, name of the method of the current class or a separate class. The list of built-in validator: boolean, compare, date, email, exist, file, filter, image, number, range, regexp, required, string, unique, url.
  • The third element is an object containing additional parameters for the rule. It is optional.

Rules are applied in the order of elements in the array. By default, when the attribute has no value or contains errors of the previous rule, it passed validation. This behavior can be changed by the rule parameters: skipOnError, skipOnAnyError, skipOnEmpty. For example, when a new user registers his name and email will be checked for uniqueness only if there are no errors in the previous rules.

models/SignUpForm.js

...
static getConstants () {
  return {
    RULES: [
      [['name', 'email', 'password', 'passwordRepeat', 'captchaCode'], 'required'],
      ['name', 'string', {min: 3, max: 24}],
      ['name', 'regexp', {pattern: /^[а-яa-z\s-]+$/i}],
      ['email', 'email'],
      ['captchaCode', require('areto/captcha/CaptchaValidator'), {
         CaptchaController: require('../controllers/AuthController')
      }],
      ['password', 'string', {min: 6, max: 24}],
      ['passwordRepeat', 'compare', {compareAttr: 'password'}],
      [['name', 'email'], 'unique', {
         skipOnAnyError: true,
         targetClass: User,
         ignoreCase: true
       }]
     ]
  };
}
...

New user registration

The signUp method checks the data received from the form. If there are no errors, it creates a new User model, initiates form values and pushes into the database. The current session is bound to a new user after successful model saving.

User registration form

All methods working with the database are asynchronous functions. They must explicitly apply the callback at the end or transfer it to another asynchronous function.

models/SignUpForm.js

...
signUp (webuser, cb) {
  this.validate(err => {
    if (err || this.hasError()) {
      cb(err);
    } else {
      let model = new User;
      model.assignAttrs(this);
      model.save(err => {
        if (model.hasError()) {
          this.addError('name', model.getFirstError());
          cb(err);
        } else {
          webuser.login(model, 0, cb);
        }
      });
    }
  })
}
...