Formation/Validation

For personal needs I required a different Validation class and it slowly expanded to also come with form generation. I call it Formation. It’s inspired by the validation class in Kohana’s svn but also by Forge and Zend_Form.

Formation/Validation can be found here. There is some documentation but it’s not complete. It should be pretty clear though if you look at the examples.

I welcome any feedback. You can file bugs and patches at the google code page. The latest release can be found on the download page over there or grab the svn version. I’d very much appreciate additional rules or special elements (csrf, dateselects etc.)

Note I will implement support similar to the JqueryForge validation shortly with a register method. I will then also stop updating JqueryForge validation.

Example of Formation

$form=new Formation;
$form->set_method('POST'); //is default but still
$form->set_attrib('id','form_css_id');
 
$form->add_element('input','username')->add_rule('Rule_Exact_Length',4);
 
if($form->validate())
{
  echo 'yah';
}
else
{
  echo $form; //will call $form->render()
}

Example of normal Validation

{{{
$_POST=new Validation($_POST);
$_POST['username']->add_rule('Rule_Required');
$_POST['password']->add_pre_filter('trim')->add_rule('Rule_Min_Length',8)->add_rule('Rule_Required');
if($_POST->validate())
{
 echo 'have fun';
}
else
{
 var_dump($_POST->errors();
}
}}}


9 Responses to “Formation/Validation”

  1. Rick Jolly Says:

    I’m wondering how Formation/Validation differs from the Kohana Forge and validation libraries.

    BTW, your kohana blog is an excellent resource. A little hard to find though.

  2. admin Says:

    Forge and Kohana’s new validation library are two different things. In itself I was perfectly happy with Forge but needed some extras. Initially I wrote a MY_Forge which did most of the things I needed. This library sort of evolved from that and the new validation library which worked well except for some quirks (message handling, syntax and rules as callbacks)

    This library combines a simple standalone validation library with rules,filter,callback, message handling. Using inheritance it can be extended to also do form generation. This way there is a single syntax for adding rules/messages and also a single repository of rules.

  3. Rick Jolly Says:

    Great explanation. Thank you.

  4. neovive Says:

    This looks like an excellent module. I agree with Rick, this blog is a great resource for Kohana users.

  5. ggs Says:

    Shouldn’t the validation mechanism rather be suited to work within Models/ORM ??? A model receiving data should validate/filter it and possibly throw errors, which in turn could be rendered by the view. This would help the model to be more consistent and independent from controllers/forms used

  6. admin Says:

    I agree with you but forms not always map to models so that might complicate things a bit.

    In fact I a new post I show off some code which renders the form from the model and where you indeed declare rules and filters in the model. You then ask Formation to render the form according to the fields, rules and filters in the model.

    I am working on an extension so you can also use normal validation within the model without form generation and therefore will do exactly what you mean. When you don’t generate your forms the model is the right place to do validation.

  7. ggs Says:

    Thats right, I have expressed myself a bit unprecisely, should rather had typed “within Models too”. Good to hear about that extension coming!

  8. Rick Jolly Says:

    For displaying validation errors, I’m wondering how to override the field name? The name defaults to the name of the form element, but I’d like to use the form field title as the user sees it.

    Thanks.

  9. dlib Says:
    $form->add_element('input','field')->set_screen_name('your field name');

Leave a Comment