<?php
namespace models;

use \ActiveRecord;
use \IoC;

class Page extends ActiveRecord {

    protected function validate() {
        $i18n = IoC::resolve( 'i18n' );
        $this->validateMaxLength( 'title', 100, $i18n->__( 'too_long_title', 100 ) );
        $this->validateNotEmpty( 'title', $i18n->__( 'title_cannot_be_empty' ) );
        $this->validateRegExpr( 'title', '#^[A-ZÀ-Ÿa-zà-ÿ0-9_ ]*$#', $i18n->__( 'invalid_title') );
        if ( $this->isNewRecord() || $this->isAttributeChanged( 'title' ) ) {
            $this->validateUnique( 'title', $i18n->__( 'page_already_exists', $this->title ) );
        }
    }

    protected function beforeValidate() {
        $this->title = trim( $this->title );
    }

}
