public function __construct($params) { date_default_timezone_set('UTC'); ValitronValidator::lang('ru'); ValitronValidator::addRule('stringWords', function ($field, $value, array $params) { return count(explode(' ', $value)) <= $params[0]; }, 'должно содержать не более слов: %s'); self::$validator = new ValitronValidator($params); }
/** * Form constructor. * @param string $formId Form identificator * @param array $post $_POST data array * @param \DigitalHammer\LpForms\Mailer $mailer Mailer instance * @param string $lang language for validator */ public function __construct($formId, array $post, $mailer, $lang = 'ru') { $this->formId = $formId; $this->post = $post; $this->mailer = $mailer; Validator::lang($lang); $this->validator = new Validator($this->post); $this->response = new ResponseJson(); $this->setMessageBodyTemplate($formId); }
public function __construct($data, $lang = 'ja') { V::lang($lang); if ($data instanceof Params) { $data = $data->toArray(); } elseif (!is_array($data)) { throw new \RuntimeException('$data should be array or Params'); } $this->v = new V($data); $this->labels(); $this->rules(); }
public function register() { $di = $this->getContainer(); $config = $this->getConfig(); if (!is_null($config['locales_dir'])) { Validator::langDir($config['locales_dir']); } Validator::lang($config['locale']); foreach ($config['rules'] as $rule) { call_user_func_array('Valitron\\Validator::addRule', $rule); } $di->add('Valitron\\Validator', function ($data, $fields = [], $lang = null, $langDir = null) { return new Validator($data, $fields, $lang, $langDir); }); }
} else { // Global configuration $siteName = get_config('site_name'); $Institution = get_config('institution'); $InstitutionUrl = get_config('institution_url'); $urlServer = get_config('base_url'); $session = new Session(); $uid = $session->user_id; $language = $session->language; } //Initializing Valitron (form validation library) require_once 'include/Valitron/Validator.php'; use Valitron\Validator as V; V::langDir(__DIR__ . '/Valitron/lang'); // always set langDir before lang. V::lang($language); // Managing Session Flash Data if (isset($_SESSION['flash_old'])) { foreach ($_SESSION['flash_old'] as $row) { unset($_SESSION[$row]); } unset($_SESSION['flash_old']); } if (isset($_SESSION['flash_new'])) { $_SESSION['flash_old'] = $_SESSION['flash_new']; unset($_SESSION['flash_new']); } if (!isset($session)) { $session = new Session(); } $uid = $session->user_id;
public function testDefaultLangShouldBeEn() { $validator = new Validator(array()); $this->assertEquals('en', Validator::lang()); }
<?php require 'vendor/autoload.php'; //dompdf config define('DOMPDF_ENABLE_AUTOLOAD', false); //validator config use Valitron\Validator as V; V::langDir('vendor/vlucas/valitron/lang'); V::lang('en'); $f3 = Base::instance(); $f3->config('config/config.ini'); $f3->config('config/routes.ini'); $f3->set('ONERROR',function($f3){ echo Template::instance()->render('404.html'); }); $f3->run();
public function testInstanceOverrideStaticLang() { Validator::lang('ar'); new Validator(array(), array(), 'en'); $this->assertEquals('ar', Validator::lang(), 'instance defined lang should not replace static global lang'); }
require_once './vendor/autoload.php'; $config = (require_once './config/config.php'); use Doctrine\ORM\Tools\Setup; use Doctrine\ORM\EntityManager; use Doctrine\ORM\Mapping\Driver\SimplifiedYamlDriver; use Slim\Slim; use Slim\Extras\Views\Twig; use Valitron\Validator as V; // ----------------> YAML $namespaces = $config['yaml']['namespaces']; $driver = new SimplifiedYamlDriver($namespaces); $driver->setGlobalBasename('global'); // global.orm.yml // ----------------> Doctrine $isDevMode = true; $dbParams = $config['doctrine']; // ----------------> YAML $yml_config = Setup::createYAMLMetadataConfiguration(array(__DIR__ . "/config/doctrine"), $isDevMode); $entityManager = EntityManager::create($dbParams, $yml_config); $em = $entityManager; // ---------------> Slim $view = new Twig(); $view->twigOptions = $config['slim_twig']; $view->twigExtensions = $config['slim_ext']; $app = new Slim(array('view' => $view)); $app->config($config['slim']); // ---------------> Valitron V::langDir(__DIR__ . '/config/lang/'); V::lang('nl');
/** * Проверяет REQUEST * * @param array $rules правила проверки * https://github.com/vlucas/valitron#built-in-validation-rules * например: [ * ['required', []], * ['integer', ['category', 'page', 'sort', 'isFree', 'countAnswersOption']], * ['array', ['answersCounter', 'priceAction']], * ['lengthMax', ['text'], 10], * ] * * @return void * Если возникают ошибки при валидации, то они выводятся через self::jsonError с кодом 10 * и происходит завершение приложения через exit(); */ public static function validateRequestJson($rules) { \Valitron\Validator::lang('ru'); $validator = new \Valitron\Validator($_REQUEST); foreach ($rules as $rule) { if (count($rule) == 2) { $validator->rule($rule[0], $rule[1]); } if (count($rule) == 3) { $validator->rule($rule[0], $rule[1], $rule[2]); } } if ($validator->validate()) { return; } else { $errors = $validator->errors(); echo self::jsonErrorId(10, $errors); exit; } }