CodeIgniter Forums
Form Generation Library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Form Generation Library (/showthread.php?tid=16439)



Form Generation Library - El Forum - 11-24-2009

[eluser]dinhtrung[/eluser]
I tried to build a login form with Tank Auth and Form Generation, but Form validation keep complain
Code:
add_error: Element name "username" does not exist
Here is my code:
Code:
$this->form->open($this->uri->uri_string())
->text('username', "Username or Email", 'required|trim|xss_clean')
->password('password', "Password", 'required|trim|xss_clean')
->bool('remember', 'Remeber Me')
->submit('Submit', 'submit')
->reset('Reset', 'reset')
->model('dev_m', 'login')                        
->onsuccess('redirect', 'dev/status');
And in dev_m model, I write the login function like this:
Code:
function login(&$form, $data)
{
    if (! array_key_exists('remember', $data)) $data['remember'] = 0;
    if (! $this->tank_auth->login(

        $data['username'],

        $data['password'],

        $data['remember'],

        $this->config->item('login_by_username'),

        $this->config->item('login_by_email')))
    {
        $form->add_error('username', $this->tank_auth->get_error_message());
    }
    else return TRUE;
}
I could not figure out where I went wrong. Any idea?


Form Generation Library - El Forum - 11-25-2009

[eluser]hugle[/eluser]
[quote author="dinhtrung" date="1259139436"]I tried to build a login form with Tank Auth and Form Generation, but Form validation keep complain
Code:
add_error: Element name "username" does not exist
Here is my code:
Code:
$this->form->open($this->uri->uri_string())
->text('username', "Username or Email", 'required|trim|xss_clean')
->password('password', "Password", 'required|trim|xss_clean')
->bool('remember', 'Remeber Me')
->submit('Submit', 'submit')
->reset('Reset', 'reset')
->model('dev_m', 'login')                        
->onsuccess('redirect', 'dev/status');
And in dev_m model, I write the login function like this:
Code:
function login(&$form, $data)
{
    if (! array_key_exists('remember', $data)) $data['remember'] = 0;
    if (! $this->tank_auth->login(

        $data['username'],

        $data['password'],

        $data['remember'],

        $this->config->item('login_by_username'),

        $this->config->item('login_by_email')))
    {
        $form->add_error('username', $this->tank_auth->get_error_message());
    }
    else return TRUE;
}
I could not figure out where I went wrong. Any idea?[/quote]

Hello
It's strange, your Form setup looks good. I can't see why it can be related with Tank Auth. Maybe this appeared after all your modifications/patches to form library?

Maybe you should try not modified version?

and a little info about model..
the $data returns not the validated data, but $_POST, author have already fixed this, and soon will be releasing new version of this great lib.

As a temp sullution I made this:

added function to model:
Code:
function _process_data($form)
    {
        $post_data = $form->CI->form_validation->_field_data;
        foreach ($post_data as $key => $value) {
            $key = str_replace("[]",'', $key); // listboxes produces KEY names like country[] (maybe checkboxes also)
            $ndata[$key] = $value['postdata'];
        }
        return $ndata;
    }

and later, instead of $data, I use for example:
Code:
function support(&$form, $data)
{
  $post_data = $this->_process_data($form); // this returns validated Form data
}



Form Generation Library - El Forum - 11-25-2009

[eluser]dinhtrung[/eluser]
Quote:Maybe you should try not modified version?
Yep, tried with the un-modified version right after seeing the error message. It still there. I tried print_r($this), but it keep print out the structure of Tank_Auth object, not Form library. Maybe it is the cause.
Anyway, I worked around this issue by using validate(), not model().
Quote:the $data returns not the validated data, but $_POST, author have already fixed this, and soon will be releasing new version of this great lib.
Yeah, waiting for the new version one.
I'll try to integrate some more special types of fields.
As I usually have to generate time-based report, I think of a form element with jQuery clockpick.js and jQuery-UI datepicker. Another regularly used element is CKeditor or TinyMCE. But it won't be only PHP anymore. Don't know if these kind of element should be add into the library or not.
I used to write alot of form elements in pure HTML and <?php echo ?> statement in my views... and now really tired of this similar tasks... Sad Really, Form Generation helps me alot.


Form Generation Library - El Forum - 11-25-2009

[eluser]hugle[/eluser]
[quote author="dinhtrung" date="1259183816"]
Quote:Maybe you should try not modified version?
Yep, tried with the un-modified version right after seeing the error message. It still there. I tried print_r($this), but it keep print out the structure of Tank_Auth object, not Form library. Maybe it is the cause.
Anyway, I worked around this issue by using validate(), not model().
Quote:the $data returns not the validated data, but $_POST, author have already fixed this, and soon will be releasing new version of this great lib.
Yeah, waiting for the new version one.
I'll try to integrate some more special types of fields.
As I usually have to generate time-based report, I think of a form element with jQuery clockpick.js and jQuery-UI datepicker. Another regularly used element is CKeditor or TinyMCE. But it won't be only PHP anymore. Don't know if these kind of element should be add into the library or not.
I used to write alot of form elements in pure HTML and <?php echo ?> statement in my views... and now really tired of this similar tasks... Sad Really, Form Generation helps me alot.[/quote]

Well, models are a bit buggy in this version, I have also switched to VALIDATE();

this library AMAZINGLY descreases the code need to be written etc etc. with this lib with 5 lines of code you do all logic - superior.

Well, this lib from start seems very nice, so I think such clockpick.js and datepickers would appear in this lib soon.

Maybe it would be even better to add smth:
config['enable_javasript_validation'] = TRUE;
And it would generate jQuery validation rules - seems perfect to meSmile

But as for now we must exclude all of other bugs Smile

p.s. also using TankAuth hereSmile))


Form Generation Library - El Forum - 11-26-2009

[eluser]Mat-Moo[/eluser]
Is there an easy way to add wrappers around ->text ->password etc.? I need to get the format
Code:
<div><label>sss</label>&lt;input blah&gt;&lt;/div>
that possible? am I missing the obvious?


Form Generation Library - El Forum - 11-26-2009

[eluser]hugle[/eluser]
Hello

It is quite simple, imho

If I understood you right, you can try:

Code:
$this->form->open('/upload/test')
        ->fieldset('Please enter user data')
        ->html('<div>');
        ->text('username', 'Your Username', 'trim|xss_clean|required');
        ->password('pass', 'Your Password', 'trim|xss_clean|md5');
        ->html('</div>');
        ->submit('Submit')
        ->validate();
        
        if ($this->form->valid)
        {
            $post_data= array();
            $post_data = $this->form->get_post();
            echo '<pre> VALIDATED DATA: ';print_r($post_data);echo '</pre>';
        }

        $data['form'] = $this->form->get();
        $data['errors'] = $this->form->errors;



Form Generation Library - El Forum - 11-26-2009

[eluser]Mat-Moo[/eluser]
I need it around each entity e.g.
Code:
->html('<div>');
->text('username', 'Your Username', 'trim|xss_clean|required');
->html('</div>');
->html('<div>');
->password('pass', 'Your Password', 'trim|xss_clean|md5');
->html('</div>');
on long forms this will be really messy!


Form Generation Library - El Forum - 11-26-2009

[eluser]hugle[/eluser]
well...
maybe I then would do for example:
str_replace('<label>', '<div><label>');
then would fix somehow 1st and last matches.. it would do the trick Smile
I think Smile


Form Generation Library - El Forum - 11-26-2009

[eluser]dinhtrung[/eluser]
[quote author="hugle" date="1259298926"]well...
maybe I then would do for example:
str_replace('<label>', '<div><label>');
then would fix somehow 1st and last matches.. it would do the trick Smile
I think Smile[/quote]
That's why I modified Form Generation for support various form structure. Look for my patch in this thread.
Then in config/form.php, add following config variables:
Code:
$config['label_prefix'] = "<div>\n";
$config['label_suffix'] = "<br>\n";
$config['input_prefix'] = "\t\t";
$config['input_suffix'] = "</div>\n";
This will generate the form structure you wanted. Basically:
$label_prefix.form_label().$label_suffix.$input_prefix.form_input().$input_suffix
I also add $config['fieldset_open'] and $config['fieldset_close'] in case you use ol, ul and li as form element separator. Example:
Code:
$config['label_prefix'] = "<li>\n";
$config['label_suffix'] = "\n";
$config['input_prefix'] = "\t\t";
$config['input_suffix'] = "</li>\n";
$config['fieldset_open'] = "<ul>";
$config['fieldset_close'] = "</ul>";
Will generate something like
Code:
<fieldset><ul>
<li><label for='username'>Username</label>
    &lt;input type='text' name='username'&gt;&lt;/li>
<li><label for='password'>Password</label>
    &lt;input type='password' name='password'&gt;&lt;/li>
</ul></fieldset>



Form Generation Library - El Forum - 11-27-2009

[eluser]hugle[/eluser]
[quote author="dinhtrung" date="1259319902"][quote author="hugle" date="1259298926"]
That's why I modified Form Generation for support various form structure. Look for my patch in this thread.
Then in config/form.php, add following config variables:
Code:
$config['label_prefix'] = "<div>\n";
$config['label_suffix'] = "<br>\n";
$config['input_prefix'] = "\t\t";
$config['input_suffix'] = "</div>\n";
[/quote]

I really like your patch, but haven't tried it yet Smile just wanna author to release the patched version with all our thoughts Smile

Btw, maybe you have already patch for smth like this:
for example, I want my output look like:
<label>Username:</label> <-- it's with ":"
so I basicaly set label to have an ":" -
Code:
->text('username', 'Username:', 'trim|xss_clean|required|unique[users.username]');

But when I get errors, I don't want the ":" to appear...

Basically now I play with str_replace, But maybe there is already more elegant sollution?Smile

Thanks