📜 ⬆️ ⬇️

Working with forms

A well-known thing is the development of any web application can be divided into stages, and the stages themselves - into typical tasks. One of the most common typical tasks is working with forms . Every time when the programmer has to deal with it, you can catch some dismay if the annoying routine is not framed properly. Before leaving under the cat, I will show you how the work with the forms in cogear is implemented :
$ this ->form-> set ( 'add-comments' )
->input( 'subject' ,array( 'validation' => 'required|max_length[80]' ))
->editor( 'body' ,array( 'validation' => 'required|min_length[5]' ))
->buttons( 'send' );
if ($result = $ this ->form->result()){
if ($ this ->form->save( 'comments' ,$result)){
redirect( '/node_url' );
}
}
$ this ->form->compile();


* This source code was highlighted with Source Code Highlighter .


Above shows the most simple, but very effective form of work, sorry for the pun, with the forms.

One code is responsible for everything


There is no need to create separate controller methods for catching data - all functions are taken on by one piece of code.

First of all, we set the form id , which is useful to us when using hooks or in another case.
$ this ->form-> set ( 'add-comments' )

* This source code was highlighted with Source Code Highlighter .

After that, the chain sets the elements and their parameters, as well as form buttons.
// ""
->input( 'subject' , array( 'validation' => 'required|max_length[80]' ))
// ""
->editor( 'body' ,array( 'validation' => 'required|min_length[5]' ))
//
->buttons( 'send' );

* This source code was highlighted with Source Code Highlighter .

')
Immediately a slight digression to answer possible questions:
1. When forming the label output to the element, it is taken on the basis of the specified, or a common translation variable (the general ones are stored in the edit section).
Suppose we have a language file in which the names and descriptions of our fields are given.
[my_form]
subject = " "
subject_description = " . 80 ."
body = ""
body_description = " – 5 , ."



// - i18n
d( 'my_form' );
$ this ->form-> set ( 'add-comments' )
->input( 'subject' , array( 'validation' => 'required|max_length[80]' ))
->editor( 'body' ,array( 'validation' => 'required|min_length[5]' ))
->buttons( 'send' );
if ($result = $ this ->form->result()){
if ($ this ->form->save( 'comments' ,$result)){
redirect( '/node_url' );
}
}
$ this ->form->compile();


* This source code was highlighted with Source Code Highlighter .


If before setting the form we set the current section of translations, then at the output we will get a form of the following form:


2. Error handling is automatically based on the specified rules. No further action is required on your part.


Work with errors


You submit the form, and if the validation fails, the form is re-displayed with errors.


You can enhance the form by adding validation to javascript before sending it.
// - i18n
d( 'my_form' );
$ this ->form-> set ( 'add-comments' )
->input( 'subject' , array( 'validation' => 'required|max_length[80]' , 'js_validation' => 'required|length[5,80]' ))
->editor( 'body' ,array( 'validation' => 'required|min_length[5]' , 'js_validation' => 'required|length[5,-1]' ))
->buttons( 'send' );
if ($result = $ this ->form->result()){
if ($ this ->form->save( 'comments' ,$result)){
redirect( '/node_url' );
}
}
$ this ->form->compile();

* This source code was highlighted with Source Code Highlighter .


Having updated the page we will see that now the scripts will not allow us to send the form before it is filled correctly.


If you have already wondered why the rules for pre- and post-validation differ, I answer:
- For pre-validation, the modified MooTools.Floor Form Check class is used.
- For post-validation, a modified CodeIgniter library (which underlies the engine) is used.

Finally I will give a more detailed example - with a simplified creation / editing of topics.

class Index extends Controller{

/**
* .
*
* @param int $id id
* @return void
*/
function createdit($id = FALSE){
// - i18n
d( 'node_edit' );
// ,
if ($id && $node = $ this ->db->get_where( 'nodes' ,array( 'id' =>$id))->row()){
/*
*
*
* …
* edit = " '%s'"
* …
*/
title(t( 'edit' ,$node->name));
}
else {
//
title(t( 'node_edit create' ));
}
//
$ this ->form-> set ( 'node-createdit' )
// ""
->input( 'subject' , array( 'validation' => 'required|max_length[80]' , 'js_validation' => 'required|length[-1,80]' ))
// ""
->editor( 'body' ,array( 'validation' => 'required|min_length[5]' , 'js_validation' => 'required|length[5,-1]' ))
//
// , "" "".
->buttons(empty($node) ? 'create' : 'save' );

// —
if (!empty($node)){
$ this ->form->set_values($node);
}
//
if ($result = $ this ->form->result()){
// —
if (!empty($node) && $ this ->form->update( 'nodes' ,$result,array( 'id' =>$node->id)){
redirect( '/nodes/' .$node->id);
}
//
elseif($ this ->form->save( 'comments' ,$result)){
redirect( '/' .$ this ->form->insert_id);
}
}
//
$ this ->form->compile();
}

}


* This source code was highlighted with Source Code Highlighter .


Of course, this topic shows the simplest examples of working with forms in the cogear - for ease of perception and clarity.
Our implementation of working with forms has proven itself well over the past year - it allows us to reduce the time for developing typical tasks for entering and processing information to a minimum. Yes, to someone it may not seem perfect, but

I hope you enjoyed the brief excursion into your own ideas.

If there is a question - ask, if you want to continue - it will be.

How do you organize work with forms?

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


All Articles