Question

What is the simplest (cleaner) way to validate optional fields using Kohana 3.2 ORM?

  • Validates only if the field has something, after trim filter, if it is possible.
Was it helpful?

Solution

Well, maybe (:P) I've forgot the differences between mandatory fields and optional fields:

'city' => array(
            array('not_empty'),
            array('min_length', array(':value', 3)),
            array('max_length', array(':value', 255)),
        ),
'longitude' => array(
            array('is_coordinate'),
        )

The second one, the optional field 'longitude' is only triggered if has something that is not accepted by Valid::is_coordinate.

The first one, the mandatory field, is controlled by 'not_empty', or something with similar behavior.

About the filter, ORM filters are processed before the rules, so something like this is enough:

public function filters() {
    return array(
        'longitude' => array(array('trim'))
    );
}

I'm tested myself right now, and is working nicely.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top