Form Generation Library - El Forum - 01-23-2010
[eluser]dinhtrung[/eluser]
Hi,
I would like to know is there anyway to render the form with submit URL different from $this->uri->uri_string()?
For example, I build controllers/articles.php. The controller has a comment form like this:
Code: $this->load->helper('date');
$form = $this->form;
$form->open('comments/commentapi/create')
->hidden('module', $module)
->hidden('module_id', $module_id)
->hidden('redirect', $this->uri->uri_string());
/* Only show this fieldset for guest. */
if (! $this->session->userdata('username'))
{
$form->fieldset('Author_information')
->text('name', 'Name', 'required|trim|max_length[40]')
->text('email', 'Email', 'required|trim|max_length[40]|valid_email')
->captcha('Captcha');
};
$form->fieldset('Comment_content')->textarea('body', 'Body', 'required', '', 'class=richtext-simple');
$form->submit('submit', 'Submit')->reset('reset', 'Reset');
$html = $form->get();
$this->render('form', $data);
Because submit data will be post to 'comments/commentapi' controllers, so those nice validation feature won't be any use, if the commentapi controller didn't write form validation process, and redirect the user back to form's url (here is /articles/).
Is there any other way to keep validation and stuff intact???
Should FGL store validation rules into session, and process it through another redirect with onsuccess() built-in?
I'm thinking of a way to do so...
Form Generation Library - El Forum - 01-25-2010
[eluser]seanloving[/eluser]
[quote author="dinhtrung" date="1264320640"]Hi,
I would like to know is there anyway to render the form with submit URL different from $this->uri->uri_string()?[/quote]
Me too. Is there a way to alter the submit URL in ->open() sometime during the validation process, e.g. inside or after ->model() ???
SL
Form Generation Library - El Forum - 01-25-2010
[eluser]Deep Arora[/eluser]
Need help .....
I have this code to generate form in my helper file:
Code: $CI->form // then we fill the form with elements
->open($search_form_action, "form_search");
$CI->form->text('title', 'title', 'trim|alpha_numeric|max_length[255]|xss_clean');
$CI->form->textarea('question', 'question', 'trim|xss_clean');
$CI->form->indent(200);
$CI->form->submit();
$CI->form->reset();
$CI->form->onsuccess('redirect', $search_form_action);
$data['search_form'] = $CI->form->get();
Then in my controller, I am using this:
Code: if ($this->form->valid){
$post_vars = $this->form->get_post();
echo '<pre>'.print_r($post_vars, TRUE).'</pre>';
}
But it displays the empty array:
The generated code in my page is as follows:
Code: <form action="http://localhost:8080/code/development/smpcore/index.php/link/listing/search" name="search" id="search" method="post"><label for="title" class="left">Title</label><input type="text" name="title" value=""><br />
<label for="category" class="left">Category</label><input type="text" name="category" value=""><br />
<div style="float:left;width:200px"> </div><div style="float:left">
<input type="submit" name="submit" value="Submit" class="button" id="submit" />
<input type="reset" name="reset" value="Reset" class="button" id="reset" /><br />
</div><div style="clear:both"></div>
</form>
What could be wrong?
Form Generation Library - El Forum - 01-25-2010
[eluser]Deep Arora[/eluser]
[quote author="Deep Arora" date="1264497762"]Need help .....
I have this code to generate form in my helper file:
Code: $CI->form // then we fill the form with elements
->open($search_form_action, "form_search");
$CI->form->text('title', 'title', 'trim|alpha_numeric|max_length[255]|xss_clean');
$CI->form->textarea('question', 'question', 'trim|xss_clean');
$CI->form->indent(200);
$CI->form->submit();
$CI->form->reset();
$CI->form->onsuccess('redirect', $search_form_action);
$data['search_form'] = $CI->form->get();
Then in my controller, I am using this:
Code: if ($this->form->valid){
$post_vars = $this->form->get_post();
echo '<pre>'.print_r($post_vars, TRUE).'</pre>';
}
But it displays the empty array:
The generated code in my page is as follows:
Code: <form action="http://localhost:8080/code/development/smpcore/index.php/link/listing/search" name="search" id="search" method="post"><label for="title" class="left">Title</label><input type="text" name="title" value=""><br />
<label for="category" class="left">Category</label><input type="text" name="category" value=""><br />
<div style="float:left;width:200px"> </div><div style="float:left">
<input type="submit" name="submit" value="Submit" class="button" id="submit" />
<input type="reset" name="reset" value="Reset" class="button" id="reset" /><br />
</div><div style="clear:both"></div>
</form>
What could be wrong?[/quote]
I just noticed that in teh generated form code, the "id" is not generated. I am using Code: $config['nameasid'] = TRUE;
in the config file. Still it doesnt generate. Can this be the problem?
Form Generation Library - El Forum - 01-26-2010
[eluser]hugle[/eluser]
[quote author="Deep Arora" date="1264498000"][quote author="Deep Arora" date="1264497762"]Need help .....
I have this code to generate form in my helper file:
Code: $CI->form // then we fill the form with elements
->open($search_form_action, "form_search");
$CI->form->text('title', 'title', 'trim|alpha_numeric|max_length[255]|xss_clean');
$CI->form->textarea('question', 'question', 'trim|xss_clean');
$CI->form->indent(200);
$CI->form->submit();
$CI->form->reset();
$CI->form->onsuccess('redirect', $search_form_action);
$data['search_form'] = $CI->form->get();
Then in my controller, I am using this:
Code: if ($this->form->valid){
$post_vars = $this->form->get_post();
echo '<pre>'.print_r($post_vars, TRUE).'</pre>';
}
But it displays the empty array:
The generated code in my page is as follows:
Code: <form action="http://localhost:8080/code/development/smpcore/index.php/link/listing/search" name="search" id="search" method="post"><label for="title" class="left">Title</label><input type="text" name="title" value=""><br />
<label for="category" class="left">Category</label><input type="text" name="category" value=""><br />
<div style="float:left;width:200px"> </div><div style="float:left">
<input type="submit" name="submit" value="Submit" class="button" id="submit" />
<input type="reset" name="reset" value="Reset" class="button" id="reset" /><br />
</div><div style="clear:both"></div>
</form>
What could be wrong?[/quote]
I just noticed that in teh generated form code, the "id" is not generated. I am using Code: $config['nameasid'] = TRUE;
in the config file. Still it doesnt generate. Can this be the problem?[/quote]
Hello, try removing:
Code: $CI->form->onsuccess('redirect', $search_form_action);
And first of all, I'd try without a helper. but right into the controller
Form Generation Library - El Forum - 01-26-2010
[eluser]dinhtrung[/eluser]
[quote author="seanloving" date="1264487698"]
Is there a way to alter the submit URL in ->open() sometime during the validation process, e.g. inside or after ->model() ???
SL[/quote]
Yes, there is!!! Just call open() again, or if you want, change the public class variable named `action`. For example:
Code: if ($condition1) $this->form->action = $this_uri;
elseif ($condition2) $this->form->action = $that_uri;
else $this->form->action = $this->uri->uri_string();
Just remember that those line must be put before get(), because when you run get(), you'll get your form processed with validation, upload and model (IMPORTANT: this only happens if the action URL is current URL!), and HTML output.
So, when action URL is differ from $this->uri->uri_string, those validation, upload and models won't work. Have to write separate controller for this.
Form Generation Library - El Forum - 01-26-2010
[eluser]Maglok[/eluser]
[quote author="hugle" date="1264301876"][quote author="Maglok" date="1263997278"]I set up the demo on my server. It seems like two 'bugs' in the library. There must be something in my host, but as far as I know it is just PHP 5.
Issues
- Does not generate labels on radiogroups
- Does not generate element_suffix
Mine: http://test.lore-ley.nl/welcome
Official demo: http://www.frankmichel.de/formgenlib/
Both use the exact same code. I only moved the stylesheet directory.[/quote]
are you sure it is clean install of FGL ?
which version of FGL? it linux or win?
I tested it on two different machines - all ok with default config/controller/view[/quote]
It is a clean install of the FGL, it is a linux system. And my test at http://test.lore-ley.nl/welcome is straight out of the box. This situation is kind of keeping me from using the library in all it's glory. I like it a lot, just those two issues are very odd. I use the most recent version.
EDIT: I have installed the full version on a different url http://www.lore-ley.nl/fgl/index.php?welcome This DOES work. Mabye the just the library download is not up to date?
Form Generation Library - El Forum - 01-26-2010
[eluser]seanloving[/eluser]
[quote author="dinhtrung" date="1264517392"][quote author="seanloving" date="1264487698"]
Is there a way to alter the submit URL in ->open() sometime during the validation process, e.g. inside or after ->model() ???
SL[/quote]
Yes, there is!!! Just call open() again, or if you want, change the public class variable named `action`. For example:
Code: if ($condition1) $this->form->action = $this_uri;
elseif ($condition2) $this->form->action = $that_uri;
else $this->form->action = $this->uri->uri_string();
Just remember that those line must be put before get(), because when you run get(), you'll get your form processed with validation, upload and model (IMPORTANT: this only happens if the action URL is current URL!), and HTML output.
So, when action URL is differ from $this->uri->uri_string, those validation, upload and models won't work. Have to write separate controller for this. [/quote]
@dinhtrung - thank you for pointing out what seems obvious now. -SL
Form Generation Library - El Forum - 02-03-2010
[eluser]seanloving[/eluser]
Sometimes you might want to use a single form to add or edit a database record like this:
CONTROLLER
Code: <?php
class Contacts extends Application {
function Contacts()
{
parent::Application();
$this->load->library('form');
$this->auth->restrict('Admin');
$this->load->model('contacts_model');
}
function index( )
{
// display to the user (Admin) a list of contacts
// MODEL
$headerdata['title'] = "Contact List";
$maindata['heading'] = "Contact List";
$maindata['button'] = anchor('contacts/add','+ add new contact');
$maindata['contacts'] = $this->contacts_model->find_all('contact_id', 'ASC');
// VIEW
$this->load->view('header',$headerdata);
$this->load->view('contacts', $maindata);
$this->load->view('footer');
} // end of index()
function add()
{
// create a new contact
// MODEL
$headerdata['title']="Add Contact";
$maindata = $this->_form('add'); // returns blank form, or submitted form with errors
$maindata['heading']="Add Contact";
$maindata['button'] = anchor('contacts','go back');
// VIEW
$this->load->view('header', $headerdata);
$this->load->view('contacts', $maindata);
$this->load->view('footer');
} // end of add()
function edit($contact_id='')
{
// display the form populated with data corresponding to the requested contact
// or else process an edit form submission
// MODEL
if( $contact_id )
{
// form was requested
// get the record corresponding to this $contact_id (if one already exists)
$rules = array( 'contact_id'=>$contact_id );
$_POST = $this->contacts_model->get_row( $rules );
}
// else data was posted
// prepare remaining view information
$headerdata['title']="Edit Contact";
$maindata = $this->_form( $_POST ); // gets form data as an array
$maindata['heading']="Edit Contact";
$maindata['button'] = anchor('contacts','go back');
// VIEW
$this->load->view('header', $headerdata);
$this->load->view('contacts', $maindata);
$this->load->view('footer');
} // end of edit()
function _form( $contact )
{
// either "add a contact" or "edit a contact"
$action = ($contact=='add') ? 'add' : 'edit';
// action is add or edit
$this->form->open('contacts/'.$action)
->hidden('contact_id', $this->input->post('contact_id'))
->fieldset( ($action=='add') ? 'Add New Contact' : 'Edit Contact Details' )
->text('firstname|firstnameID', 'First Name', 'required|trim|max_length[50]', $this->input->post('firstname'))
->text('lastname|lastnameID', 'Last Name', 'required|trim|max_length[50]', $this->input->post('lastname'))
->br()
->text('telephone|telephoneID', 'Telephone', 'required|trim|max_length[10]', $this->input->post('telephone'))
->text('email|emailID', 'Email', 'required|trim|max_length[50]', $this->input->post('email'))
->br()
->text('notes|notesID', 'Notes', 'required|trim|max_length[250]', $this->input->post('notes'))
->br()
->submit( ($action=='add') ? 'Add': 'Update', $action )
->model('contacts_model', $action.'_contact') // writes to database if valid, otherwise sets a form submission error
->validate();
// redirect if ->model() was successful
if( $this->form->valid )
{
$this->session->set_flashdata('success', ($action=='add') ? 'Contact was added to the database.' : 'Contact has been updated.');
redirect( 'contacts' );
}
// return the form and its data, and any submission errors
$data['form'] = $this->form->get(); // gets the submitted form data as a string
$data['errors'] = $this->form->errors;
return $data;
} // end of _form
}
/* End of file contacts.php */
/* Location: ./application/modules/contacts/controllers/ */
?>
Form Generation Library - El Forum - 02-03-2010
[eluser]seanloving[/eluser]
... and here is the model to go with the controller
MODEL
Code: <?php
class Contacts_model extends MX_Model {
function Contacts_model()
{
parent::MX_Model();
$this->default_table = 'contacts';
}
function add_contact( &$form, $data )
{
// called by the "contacts/add" form whenever (pre-validated) data is posted to it
// verify the posted contact name (firstname + lastname) does not already exist
$rules = array( 'firstname' => $this->input->post('firstname'),
'lastname' => $this->input->post('lastname') );
if( $this->get_row($rules) )
return $form->add_error('contact_id', 'This contact already exists!');
// prepare (the posted data) for database insert
unset($data['add']); // form submit button (not a database field / object property)
$data['updated_by_user_id'] = $this->session->userdata('user_id'); // not part of the form
// write new record to the database
return $this->create($data);
}
function edit_contact( &$form, $data )
{
// the "contacts/edit/" form calls this function when data is posted to it
// TBD - don't let a user change a contact name to the name of an already existing contact because contact names (firstname + lastname) are unique
// see if the posted contact_id identifies a unique record
$rules = array( 'contact_id' => $this->input->post('contact_id') );
if( $this->get_row($rules) == false )
{
return $form->add_error('contact_id', 'No such contact to edit!');
}
// prepare (the posted data) for database update
unset($data['edit']); // form submit button (not a database field / object property)
$data['updated_by_user_id'] = $this->session->userdata('user_id'); // not part of the form
// update the contact_id record with validated form $data
return $this->updater($data,$rules);
}
}
?>
This single view file supports calls from each of index and add and edit
VIEW
Code: <div id="content" class="main">
<div id="container">
<!-- insert left side-bar navigation for this page -->
<div id="navadminID">
<?php $this->load->view('nav_side'); ?>
</div>
<div id="mainID">
<div id="content" style="clear:both;">
<br>
<h2><?php echo $heading; ?></h2>
<?php
// optional heading button
if( isset($button) ) echo "<p id=\"goback_button\">".$button."</p>";
// show flash data
echo '<p style="color:red">'.$this->session->flashdata('success').'</p><br>';
// show the form
if( isset($form) )
{
$this->load->view('form');
}
// or else show the list of records for this report
else
{
$this->load->view('contact_report');
}
?>
</div>
</div><!-- // end #mainID -->
</div> <!-- // end #container -->
</div> <!-- // end #content -->
I hope it helps somebody.
If any experts can take a look, any feedback would be appreciated.
--Sean Loving
|