Overview

Namespaces

  • MvcCore
    • Ext
      • Auth
        • Virtual
      • Debug
        • Tracy
      • Form
        • Core
        • Validators
      • Request
      • Router
        • Lang
      • View
        • Helpers
  • None

Classes

  • MvcCore
  • MvcCore\Config
  • MvcCore\Controller
  • MvcCore\Debug
  • MvcCore\Ext\Auth
  • MvcCore\Ext\Auth\Controller
  • MvcCore\Ext\Auth\SignInForm
  • MvcCore\Ext\Auth\SignOutForm
  • MvcCore\Ext\Auth\User
  • MvcCore\Ext\Auth\Virtual\Controller
  • MvcCore\Ext\Auth\Virtual\Form
  • MvcCore\Ext\Auth\Virtual\User
  • MvcCore\Ext\Debug\Tracy
  • MvcCore\Ext\Debug\Tracy\AuthPanel
  • MvcCore\Ext\Debug\Tracy\IncludePanel
  • MvcCore\Ext\Debug\Tracy\MvcCorePanel
  • MvcCore\Ext\Debug\Tracy\RoutingPanel
  • MvcCore\Ext\Debug\Tracy\SessionPanel
  • MvcCore\Ext\Form
  • MvcCore\Ext\Form\Button
  • MvcCore\Ext\Form\Checkbox
  • MvcCore\Ext\Form\CheckboxGroup
  • MvcCore\Ext\Form\Core\Base
  • MvcCore\Ext\Form\Core\Configuration
  • MvcCore\Ext\Form\Core\Field
  • MvcCore\Ext\Form\Core\FieldGroup
  • MvcCore\Ext\Form\Core\Helpers
  • MvcCore\Ext\Form\Core\Validator
  • MvcCore\Ext\Form\Core\View
  • MvcCore\Ext\Form\CountrySelect
  • MvcCore\Ext\Form\Date
  • MvcCore\Ext\Form\DateTime
  • MvcCore\Ext\Form\Email
  • MvcCore\Ext\Form\Hidden
  • MvcCore\Ext\Form\NoType
  • MvcCore\Ext\Form\Number
  • MvcCore\Ext\Form\Password
  • MvcCore\Ext\Form\RadioGroup
  • MvcCore\Ext\Form\Range
  • MvcCore\Ext\Form\ResetButton
  • MvcCore\Ext\Form\ResetInput
  • MvcCore\Ext\Form\Select
  • MvcCore\Ext\Form\SubmitButton
  • MvcCore\Ext\Form\SubmitInput
  • MvcCore\Ext\Form\Text
  • MvcCore\Ext\Form\Textarea
  • MvcCore\Ext\Form\Time
  • MvcCore\Ext\Form\Validators\CompanyId
  • MvcCore\Ext\Form\Validators\CompanyVatId
  • MvcCore\Ext\Form\Validators\Date
  • MvcCore\Ext\Form\Validators\Email
  • MvcCore\Ext\Form\Validators\FloatVal
  • MvcCore\Ext\Form\Validators\Integer
  • MvcCore\Ext\Form\Validators\Maxlength
  • MvcCore\Ext\Form\Validators\MaxSelectedOptions
  • MvcCore\Ext\Form\Validators\MinSelectedOptions
  • MvcCore\Ext\Form\Validators\NumberField
  • MvcCore\Ext\Form\Validators\Pattern
  • MvcCore\Ext\Form\Validators\Phone
  • MvcCore\Ext\Form\Validators\RangeField
  • MvcCore\Ext\Form\Validators\SafeString
  • MvcCore\Ext\Form\Validators\Time
  • MvcCore\Ext\Form\Validators\Url
  • MvcCore\Ext\Form\Validators\ValueInOptions
  • MvcCore\Ext\Form\Validators\ZipCode
  • MvcCore\Ext\Request\ApacheDpi
  • MvcCore\Ext\Request\Cli
  • MvcCore\Ext\Router\Lang
  • MvcCore\Ext\Router\Lang\Route
  • MvcCore\Ext\Router\Media
  • MvcCore\Ext\Router\MediaSiteKey
  • MvcCore\Ext\View\Helpers\Assets
  • MvcCore\Ext\View\Helpers\Css
  • MvcCore\Ext\View\Helpers\Js
  • MvcCore\Ext\View\Helpers\LineBreaks
  • MvcCore\Model
  • MvcCore\Request
  • MvcCore\Response
  • MvcCore\Route
  • MvcCore\Router
  • MvcCore\Session
  • MvcCore\Tool
  • MvcCore\View

Exceptions

  • MvcCore\Ext\Form\Core\Exception
  • Overview
  • Namespace
  • Class
  • Tree
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 
<?php

/**
 * MvcCore
 *
 * This source file is subject to the BSD 3 License
 * For the full copyright and license information, please view 
 * the LICENSE.md file that are distributed with this source code.
 *
 * @copyright   Copyright (c) 2016 Tom FlĂ­dr (https://github.com/mvccore/mvccore)
 * @license     https://mvccore.github.io/docs/mvccore/4.0.0/LICENCE.md
 */

namespace MvcCore\Ext\Form\Core;

require_once('Configuration.php');
require_once('Field.php');

abstract class Validator
{
    /** @var \MvcCore\Ext\Form|\MvcCore\Ext\Form\Core\Base */
    protected $Form = NULL;

    /** @var \MvcCore\Controller|mixed */
    protected $Controller = NULL;

    /** @var bool */
    protected $Translate = FALSE;
    
    /** @var callable */
    protected $Translator = NULL;

    /** @var string */
    protected static $validatorsClassNameTemplate = '\MvcCore\Ext\Form\Validators\{ValidatorName}';

    /** @var \MvcCore\Ext\Form\Core\Validator[]|mixed */
    protected static $instances = array();

    /**
     * Create new validator instance by validator class name end if necessary,
     * if validator instance for this name exists, previous instance is returned.
     * @param \MvcCore\Ext\Form $form submitting simple form instance
     * @param string $validatorName validator class name end
     * @throws Exception
     * @return \MvcCore\Ext\Form\Core\Validator[]|mixed
     */
    public static function Create (\MvcCore\Ext\Form\Core\Configuration & $form, $validatorName = '') {
        if (!isset(static::$instances[$validatorName])) {
            $localValidatorClassName = strpos($validatorName, '_') === FALSE && strpos($validatorName, '\\') === FALSE;
            if ($localValidatorClassName) {
                // if not any full class name - it's built in validator
                $className = str_replace('{ValidatorName}', $validatorName, static::$validatorsClassNameTemplate);
            } else {
                $className = $validatorName;
            }
            static::$instances[$validatorName] = new $className($form);
        }
        return static::$instances[$validatorName];
    }
    /**
     * Create new validator instance.
     * @param \MvcCore\Ext\Form $form 
     */
    public function __construct (\MvcCore\Ext\Form\Core\Configuration & $form) {
        $this->Form = & $form;
        $this->Controller = & $form->Controller;
        $this->Translate = $form->Translate;
        if ($this->Translate) $this->Translator = & $form->Translator;
    }
    /**
     * Validation template method.
     * In your validator implementation, check submitted value 
     * by validator specific rules and if there is any error, call
     * $form->AddError with translated or not translated error message.
     * Return safe submitted value as result.
     * @param string|array                  $submitValue 
     * @param string                        $fieldName 
     * @param \MvcCore\Ext\Form\Core\Field  $field
     * @return string|array                 safe submitted value
     */
    public function Validate ($submitValue, $fieldName, \MvcCore\Ext\Form\Core\Field & $field) {
        return $submitValue;
    }

    protected function addError (\MvcCore\Ext\Form\Core\Field & $field, $msg = '', callable $replaceCall = NULL) {
        $replacing = !is_null($replaceCall);
        $label = '';
        if ($replacing) $label = $field->Label ? $field->Label : $field->Name;
        if ($this->Translate) {
            $msg = call_user_func($this->Translator, $msg);
            if ($replacing) {
                $label = $field->Label ? call_user_func($this->Translator, $field->Label) : $field->Name;
            }
        }
        if ($replacing) {
            $msg = call_user_func($replaceCall, $msg, array($label));
        }
        $this->Form->AddError(
            $msg, $field->Name
        );
    }
}
MvcCore API documentation generated by ApiGen