2007/9/30, Fabien POTENCIER <[EMAIL PROTECTED]>:
> My preference goes to the exception but PHP exceptions are quite
> inflexible, the message has to be a string, so if I want to return
> several error messages, I can't
To me, an exception is related to a unique error, but I may have
missing the point. So because what you want to do here is "catch"
multiple errors, I guess adding a custom method tho the exception
object is a solution... Here an example code (quick and dirty):
<?php
class sfValidatorException extends Exception
{
protected $messages = array();
public function getMessages()
{
return $this->messages;
}
public function setMessages(array $messages)
{
$this->messages = $messages;
}
}
class sfValidatorString
{
public function validate($value)
{
$errors = array();
// Something fails here
$errors[] = 'A first problem has occured';
// Something fails here again
$errors[] = 'A second problem has occured';
// Throws exception
$count_errors = count($errors);
if ($count_errors > 0)
{
$exception = new sfValidatorException(sprintf('%d string
Error(s) encountered', $count_errors));
$exception->setMessages($errors);
throw $exception;
}
return $value;
}
}
try
{
$v = new sfValidatorString();
$ret = $v->validate('foo');
}
catch (sfValidatorException $e)
{
echo $e->getMessage().":\n";
foreach ($e->getMessages() as $message)
{
echo " - ".$message."\n";
}
}
Of course you can add error codes via setErrorCodes() and so on...
++
--
Nicolas Perriault http://www.clever-age.com
Clever Age - conseil en architecture technique
GSM: +33 6 60 92 08 67 Tél: +33 1 53 34 66 10
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"symfony developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/symfony-devs?hl=en
-~----------~----~----~----~------~----~------~--~---