📜 ⬆️ ⬇️

Kohana-form: module of management and form generation

Hello. With this article I want to present my forms module for the Kohana framework, which was written inspired by a similar module for Django.

I will not go deep into prehistory and reflection, but let me get straight to the point. We first describe the shortcomings:


Advantages:
')


Details under the cut.



How to use:

Form creation

<?php defined('SYSPATH') OR die('No direct access allowed.'); class Form_Login extends Form { public static function meta() { return array( "fields" => array( "login" => Field::factory("Varchar"), "password" => Field::factory("Password"), ), "options" => array( //   .    . "valid_messages_file" => "login", //      ,     messages "theme" => "base" //  . base -   ,    2 : base, nolabels. nolabels    base    label ), ); } } 


Available field types



How to show the form

Just show

  <form> <?php echo $form; ?> <input type="submit" value="Add"/> </form> 


Show with bootstrap styles

  <form method="POST" role="form"> <?php foreach ($form as $field): ?> <div class="form-group"> <?php $field->css_class(array("form-control")); ?> <?php foreach ($field->errors() as $error): ?> <div class="alert alert-danger"> <?php echo $error; ?> </div> <?php endforeach; ?> <?php echo $field; ?> </div> <? endforeach; ?> <input type="submit" class="btn btn-primary" value="Add"/> </form> 


Create a model form

 class Form_Article extends ModelForm { public static function meta() { return array( "fields" => array( //  .    ,   . "image" => Field::factory("Image") ), "options" => array( "model" => ORM::factory("Article"), //       "display_fields" => array("title", "body", "image"), //  "valid_messages_file" => "news", //     "except_fields" => array() //      ), ); } } 


Create a form for a specific entity in the database

 Form::factory("Article", array(), $id); 


Create a form for a specific data set

 Form::factory("Article", array("title" => "Hello, Habr!")); 


Get and save the model form

 public function action_add() { if ($this->request->method() == "POST") { $form = Form::factory("Article", $this->request->post()); $form->add_field( Field::factory("Hidden") ->name("user") ->value(Auth::instance() ->get_user()) ); if ($form->validate()) $form->save(); } } 


Create formset

 <?php defined('SYSPATH') OR die('No direct access allowed.'); class Formset_News extends Formset { public static function meta() { return array( "base_form" => "News", "theme" => "bootstrap" ); } } 


Formsets also implement Iterator, so you can easily access each element.

In short, this is all the functionality implemented at the moment. Naturally, there are drawbacks both in terms of the purity of the code, and in some other aspects. And publishing this module here, I hope that the open source community will respond, and together we will finally be able to make a decent module for working with forms under the Kohana framework.

All code is available on github - kohana-form , you can make forks, send pullreves and write in “Issues”. I will be glad comments and advice.

Source: https://habr.com/ru/post/216187/


All Articles