I am encountering a scenario where I need to enforce a rule that a
submitted value is NOT equal to something else. Before I write it, I wonder
if anyone else has done this already.

Specifically, it's kind of an unusual situation where the user can insert
what I call a "location" into a db table, and this model supports one level
of nesting -- that is, a location can either be in another location, or it
can be a top-level location, that's it. These locations have an attribute
"type," and the rule I want to enforce is:  a location of a given type
cannot have as parent a location of the same type. Example:  a hotel room is
in a hotel. A hotel cannot be in another hotel.

The form has a select menus for choosing a parent location, and for choosing
the type of the location. I am thinking of writing a custom validator for
the "select type" element, and throwing the parent location's type_id at its
constructor. Then isValid($value) will simply compare $value to
$this->parent_type_id.

Do you think this makes sense?

class My_Validate_NotEqual
{
    function __construct($valueThatShouldBeDifferent) {
        $this->valueThatShouldBeDifferent = $valueThatShouldBeDifferent;
   }

   function isValid($value) {
        // stuff omitted for brevity, but basically...
        return $value != $this->valueThatShouldBeDifferent;
  }

}


Thanks.

-- 
Demand health care for everyone:
http://mobilizeforhealthcare.org/

--
David Mintz
http://davidmintz.org/

Reply via email to