mirror of
https://github.com/vlucas/valitron.git
synced 2025-12-30 23:01:52 +00:00
Merge pull request #22 from neves/static-lang
add global static lang support and sample to README.md
This commit is contained in:
commit
d3dfa81996
15
README.md
15
README.md
@ -67,6 +67,21 @@ if($v->validate()) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Setting language and language dir globally:
|
||||||
|
|
||||||
|
```php
|
||||||
|
|
||||||
|
// boot or config file
|
||||||
|
|
||||||
|
use Valitron\Validator as V;
|
||||||
|
|
||||||
|
V::langDir(__DIR__.'/validator_lang'); // always set langDir before lang.
|
||||||
|
V::lang('ar');
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Built-in Validation Rules
|
## Built-in Validation Rules
|
||||||
|
|
||||||
* `required` - Required field
|
* `required` - Required field
|
||||||
|
|||||||
@ -29,7 +29,7 @@ class Validator
|
|||||||
/**
|
/**
|
||||||
* Setup validation
|
* Setup validation
|
||||||
*/
|
*/
|
||||||
public function __construct($data, $fields = array(), $lang = 'en', $langDir = null)
|
public function __construct($data, $fields = array(), $lang = null, $langDir = null)
|
||||||
{
|
{
|
||||||
// Allows filtering of used input fields against optional second array of field names allowed
|
// Allows filtering of used input fields against optional second array of field names allowed
|
||||||
// This is useful for limiting raw $_POST or $_GET data to only known fields
|
// This is useful for limiting raw $_POST or $_GET data to only known fields
|
||||||
@ -39,17 +39,13 @@ class Validator
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only load language files if language or directory has changed
|
// set lang in the follow order: constructor param, static::$_lang, defaul to en
|
||||||
if($lang !== static::$_lang || $langDir !== static::$_langDir) {
|
$lang = $lang ?: static::$_lang ?: 'en';
|
||||||
// Set language directory for loading language files
|
// set langDir in the follow order: constructor param, static::$_langDir, default to package lang dir
|
||||||
if($langDir === null) {
|
$langDir = $langDir ?: static::$_langDir ?: dirname(dirname(__DIR__)) . '/lang';
|
||||||
$langDir = dirname(dirname(__DIR__)) . '/lang';
|
|
||||||
}
|
|
||||||
static::langDir($langDir);
|
|
||||||
|
|
||||||
// Set language for error messages
|
static::langDir($langDir);
|
||||||
static::lang($lang);
|
static::lang($lang);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
59
tests/Valitron/StaticLangTest.php
Normal file
59
tests/Valitron/StaticLangTest.php
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
<?php
|
||||||
|
use Valitron\Validator;
|
||||||
|
|
||||||
|
class StaticLangTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
public function tearDown()
|
||||||
|
{
|
||||||
|
$this->resetProperty('_lang');
|
||||||
|
$this->resetProperty('_langDir');
|
||||||
|
$this->resetProperty('_ruleMessages', array());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function resetProperty($name, $value = null)
|
||||||
|
{
|
||||||
|
$prop = new ReflectionProperty('Valitron\Validator', $name);
|
||||||
|
$prop->setAccessible(true);
|
||||||
|
$prop->setValue($value);
|
||||||
|
$prop->setAccessible(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getLangDir()
|
||||||
|
{
|
||||||
|
return __DIR__.'/../../lang';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lang defined statically should not be overrided by constructor default
|
||||||
|
*/
|
||||||
|
public function testLangDefinedStatically()
|
||||||
|
{
|
||||||
|
$lang = 'ar';
|
||||||
|
Validator::lang($lang);
|
||||||
|
$validator = new Validator(array());
|
||||||
|
$this->assertEquals($lang, Validator::lang());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* LangDir defined statically should not be overrided by constructor default
|
||||||
|
*/
|
||||||
|
public function testLangDirDefinedStatically()
|
||||||
|
{
|
||||||
|
$langDir = $this->getLangDir();
|
||||||
|
Validator::langDir($langDir);
|
||||||
|
$validator = new Validator(array());
|
||||||
|
$this->assertEquals($langDir, Validator::langDir());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDefaultLangShouldBeEn()
|
||||||
|
{
|
||||||
|
$validator = new Validator(array());
|
||||||
|
$this->assertEquals('en', Validator::lang());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDefaultLangDirShouldBePackageLangDir()
|
||||||
|
{
|
||||||
|
$validator = new Validator(array());
|
||||||
|
$this->assertEquals(realpath($this->getLangDir()), realpath(Validator::langDir()));
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user