$ 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 .
$ this ->form-> set ( 'add-comments' )
* This source code was highlighted with Source Code Highlighter .
// ""
->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.
// - 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 .
…
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 .
Source: https://habr.com/ru/post/85123/
All Articles