forked from fuel/validation
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
184 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
/** | ||
* @package Fuel\Validation | ||
* @version 2.0 | ||
* @author Fuel Development Team | ||
* @license MIT License | ||
* @copyright 2010 - 2013 Fuel Development Team | ||
* @link http://fuelphp.com | ||
*/ | ||
|
||
namespace Fuel\Validation\Rule; | ||
|
||
/** | ||
* Checks that the given field exists if the conditional field is passed and is not empty | ||
* | ||
* @package Fuel\Validation\Rule | ||
* @author Fuel Development Team | ||
* | ||
* @since 2.0 | ||
*/ | ||
class RequiredIf extends Required | ||
{ | ||
|
||
/** | ||
* @param mixed $value | ||
* @param null $field | ||
* @param null $allFields | ||
* | ||
* @return bool | ||
* | ||
* @since 2.0 | ||
*/ | ||
public function validate($value, $field = null, $allFields = null) | ||
{ | ||
$requiredField = $this->getParameter(); | ||
|
||
if ($allFields !== null and array_key_exists($requiredField, $allFields) and ! empty($allFields[$requiredField])) | ||
{ | ||
return parent::validate($value, $field, $allFields); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
/** | ||
* @package Fuel\Validation | ||
* @version 2.0 | ||
* @author Fuel Development Team | ||
* @license MIT License | ||
* @copyright 2010 - 2013 Fuel Development Team | ||
* @link http://fuelphp.com | ||
*/ | ||
|
||
namespace Fuel\Validation\Rule; | ||
|
||
class AbstractRequiredTest extends AbstractRuleTest | ||
{ | ||
|
||
/** | ||
* {@inheritdocs} | ||
*/ | ||
protected $message = 'The field is required and has not been specified.'; | ||
|
||
protected function _before() | ||
{ | ||
$this->object = new Required; | ||
} | ||
|
||
/** | ||
* @dataProvider validateProvider | ||
*/ | ||
public function testValidate() | ||
{ | ||
list($value, $field, $expected, $data) = func_get_args(); | ||
|
||
$this->assertEquals( | ||
$expected, | ||
$this->object->validate($value, $field, $data) | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdocs} | ||
*/ | ||
public function validateProvider() | ||
{ | ||
return array( | ||
0 => array('[email protected]', null, false, null), | ||
1 => array('', null, false, null), | ||
2 => array(array(), null, false, null), | ||
3 => array(null, null, false, null), | ||
4 => array(false, null, false, null), | ||
|
||
5 => array('test string 5', 'test', false, | ||
array() | ||
), | ||
|
||
6 => array('test string 6', 'test', false, | ||
array( | ||
'foo' => 'bar', | ||
'baz' => 'bat', | ||
) | ||
), | ||
|
||
7 => array('test string 7', 'test', true, | ||
array( | ||
'foo' => 'bar', | ||
'test' => 'value', | ||
'baz' => 'bat', | ||
) | ||
), | ||
|
||
8 => array('bla', 'test', true, null), | ||
9 => array('', 'test', false, null), | ||
10 => array('bla', null, false, array()), | ||
11 => array('', null, false, array()), | ||
|
||
12 => array(false, 'foo', true, | ||
array( | ||
'foo' => false, | ||
) | ||
), | ||
13 => array(true, 'foo', true, | ||
array( | ||
'foo' => true, | ||
) | ||
), | ||
|
||
14 => array(false, 'bar', true, | ||
array( | ||
'bar' => false, | ||
) | ||
), | ||
15 => array(0, 'test', true, | ||
array( | ||
'foo' => 'bar', | ||
'test' => 0, | ||
'baz' => 'bat', | ||
) | ||
), | ||
16 => array(0, 'test', true, array('test' => 0)), | ||
); | ||
} | ||
|
||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
/** | ||
* @package Fuel\Validation | ||
* @version 2.0 | ||
* @author Fuel Development Team | ||
* @license MIT License | ||
* @copyright 2010 - 2013 Fuel Development Team | ||
* @link http://fuelphp.com | ||
*/ | ||
|
||
namespace Fuel\Validation\Rule; | ||
|
||
class RequiredIfTest extends AbstractRequiredTest | ||
{ | ||
|
||
protected function _before() | ||
{ | ||
$this->object = new RequiredIf('field'); | ||
} | ||
|
||
/** | ||
* {@inheritdocs} | ||
*/ | ||
public function validateProvider() | ||
{ | ||
return array( | ||
array('value', null, true, array('field' => 'value')), | ||
array('', null, true, null), | ||
array('value', null, true, array('field' => 'othervalue')), | ||
array('value', null, false, array('field' => '')), | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,93 +10,7 @@ | |
|
||
namespace Fuel\Validation\Rule; | ||
|
||
class RequiredTest extends AbstractRuleTest | ||
class RequiredTest extends AbstractRequiredTest | ||
{ | ||
|
||
/** | ||
* {@inheritdocs} | ||
*/ | ||
protected $message = 'The field is required and has not been specified.'; | ||
|
||
protected function _before() | ||
{ | ||
$this->object = new Required; | ||
} | ||
|
||
/** | ||
* @dataProvider validateProvider | ||
*/ | ||
public function testValidate() | ||
{ | ||
list($value, $field, $expected, $data) = func_get_args(); | ||
|
||
$this->assertEquals( | ||
$expected, | ||
$this->object->validate($value, $field, $data) | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdocs} | ||
*/ | ||
public function validateProvider() | ||
{ | ||
return array( | ||
0 => array('[email protected]', null, false, null), | ||
1 => array('', null, false, null), | ||
2 => array(array(), null, false, null), | ||
3 => array(null, null, false, null), | ||
4 => array(false, null, false, null), | ||
|
||
5 => array('test string 5', 'test', false, | ||
array() | ||
), | ||
|
||
6 => array('test string 6', 'test', false, | ||
array( | ||
'foo' => 'bar', | ||
'baz' => 'bat', | ||
) | ||
), | ||
|
||
7 => array('test string 7', 'test', true, | ||
array( | ||
'foo' => 'bar', | ||
'test' => 'value', | ||
'baz' => 'bat', | ||
) | ||
), | ||
|
||
8 => array('bla', 'test', true, null), | ||
9 => array('', 'test', false, null), | ||
10 => array('bla', null, false, array()), | ||
11 => array('', null, false, array()), | ||
|
||
12 => array(false, 'foo', true, | ||
array( | ||
'foo' => false, | ||
) | ||
), | ||
13 => array(true, 'foo', true, | ||
array( | ||
'foo' => true, | ||
) | ||
), | ||
|
||
14 => array(false, 'bar', true, | ||
array( | ||
'bar' => false, | ||
) | ||
), | ||
15 => array(0, 'test', true, | ||
array( | ||
'foo' => 'bar', | ||
'test' => 0, | ||
'baz' => 'bat', | ||
) | ||
), | ||
16 => array(0, 'test', true, array('test' => 0)), | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters