Fabien POTENCIER wrote:
> I can also add a ->getErrors() method with an exception.
>
> Your comparison with PEAR::isError() makes me think this option is not
> the good one ;-) As I said, it feels like a hack (more PHP4 than PHP5).
>
> Fabien
>
> Kiril Angov wrote:
>
>> Fabien POTENCIER wrote:
>>
>>> Hi all,
>>>
>>> I'm working on the new validation system and I want your thoughts about
>>> a design decision I have to take.
>>>
>>> Let's take a string validator:
>>>
>>> $v = new sfValidatorString(array('max_length' => 2));
>>>
>>> Then let's try to validate a string:
>>>
>>> $ret = $v->validate('t');
>>>
>>> Here, $ret is 't'.
>>>
>>> Now, if I do:
>>>
>>> $ret = $v->validate('foo');
>>>
>>> What happens?
>>>
>>> 1 - The validator throws a sfValidatorException:
>>>
>>> throw new sfValidatorException('String is too long');
>>>
>>>
>>> 2 - The validator returns a sfValidatorError object:
>>>
>>> return new sfValidatorError('String is too long');
>>>
>>> 3 - The validator returns false or another "magic" value:
>>>
>>> return false;
>>>
>>> 3 is not very good because then I can't return false as a valid value
>>> (same goes for null or whatever other value).
>>>
>>> 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:
>>>
>>> throw new sfValidatorException(array('String is too long', 'foo is
>>> not a valid string'));
>>>
>>> Here is a typical usage:
>>>
>>> try
>>> {
>>> $ret = $v->validate('foo');
>>> }
>>> catch (sfValidatorException $e)
>>> {
>>> // do something with the error
>>> }
>>>
>>> The sfValidatorError usage looks good but the user code looks less "clean":
>>>
>>> $ret = $v->validate('foo');
>>> if ($ret instanceof sfValidatorError)
>>> {
>>> // do something with the error
>>> }
>>>
>>> It's less clean because if you forget to check the return value, you can
>>> have some weird behaviors in your code and debugging can then be much
>>> harder.
>>>
>>> Thanks for your ideas,
>>> Fabien
>>>
>>>
>>>
>>>
>> I like the third version. If you return an error object then there can
>> be a static method like the PEAR::isError() where you pass wth return to
>> chech if validation succeeded or not and then take the errors with
>> $e->getErrors() or something like this.
>>
>> Kupo
>>
>>
>>
>
> >
>
>
Yes, maybe you are right. I am thinking you can overcome the limitation
of the exception message being a text. Just serialize the array and then
in the getMessage() method just unserialize it. Should work, right?
Kupo
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---