TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
"Valid::one_of()" method
  • I was fiddling with a login form and started wondering why there is no easy (or, at least, intuitive) way of matching an input element to an array of known values. There is the Valid::equals() method, but it only compares against one string.

    I could not find anything that does this, so I extended the Valid class with two possible solutions:

    class Valid extends Kohana_Valid {
       * Checks if the string is in an array of values.
       * @param   string  input string
       * @param   array   array of values
       * @param   boolean strict type checking
       * @return  boolean
      public static function one_of($str, Array $list, $strict=TRUE)
        return in_array($str, $list, $strict);
       * Checks that a field is exactly the value required, or in an array of values.
       * @param   string   value
       * @param   string/array   required value
       * @return  boolean
      public static function equals($value, $required)
        if (is_array($required))
          return in_array($value, $required, TRUE);
        return ($value === $required);

    The former method checks if $str is in an array of values. The latter method modifies the Valid::equals() method so that it supports both strings and arrays.

    $valid = array('yes','no');
    echo Valid::one_of($_POST['remember'], $valid) ? 'Valid' : 'Not valid';
    echo Valid::equals($_POST['remember'], $valid) ? 'Valid' : 'Not valid';

    I prefer the former method because it avoids breaking/complicating Valid::equals().

    What do you guys think? Have I overlooked some painfully obvious functionality that already does this, or could this fly? I have looked through the Valid class several times for something of this nature, and did not find anything, but I could have overlooked something.

  • I guess the example given in the docs would suit most people's needs:

    $object->rule('color', 'in_array', array(':value', array('red', 'green', 'blue')));
  • I support the zeebee response. You do not need aliases to native PHP functions in the Valid class. If you can use in_array the same way, why just don't use the built-in method.

  • @zeebee and @hkdobrev,

    At time I need a method like no_equal() to compare two fields in form and generate validation error(s) if they are equal. (both fields can not be same)

    Any solution in Validation Class or in native PHP which I may have missed?

    Please suggest.

  • I don't think there is such a method, but you can easily build your own one.

    Have a look at the docs for equals, and you should have your solution.

    public static function unequal($value, $required)
        return ($value !== $required);

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion