diff --git a/README.md b/README.md index d8d0223..1eb8745 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,19 @@ if($v->validate()) { } ``` +Or use dot syntax to validate all members of a numeric array: + +```php +$v = new Valitron\Validator(array('values' => array(50, 90))); +$v->rule('max', 'values.*', 100); +if($v->validate()) { + echo "Yay! We're all good!"; +} else { + // Errors + print_r($v->errors()); +} +``` + Setting language and language dir globally: ```php @@ -121,6 +134,7 @@ V::lang('ar'); * `notIn` - Negation of `in` rule (not in array of values) * `ip` - Valid IP address * `email` - Valid email address + * `emailDNS` - Valid email address with active DNS record * `url` - Valid URL * `urlActive` - Valid URL with active DNS record * `alpha` - Alphabetic characters only diff --git a/composer.json b/composer.json index e3faef9..066dda7 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "description": "Simple, elegant, stand-alone validation library with NO dependencies", "keywords": ["validation", "validator", "valid"], "homepage": "http://github.com/vlucas/valitron", - "license" : "BSD", + "license" : "BSD-3-Clause", "authors" : [ { "name": "Vance Lucas", @@ -16,7 +16,7 @@ "php": ">=5.3.2" }, "require-dev": { - "phpunit/phpunit": "~4.0" + "phpunit/phpunit": "~4.8.35" }, "autoload": { "psr-0": { diff --git a/lang/lt.php b/lang/lt.php new file mode 100644 index 0000000..cace602 --- /dev/null +++ b/lang/lt.php @@ -0,0 +1,33 @@ + "yra privalomas", + 'equals' => "reikšmė turi būti tokia pati, kaip „%s“", + 'different' => "turi būti kitokia, nei „%s“", + 'accepted' => "turi būti priimta", + 'numeric' => "turi būti sveikasis skaičius", + 'integer' => "turi būti skaičius", + 'length' => "turi būti %d ženklų ilgio", + 'min' => "turi būti bent %s", + 'max' => "turi būti ne daugiau kaip %s", + 'in' => "turi neteisingą vertę", + 'notIn' => "turi neteisingą vertę", + 'ip' => "nėra teisingas IP adresas", + 'email' => "nėra teisingas el. pašto adresas", + 'url' => "nėra teisingas URL", + 'urlActive' => "turi būti aktyvus domenas", + 'alpha' => "turi turėti tik raides a-ž", + 'alphaNum' => "turi turėti tik raides a-ž ir/ar skaičius 0-9", + 'slug' => "turi turėti tik raides a-ž, skaičius 0-9, brukšnelius ir apatinius brukšnelius", + 'regex' => "yra neteisingų ženklų", + 'date' => "nėra teisinga data", + 'dateFormat' => "turi būti „%s“ formato data", + 'dateBefore' => "turi būti data, ankstesnė nei „%s“", + 'dateAfter' => "turi būti data, vėlesnė nei „%s“", + 'contains' => "turi turėti %s", + 'boolean' => "turi būti būlio (taip/ne) tipo", + 'lengthBetween' => "turi būti nuo %d iki %d ilgio", + 'creditCard' => "turi būti teisingas kreditinės kortelės numeris", + 'lengthMin' => "turi būti bent %d ženklų ilgio", + 'lengthMax' => "turi būti ne ilgesnis nei %d ženklų", + 'instanceOf' => "turi būti „%s“ atvejis" +); diff --git a/lang/sv.php b/lang/sv.php new file mode 100644 index 0000000..94884cf --- /dev/null +++ b/lang/sv.php @@ -0,0 +1,34 @@ + "är obligatorisk", + 'equals' => "måste vara samma som '%s'", + 'different' => "får inte vara samma som '%s'", + 'accepted' => "måste accepteras", + 'numeric' => "måste vara numerisk", + 'integer' => "måste vara ett heltal", + 'length' => "måste vara %d tecken långt", + 'min' => "måste vara minst %s", + 'max' => "får inte vara mer än %s", + 'in' => "innehåller ogiltigt värde", + 'notIn' => "innehåller ogiltigt värde", + 'ip' => "är inte en giltlig IP-adress", + 'email' => "är inte en giltlig e-postadress", + 'url' => "är inte en giltlig URL", + 'urlActive' => "måste vara ett aktivt domännamn", + 'alpha' => "får bara inehålla bokstäver a-z", + 'alphaNum' => "får bara inehålla bokstäver a-z och/eller siffror 0-9", + 'slug' => "får bara inehålla bokstäver a-z och/eller siffror 0-9, bindesträck og understräck", + 'regex' => "inehåller ogiltliga tecken", + 'date' => "är inte ett giltligt datum", + 'dateFormat' => "måste vara ett datum med formatet '%s'", + 'dateBefore' => "måste vara ett datum före '%s'", + 'dateAfter' => "måste vara ett datum efter '%s'", + 'contains' => "måste innehålla %s", + 'boolean' => "måste vara ett booleskt värde", + 'lengthBetween' => "måste vara mellan %d och %d tecken långt", + 'creditCard' => "måste vara et gyldig kredittkortnummer", + 'lengthMin' => "måste vara minst %d tecken långt", + 'lengthMax' => "får itne överstiga %d tecken", + 'instanceOf' => "måste vara en instans av '%s'" +); diff --git a/lang/zh-cn.php b/lang/zh-cn.php index d154815..97835b4 100644 --- a/lang/zh-cn.php +++ b/lang/zh-cn.php @@ -24,5 +24,10 @@ return array( 'dateFormat' => "日期的格式应该为 '%s'", 'dateBefore' => "日期必须在 '%s' 之前", 'dateAfter' => "日期必须在 '%s' 之后", - 'contains' => "必须包含 %s" + 'contains' => "必须包含 %s", + 'boolean' => "必须是真或假", + 'lengthBetween' => "长度只能介于 %d 和 %d 之间", + 'creditCard' => "信用卡号码不正确", + 'lengthMin' => "长度必须大于 %d", + 'lengthMax' => "长度必须小于 %d" ); diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index b1fb195..c377b2f 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -172,9 +172,9 @@ class Validator */ protected function validateEquals($field, $value, array $params) { - $field2 = $params[0]; - - return isset($this->_fields[$field2]) && $value == $this->_fields[$field2]; + // extract the second field value, this accounts for nested array values + list($field2Value, $multiple) = $this->getPart($this->_fields, explode('.', $params[0])); + return isset($field2Value) && $value == $field2Value; } /** @@ -187,9 +187,9 @@ class Validator */ protected function validateDifferent($field, $value, array $params) { - $field2 = $params[0]; - - return isset($this->_fields[$field2]) && $value != $this->_fields[$field2]; + // extract the second field value, this accounts for nested array values + list($field2Value, $multiple) = $this->getPart($this->_fields, explode('.', $params[0])); + return isset($field2Value) && $value != $field2Value; } /** @@ -529,6 +529,26 @@ class Validator return filter_var($value, \FILTER_VALIDATE_EMAIL) !== false; } + /** + * Validate that a field is a valid e-mail address and the domain name is active + * + * @param string $field + * @param mixed $value + * @return bool + */ + protected function validateEmailDNS($field, $value) + { + if ($this->validateEmail($field, $value)) { + $domain = ltrim(stristr($value, '@'), '@') . '.'; + if (function_exists('idn_to_ascii') && defined('INTL_IDNA_VARIANT_UTS46')) { + $domain = idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46); + } + return checkdnsrr($domain, 'ANY'); + } + + return false; + } + /** * Validate that a field is a valid URL by syntax * @@ -1120,7 +1140,7 @@ class Validator } /** - * Returns true if either a valdiator with the given name has been + * Returns true if either a validator with the given name has been * registered or there is a default validator by that name. * * @param string $name @@ -1188,6 +1208,8 @@ class Validator } /** + * Add label to rule + * * @param string $value * @return Validator */ @@ -1200,6 +1222,8 @@ class Validator } /** + * Add labels to rules + * * @param array $labels * @return Validator */ diff --git a/tests/Valitron/BaseTestCase.php b/tests/Valitron/BaseTestCase.php index fec8efc..ccfc736 100644 --- a/tests/Valitron/BaseTestCase.php +++ b/tests/Valitron/BaseTestCase.php @@ -1,6 +1,8 @@ rule('required', array('name', 'email')); $this->assertFalse($v->validate()); } - + public function testRequiredSubfieldsArrayStringValue() { $v = new Validator(array('name' => 'bob')); @@ -70,6 +70,36 @@ class ValidateTest extends BaseTestCase $this->assertFalse($v->validate()); } + public function testEqualsBothNull() + { + $v = new Validator(array('foo' => null, 'bar' => null)); + $v->rule('equals', 'foo', 'bar'); + $this->assertTrue($v->validate()); + } + + public function testEqualsBothNullRequired() + { + $v = new Validator(array('foo' => null, 'bar' => null)); + $v->rule('required', array('foo', 'bar')); + $v->rule('equals', 'foo', 'bar'); + $this->assertFalse($v->validate()); + } + + public function testEqualsBothUnset() + { + $v = new Validator(array('foo' => 1)); + $v->rule('equals', 'bar', 'baz'); + $this->assertTrue($v->validate()); + } + + public function testEqualsBothUnsetRequired() + { + $v = new Validator(array('foo' => 1)); + $v->rule('required', array('bar', 'baz')); + $v->rule('equals', 'bar', 'baz'); + $this->assertFalse($v->validate()); + } + public function testDifferentValid() { $v = new Validator(array('foo' => 'bar', 'bar' => 'baz')); @@ -84,6 +114,36 @@ class ValidateTest extends BaseTestCase $this->assertFalse($v->validate()); } + public function testDifferentBothNull() + { + $v = new Validator(array('foo' => null, 'bar' => null)); + $v->rule('equals', 'foo', 'bar'); + $this->assertTrue($v->validate()); + } + + public function testDifferentBothNullRequired() + { + $v = new Validator(array('foo' => null, 'bar' => null)); + $v->rule('required', array('foo', 'bar')); + $v->rule('equals', 'foo', 'bar'); + $this->assertFalse($v->validate()); + } + + public function testDifferentBothUnset() + { + $v = new Validator(array('foo' => 1)); + $v->rule('equals', 'bar', 'baz'); + $this->assertTrue($v->validate()); + } + + public function testDifferentBothUnsetRequired() + { + $v = new Validator(array('foo' => 1)); + $v->rule('required', array('bar', 'baz')); + $v->rule('equals', 'bar', 'baz'); + $this->assertFalse($v->validate()); + } + public function testAcceptedValid() { $v = new Validator(array('agree' => 'yes')); @@ -560,6 +620,18 @@ class ValidateTest extends BaseTestCase $this->assertFalse($v->validate()); } + public function testEmailDnsValid(){ + $v = new Validator(array('name' => 'Chester Tester', 'email' => 'chester@tester.com')); + $v->rule('emailDNS', 'email'); + $this->assertTrue($v->validate()); + } + + public function testEmailDnsInvalid(){ + $v = new Validator(array('name' => 'Chester Tester', 'email' => 'chester@tester.zyx')); + $v->rule('emailDNS', 'email'); + $this->assertFalse($v->validate()); + } + public function testUrlValid() { $v = new Validator(array('website' => 'http://google.com')); @@ -629,7 +701,7 @@ class ValidateTest extends BaseTestCase $v->rule('slug', 'test'); $this->assertFalse($v->validate()); } - + public function testNoErrorFailOnArray() { $v = new Validator(array('test' => array())); @@ -1287,22 +1359,22 @@ class ValidateTest extends BaseTestCase public function testOptionalProvidedValid() { - $v = new Validator(array('address' => 'user@example.com')); - $v->rule('optional', 'address')->rule('email', 'address'); + $v = new Validator(array('address' => 'user@example.com')); + $v->rule('optional', 'address')->rule('email', 'address'); $this->assertTrue($v->validate()); } public function testOptionalProvidedInvalid() { - $v = new Validator(array('address' => 'userexample.com')); - $v->rule('optional', 'address')->rule('email', 'address'); + $v = new Validator(array('address' => 'userexample.com')); + $v->rule('optional', 'address')->rule('email', 'address'); $this->assertFalse($v->validate()); } public function testOptionalNotProvided() { - $v = new Validator(array()); - $v->rule('optional', 'address')->rule('email', 'address'); + $v = new Validator(array()); + $v->rule('optional', 'address')->rule('email', 'address'); $this->assertTrue($v->validate()); } @@ -1353,9 +1425,98 @@ class ValidateTest extends BaseTestCase $this->assertFalse($v1->validate()); $v2 = new Validator($data); - $v2->rule('required', array('empty_text', 'null_value', 'in_array.empty_text')); - $this->assertFalse($v2->validate()); + $v2->rule('required', array('empty_text', 'null_value', 'in_array.empty_text'), true); + $this->assertTrue($v2->validate()); } + + public function testNestedEqualsValid() + { + $v = new Validator(array('foo' => array('one' => 'bar', 'two' => 'bar'))); + $v->rule('equals', 'foo.one', 'foo.two'); + $this->assertTrue($v->validate()); + } + + public function testNestedEqualsInvalid() + { + $v = new Validator(array('foo' => array('one' => 'bar', 'two' => 'baz'))); + $v->rule('equals', 'foo.one', 'foo.two'); + $this->assertFalse($v->validate()); + } + + public function testNestedEqualsBothNull() + { + $v = new Validator(array('foo' => array('bar' => null, 'baz' => null))); + $v->rule('equals', 'foo.bar', 'foo.baz'); + $this->assertTrue($v->validate()); + } + + public function testNestedEqualsBothNullRequired() + { + $v = new Validator(array('foo' => array('bar' => null, 'baz' => null))); + $v->rule('required', array('foo.bar', 'foo.baz')); + $v->rule('equals', 'foo.bar', 'foo.baz'); + $this->assertFalse($v->validate()); + } + + public function testNestedEqualsBothUnset() + { + $v = new Validator(array('foo' => 'bar')); + $v->rule('equals', 'foo.one', 'foo.two'); + $this->assertTrue($v->validate()); + } + + public function testNestedEqualsBothUnsetRequired() + { + $v = new Validator(array('foo' => 'bar')); + $v->rule('required', array('foo.one', 'foo.two')); + $v->rule('equals', 'foo.one', 'foo.two'); + $this->assertFalse($v->validate()); + } + + public function testNestedDifferentValid() + { + $v = new Validator(array('foo' => array('one' => 'bar', 'two' => 'baz'))); + $v->rule('different', 'foo.one', 'foo.two'); + $this->assertTrue($v->validate()); + } + + public function testNestedDifferentInvalid() + { + $v = new Validator(array('foo' => array('one' => 'baz', 'two' => 'baz'))); + $v->rule('different', 'foo.one', 'foo.two'); + $this->assertFalse($v->validate()); + } + + public function testNestedDifferentBothNull() + { + $v = new Validator(array('foo' => array('bar' => null, 'baz' => null))); + $v->rule('different', 'foo.bar', 'foo.baz'); + $this->assertTrue($v->validate()); + } + + public function testNestedDifferentBothNullRequired() + { + $v = new Validator(array('foo' => array('bar' => null, 'baz' => null))); + $v->rule('required', array('foo.bar', 'foo.baz')); + $v->rule('different', 'foo.bar', 'foo.baz'); + $this->assertFalse($v->validate()); + } + + public function testNestedDifferentBothUnset() + { + $v = new Validator(array('foo' => 'bar')); + $v->rule('different', 'foo.bar', 'foo.baz'); + $this->assertTrue($v->validate()); + } + + public function testNestedDifferentBothUnsetRequired() + { + $v = new Validator(array('foo' => 'bar')); + $v->rule('required', array('foo.bar', 'foo.baz')); + $v->rule('different', 'foo.bar', 'foo.baz'); + $this->assertFalse($v->validate()); + } + } function sampleFunctionCallback($field, $value, array $params)