Hi Greg,

I think the Valid constraint is for a different purpose. See:
http://symfony.com/doc/2.0/reference/constraints/Valid.html.

I think you're right about a Comparison validation being common. One
way it could work is with a series of CLASS_CONSTRAINT constraints
(GreatherThan, LessThan, LessThanOrEqual, etc). So for the moment it
would require a custom constraint (http://symfony.com/doc/2.0/cookbook/
validation/custom_constraint.html). A rough outline of the solution (I
haven't tested this so might have some errors):

-----------------------------------------------------
namespace MyProject\Validator\Constraint;
use Symfony\Component\Validator\Constraint;

class LessThan extends Constraint {
    public $message = 'First value is not less than second value';
    public $firstProperty;
    public $secondProperty;

    public function getRequiredOptions() {
        return array('firstProperty', 'secondProperty');
    }
    public function getTargets(){
        return self::CLASS_CONSTRAINT;
    }
}
-----------------------------------------------------
namespace MyProject\Validator;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Constraint;

class LessThanValidator extends ConstraintValidator {
    public function isValid($value, Constraint $constraint) {
        $firstValue = $value->{'get' . strtoupper($constraint-
>firstProperty)}();
        $secondValue = $value->{'get' . strtoupper($constraint-
>secondProperty)}();
        if ($firstValue >= $secondValue) {
            $this->setMessage($constraint->message);
            return false;
        }
        return true;
    }
}
----------------------------------------------
/**
 * @myAssert:LessThan(firstProperty="time1", secondProperty="time2")
 */
class MyDomainObject {
    private $time1;
    private $time2;
    public function getTime1() {
        return $this->time1;
    }
    public function getTime2() {
        return $this->time2;
    }
}
----------------------------------------------
config.yml
----------------------------------------------
framework:
    ...
    validation:
        ...
        annotations:
            namespaces:
                myAssert: MyProject\Validator\Constraint\
----------------------------------------------

Daniel
On May 3, 9:04 am, Greg Militello <g...@thinkof.net> wrote:
> Hey all,
> I am not seeing a good way to use the annotation system in my case:
> I am storing 2 times (however I could easily see storing two values of ANY 
> type), and I need to ensure that time_a is less than or equal to time_b.
>
> To be more specific, I am planning to store a time range.  For reporting 
> purposes the in time can never be passed the out time.
>
> I tried using the Valid validation constraint type, however my efforts 
> failed.  Was I on the correct track, but just not successful?
>
> Perhaps this case is not so uncommon that a "Comparison" validation 
> constraint would be beneficial (I could see this getting complicate quickly 
> but I do not see an obvious solution as-is either)?  
>
> -Greg

-- 
If you want to report a vulnerability issue on symfony, please send it to 
security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "symfony users" group.
To post to this group, send email to symfony-users@googlegroups.com
To unsubscribe from this group, send email to
symfony-users+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en

Reply via email to