mirror of
https://github.com/Respect/Validation.git
synced 2024-06-08 08:42:15 +02:00
More compositing messages adjustments
This commit is contained in:
parent
45b4a4dfad
commit
b13ef8f072
|
@ -69,17 +69,12 @@ class AbstractCompositeException extends ValidationException
|
|||
|
||||
public function addRelated(ValidationException $related)
|
||||
{
|
||||
if ($related instanceof static)
|
||||
$related->setName($this->getName());
|
||||
$this->related[] = $related;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setName($name)
|
||||
{
|
||||
foreach ($this->related as $r)
|
||||
if ($r instanceof static)
|
||||
$r->setName($name);
|
||||
return parent::setName($name);
|
||||
}
|
||||
|
||||
|
@ -92,20 +87,28 @@ class AbstractCompositeException extends ValidationException
|
|||
|
||||
public function getRelated($full=false)
|
||||
{
|
||||
if (!$full
|
||||
&& 1 === count($this->related)
|
||||
&& $this->related[0] instanceof self)
|
||||
if ($full || 1 !== count($this->related))
|
||||
return $this->related;
|
||||
elseif ($this->related[0] instanceof self)
|
||||
return $this->related[0]->getRelated();
|
||||
else
|
||||
return $this->related;
|
||||
return array();
|
||||
}
|
||||
|
||||
public function getMainMessage()
|
||||
public function getTemplate()
|
||||
{
|
||||
if (1 === count($this->related))
|
||||
return $this->related[0]->getMainMessage();
|
||||
return $this->related[0]->getTemplate();
|
||||
else
|
||||
return parent::getMainMessage();
|
||||
return parent::getTemplate();
|
||||
}
|
||||
|
||||
public function getParams()
|
||||
{
|
||||
if (1 === count($this->related))
|
||||
return $this->related[0]->getParams();
|
||||
else
|
||||
return parent::getParams();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -10,12 +10,29 @@ class AbstractRelatedException extends AbstractCompositeException
|
|||
return 0;
|
||||
}
|
||||
|
||||
public function getMainMessage()
|
||||
//TODO cleanup this inheritances
|
||||
public function getRelated($full=false)
|
||||
{
|
||||
$vars = $this->getParams();
|
||||
$vars['name'] = $this->getName();
|
||||
return static::format($this->getTemplate(), $vars);
|
||||
return $this->related;
|
||||
}
|
||||
|
||||
//TODO cleanup this inheritances
|
||||
public function getParams()
|
||||
{
|
||||
return $this->params;
|
||||
}
|
||||
|
||||
//TODO cleanup this inheritances
|
||||
public function getTemplate()
|
||||
{
|
||||
if (!empty($this->template))
|
||||
return $this->template;
|
||||
$templateKey = $this->chooseTemplate();
|
||||
if (is_null($this->context))
|
||||
$this->template = static::$defaultTemplates[$templateKey];
|
||||
else
|
||||
$this->template = $this->context->getTemplate($this, $templateKey);
|
||||
return $this->template;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,8 +7,8 @@ class AttributeException extends AbstractRelatedException
|
|||
const NOT_PRESENT = 0;
|
||||
const INVALID = 1;
|
||||
public static $defaultTemplates = array(
|
||||
self::NOT_PRESENT => 'Attribute {{reference}} must be present on {{name}}',
|
||||
self::INVALID => 'Attribute {{reference}} must be valid on {{name}}',
|
||||
self::NOT_PRESENT => 'Attribute {{name}} must be present',
|
||||
self::INVALID => 'Attribute {{name}} must be valid',
|
||||
);
|
||||
|
||||
public function chooseTemplate()
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class BetweenException extends AbstractCompositeException
|
||||
class BetweenException extends AbstractRelatedException
|
||||
{
|
||||
const BOTH = 0;
|
||||
const LOWER = 1;
|
||||
|
|
|
@ -2,8 +2,28 @@
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class LengthException extends BetweenException
|
||||
class LengthException extends ValidationException
|
||||
{
|
||||
const BOTH = 0;
|
||||
const LOWER = 1;
|
||||
const GREATER = 2;
|
||||
|
||||
public function configure($name, array $params=array())
|
||||
{
|
||||
$params['minValue'] = static::stringify($params['minValue']);
|
||||
$params['maxValue'] = static::stringify($params['maxValue']);
|
||||
return parent::configure($name, $params);
|
||||
}
|
||||
|
||||
public function chooseTemplate()
|
||||
{
|
||||
if (!$this->getParam('minValue'))
|
||||
return static::GREATER;
|
||||
elseif (!$this->getParam('maxValue'))
|
||||
return static::LOWER;
|
||||
else
|
||||
return static::BOTH;
|
||||
}
|
||||
|
||||
public static $defaultTemplates = array(
|
||||
self::BOTH => '{{name}} must have a length between {{minValue}} and {{maxValue}}',
|
||||
|
|
|
@ -19,6 +19,7 @@ abstract class AbstractRelated extends AbstractRule implements Validatable
|
|||
|
||||
public function __construct($reference, Validatable $validator=null, $mandatory=true)
|
||||
{
|
||||
$this->setName($reference);
|
||||
$this->reference = $reference;
|
||||
$this->validator = $validator;
|
||||
$this->mandatory = $mandatory;
|
||||
|
@ -36,7 +37,6 @@ abstract class AbstractRelated extends AbstractRule implements Validatable
|
|||
} catch (ValidationException $e) {
|
||||
throw $this
|
||||
->reportError($this->reference, array('hasReference' => true))
|
||||
->setName($this->reference)
|
||||
->addRelated($e);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,8 +10,8 @@
|
|||
<email>alexandre@gaigalas.net</email>
|
||||
<active>yes</active>
|
||||
</lead>
|
||||
<date>2011-02-18</date>
|
||||
<time>23:07:05</time>
|
||||
<date>2011-02-19</date>
|
||||
<time>17:47:25</time>
|
||||
<version>
|
||||
<release>0.1.0</release>
|
||||
<api>0.1.0</api>
|
||||
|
@ -28,15 +28,15 @@ First Version
|
|||
<dir baseinstalldir="Respect/Validation" name="/">
|
||||
<file baseinstalldir="Respect/Validation" md5sum="4ef5c6ff21a7382ac26d74354a9c2ece" name="Contexts/AbstractContext.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="a99e9fcc3f89d82a4b6dfd0fbe487014" name="Contexts/Form.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="19f9d9adaaa5de962387107aede4cb2b" name="Exceptions/AbstractCompositeException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="1e4976e871dccc11a4821436d2e5d610" name="Exceptions/AbstractRelatedException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="7e43dac02018fdfc40846622bcc253f9" name="Exceptions/AbstractCompositeException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="93478182b3fbebb69e23f48eff09f1f7" name="Exceptions/AbstractRelatedException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="3ca12be55a230b4a1096b680714b3222" name="Exceptions/AllOfException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="ae818fe09029e856ed02ce70273da0f5" name="Exceptions/AlnumException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="a4e7f3830a04d92bf48ae2e88bb8d488" name="Exceptions/AlphaException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="023aeb25a9aca6311c81e96aa4abf4f8" name="Exceptions/ArrException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="307d62a5000949c783dcec2c6b27b578" name="Exceptions/AtLeastException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="b43d0f2fabc4e16cbf801b2e08a8123e" name="Exceptions/AttributeException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="a2e69b7b2391149e2f0d06d7679b1e84" name="Exceptions/BetweenException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="8c0b761a132dc1e1182f8a4f87684218" name="Exceptions/AttributeException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="2593e98d0240ef3ca91dfb7099f5d71b" name="Exceptions/BetweenException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="b6c595b7fcb32a829bf04377fb2baf99" name="Exceptions/CallbackException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="f63902e4f4c6a626bb3dc65997db33d3" name="Exceptions/CallException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="b22f51d15eb7d8fffceadcaf237948f0" name="Exceptions/ComponentException.php" role="php" />
|
||||
|
@ -51,7 +51,7 @@ First Version
|
|||
<file baseinstalldir="Respect/Validation" md5sum="0a858e482f0a5b88a927be1d35af826e" name="Exceptions/IntException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="fd73d54dc27f09db248f15241131a574" name="Exceptions/IpException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="c8f156b64c435a0b0be2858e644c0ce9" name="Exceptions/KeyException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="fd217a043271989aa9fdd1d14ee11887" name="Exceptions/LengthException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="648e10154cafca88993c366e9dcb0ef5" name="Exceptions/LengthException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="2b9d32dfeb050be51824bb783fe6b38b" name="Exceptions/MaxException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="aa9cba4e18c3084fc118cad99bd5b143" name="Exceptions/MinException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="5f7e12383771ce84388845282ac648d8" name="Exceptions/MostOfException.php" role="php" />
|
||||
|
@ -70,7 +70,7 @@ First Version
|
|||
<file baseinstalldir="Respect/Validation" md5sum="87d7f2f8ff53aa98b0a06f4623994033" name="Exceptions/ValidationException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="250fe508ea6c6fa437d8cd37ac1dc697" name="Exceptions/ZendException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="f2dcb5631134935d3d981313cf296e54" name="Rules/AbstractComposite.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="478b4e96e27bceb9409c3e2bd2a66ca6" name="Rules/AbstractRelated.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="7f6273711c504bc72f2aad6dc499d15c" name="Rules/AbstractRelated.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="46719f6b47886cf026271f0ab6736453" name="Rules/AbstractRule.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="38749eea751bc65585b121cdf2452b9c" name="Rules/AllOf.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="7b002c1fab94b46f85c5cb79c50a3c2d" name="Rules/Alnum.php" role="php" />
|
||||
|
@ -135,7 +135,7 @@ First Version
|
|||
<release>alpha</release>
|
||||
<api>alpha</api>
|
||||
</stability>
|
||||
<date>2011-02-18</date>
|
||||
<date>2011-02-19</date>
|
||||
<license uri="http://www.opensource.org/licenses/bsd-license.php">BSD Style</license>
|
||||
<notes>
|
||||
First Version
|
||||
|
|
|
@ -186,7 +186,7 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
public function testGmailSignInValidation()
|
||||
{
|
||||
$stringMax256 = v::string()->length(1, 256);
|
||||
$stringMax256 = v::string()->length(5, 256);
|
||||
$alnumDot = v::alnum('.');
|
||||
$stringMin8 = v::string()->length(8, null);
|
||||
$v = v::allOf(
|
||||
|
@ -198,11 +198,11 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
v::attribute('stay_signedin', v::notEmpty())->setName('Stay signed in'),
|
||||
v::attribute('enable_webhistory', v::notEmpty())->setName('Enabled Web History'),
|
||||
v::attribute('security_question', $stringMax256)->setName('Security Question')
|
||||
);
|
||||
)->setName('Validation Form');
|
||||
try {
|
||||
$v->assert(
|
||||
(object) array(
|
||||
'first_name' => null,
|
||||
'first_name' => 'fiif',
|
||||
'last_name' => null,
|
||||
'desired_login' => null,
|
||||
'password' => null,
|
||||
|
@ -213,7 +213,7 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
)
|
||||
);
|
||||
} catch (Exceptions\ValidationException $e) {
|
||||
echo $e->getFullMessage().PHP_EOL;
|
||||
//echo $e->getFullMessage().PHP_EOL;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -262,7 +262,7 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
try {
|
||||
$validBlogPost->assert($blogPost);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
echo $e->getFullMessage().PHP_EOL;
|
||||
//echo $e->getFullMessage().PHP_EOL;
|
||||
//echo $e->findRelated('author')->getMainMessage();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue