Search code examples
kohanakohana-3kohana-ormkohana-3.2

Kohana 3: Validation rule for has_many through relationship


Is it possible to create a validation rule in Kohana 3 that will validate the has_many through relationship?

Using the example on the guide page, a blog post can have many categories through the categories_posts table. Is there a validation rule that can be setup in the Post model to verify at least one category was added?

I tried the following:

public function rules()
{
    return array(
        'categories' => array(
            array(array($this, 'has'), array('categories'))
        )
    );
}

because I see that the ORM::has function will return true/false. But I think because 'categories' is a relationship, and not a field, the rule I wrote never gets checked.

Any ideas?


Solution

  • You must save Post before adding has_many relations. You can check Post for categories after saving, and mark it as draft if they were not set.