These forums are read-only and for archival purposes only!
Please join our new forums at
Kohana 3 validation question
  • I have a Model where either one of two fields need to be filled out (url or comment). This would mean that both fields cannot have 'not_empty' rule otherwise they're both mandatory. What would be the best way to implement this?

    What I have:

    protected $_rules = array(
    'title' => array(
    'not_empty' => NULL,
    'min_length' => array(5),
    'max_length' => array(128),
    'url' => array(
    'url' => NULL,
    'min_length' => array(5),
    'max_length' => array(256),
    'comment' => array(
    'min_length' => array(5),

  • You could make a callback that checks both the fields and creates an error if both are empty.
  • Callbacks are usually defined to a single field. How would I define such a callback?
  • I haven't done this with the new version of ORM. In KO 2.3 you had to override the validate() method and you manually created a Validation object. The Validation object was passed as the first argument to the callback, so you could access any of the information you needed. I am not sure how exactly it works in KO 3.0.

    Here's part of how I did this in one of my applications (in 2.3). This is part of the validate() method:
    if (array_key_exists('compete', $input))
    $validation->add_callbacks('competition_formats', array($this, '_check_competition_formats'));

    This is the callback
    public function _check_competition_formats(Validation $validation, $field)
    $check = $validation->as_array();
    $forms = array_key_exists('forms', $check);
    $sparring = array_key_exists('sparring', $check);

    if ( ! ($forms OR $sparring))
    $array->add_error($field, 'no_competition_formats');
  • Thanks for that. However, I was more referring to how I define the actual callback. In K3 you define your callback list like this:

    protected $_callbacks = array(
    'fieldname' => array('some_method'),

    The 'fieldname' represents a callback on a certain field. Since my check isn't bound to one field but two fields, how do I define the callback?
  • Looking at the guide, , the callback function still uses the validation object. So you should be able to just add the callback to one of the two fields and put your logic for checking the other field in the callback.

Howdy, Stranger!

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

In this Discussion