Updated Formation

Last couple of days I made some updates to Formation and I commited them to Google Code SVN. There are numerous updates and I hope to work out a more elaborate manual as well as finalize some changes as the Element_Multi which is the basis for lists of radio/checkbox elements as well as multi-select boxes.

Changes:

  • Numerous bugfixes
  • Setting order of form elements afterwards
  • Cross site request forgery field - already there in an older release but it’s nice :)
  • Deprecated set_values over populate_form, set_values works as an alias
  • Lot’s of other stuff I can’t quite recall
    • Model_Formation
    • db fields preceded with ‘is_’ will turn into checkboxes
    • Model formation factory
    • ‘Belongs to’ relationship will turn field into dropdown for relations. Needs an id field and a name field (name fields can be emulated with __get)

I quite like the model_formation factory, it works like this:

$form=Model_Formation::factory(ORM::factory('user',1))
						->set_form_fields(array('username','email','description'))
						->set_class('user_form');

And you get a form from the user model filled with the user where id=1. It only shows the fields: username, email and description. Model_Validation also retrieves rules set in the model. Again, I hope to document all functionality a little more in the future.

The other thing I’m happy about is the handling of relationships. It’s far from done since only belongs_to is rendered. Anyway, when you have an article model and it belongs to the user and following you have a field user_id in your article table, model_validation will render a dropdown for that field showing you all users you can assign the relationship to. It’s ideal for my CRUD operations. Note that you need a name field in the table you want to link to, you can fake a name field with __get in ORM.

The update is in svn but soon I hope to do some serious cleanups and expand documentation a bit.


9 Responses to “Updated Formation”

  1. phelz Says:

    i’m testing model_formation but i’m having some problems i can’t sort out.
    i’m using latest svn, putting all your code into a module dir named formation & loading it in config.
    if i put this code on a controller

    public function edit($id = FALSE)
    {
    	$form = new Model_Formation('customer');
    	$this->template->content = $form->build_form();
    }

    i have this result:
    Fatal error: Class ‘customer’ not found in /Users/phelipe/Sites/moorea/modules/formation/libraries/Model_Formation.php on line 32

    apparently it does’n find the model which - obviously - isn’t in module but in application->models

    i’ve also tried with
    $form = Model_Formation::factory(ORM::factory(’customer’, $id));

    no error but blank form - no fields displayed.

    where’s my fault?

    thank

  2. phelz Says:

    problem solved: i had another MY_ORM.php in application->libraries, after removing that file the form works.
    this lead to another problem: how can i have my personal ORM extension if there’s already a MY_ORM in modules->formation->libreries?

  3. dlib Says:

    Copy your modifications in ORM to the MY_ORM.php It’s the only way. Just make sure nothing clashes.

  4. dlib Says:

    Oh, and

    $form = new Model_Formation(’customer’);

    Should be

    $form = new Model_Formation('Customer_Model');

    I’ve been meaning to add your syntax but haven’t had the time.

  5. spirit Says:

    Do you think it would be possible to rewrite Formation using the official Validation lib for best integration?

  6. dlib Says:

    You could but the approach would be radically different. I’d create a Formation class and have a validate object within it for the official Validation lib. It would be a complete rewrite though.

    Problem with converting Fromation to the validation library is rules, it uses normal callbacks whereas my Validation/Formation lib uses classes for rules. My main reason for doing this that classes allow for error messages like: ‘this field {field} needs {10} chars, you gave me {5}’; I might also implement classes for the filters since it works pretty neat.

    I might rename validation to validate though, so the validation lib and my validation lib can coexist peacefully :)

  7. Alex Rodrigues Says:

    Hi! How can I do a radiobutton list? I’ve read the code but I couldn’t find a way to do it. If it’s not ready do you want help? I mean, I really need it and if I do it for me I could contribute to Formation with good php code.

  8. RastaTech Says:

    hey dlib, i’m a little confused; is Formation just an alternative to Forge and the Form library? What are the advantages/disadvantages of each? Why so many ways to do forms?

  9. dlib Says:

    Radio buttons are on their way, I’ve already placed a class Multi_Select or something in svn which should replace checkbox groups, multiselect dropdowns etc. If you need a quick fix for radio buttons you could always hack it in with the checkbox groups class and just replace all relevent checkbox code with radio code. It should work although I haven’t tested it.

    Formation is an alternative to Forge. It started out of my own need and I released it. For me it offers some advantages over Forge, namely reordering elements, validation as well as filtering, more validation rules (with custom error messages which can be very precise). Rules are classes and it’s easy to add new ones. CSRF protection in forms. Very clear standard Form view. Generator so for the jQuery validation plugin, instant js validation with a few lines of code. It also is a replacement of the validation library and as such offers the same as the Formation library except that it doesn’t generate your forms. It does provide the same rules, filters.

    Together with ORM and the

  10. Advanced
  11. Easy
  12. Medium
  13. documentation
  14. Documentation/API
  15. Forge
  16. Form tutorial
  17. Formation
  18. Forums
  19. From the web
  20. Getting started
  21. jQuery
  22. Kohana
  23. ORM
  24. Subversion
  25. Tutorials
  26. Under the hood
  27. Validation
  28. Tags

    Blogroll