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-20-2009

[eluser]Mat-Moo[/eluser]
Check out the "Model" option ->
http://frankmichel.de/formgenlib/user_guide/methods/model.html
Should have what you need


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

[eluser]score[/eluser]
EDIT:

I created a model named check_email

Code:
<?php

class Check_email extends Model {
    
        function Check_email()
        {
            parent::Model();
        }
        
        function db_check_email() {
            
            //check if email is registered
            
        }
        
        function db_insert(&$form, $data) {
            // insert signup to database
           $this->db->insert('invites', $data);
        }
}


Code:
<?php

class Welcome extends Controller {

    function Welcome(){
        parent::Controller();
        $this->load->helper('url');    
    }
    
    function index(){
        $this->load->library('form');
        $this->form
        ->open('welcome')
        ->text('firstName','First name', 'trim|required|xss_clean')
        ->text('lastName','Last name', 'trim|required|xss_clean')
        ->text('email','email','trim|required|xss_clean|valid_email')
        ->submit('Register')
        ->model('check_email','db_insert')
        ->onsuccess('redirect', 'welcome/success');
        
        $data['form'] = $this->form->get();
        $this->load->view('signup_form', $data);
    }
    
    function success(){
        $this->load->view('success');

        
    }
    
}


But im getting this error now..


A Database Error Occurred

Error Number: 1054

Unknown column 'submit' in 'field list'

INSERT INTO `invites` (`firstname`, `lastname`, `email`, `submit`) VALUES ('asd', 'asd', '[email protected]', 'Register')


What have I done wrong? Big Grin


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

[eluser]score[/eluser]
Managed to solve my problem.

heres my function to check if the email exists in the database.
Code:
class Check_email extends Model {
    
        function Check_email()
        {
            parent::Model();
        }
        
        
        // function to check database if email exists, if it doesnt exist -> insert user to database
        function db_check_email(&$form, $data) {
            
            $email = $data['email'];
            
    
            $this->db->where('email', $email);
            $this->db->select('email');
            $query = $this->db->get('invites');
    
            if ($query->num_rows() == 0)
            {
                   $this->db->insert('invites', $data);            
            }else{
              $form->add_error('email', 'This email adress is already registered.');
                
            }
        }
            
}



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

[eluser]dinhtrung[/eluser]
Form Generation is such a great library! Save a lot of time to write form elements. But it'll be better if we can adjust it to generate various form structure.
As of The art and science of CSS demonstrate, a form maybe like this:
Code:
<form>
<fieldset>
<ul>
<li><label>My Label</label>&lt;input type='text' name='my_input' id='my_input'&gt;&lt;/li>
</ul>
</fieldset>
<fieldset class='button'>
&lt;input type='submit' value='Submit'&gt;&lt;input type='reset' value='Reset'>
</fieldset>
In jQuery UI homepage, I saw a form like this
Code:
&lt;form&gt;
<dl>
<dt><label>My Label</dt>
<dd>&lt;input name='my_input' id='my_input'&gt;&lt;/dd>
</dl>
&lt;/form&gt;
Also, there's form with <p> and <br> as separator, like ones in Drupal and in BlueprintCSS framework.
So, can we add more config elements to form config file, like:
Code:
$config['label_prefix'] = "<p>";
$config['label_suffix'] = "<br>";
$config['input_prefix'] = "";
$config['input_suffix'] = "</p>";
Will give
Code:
<p><label>My Label</label><br>
&lt;input type='text' name='myinput'&gt;&lt;/p>
Just a small suggestion, for more creative way to render forms.
Also, when I use button() instead of submit(), I can't submit my form. I couldn't understand why. Although output_profiler show that there are POST variables, but the form didn't call the model method. I prefer button with type='submit' rather than just input type='submit' for more theme attributes.


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

[eluser]dinhtrung[/eluser]
I extend your class to populate data in one function, instead of all available fields.
Code:
function populate($values)
    {
        if (! $this->_posted)
        foreach ($values as $k => $v) $this->set_value($k, $v);
        return $this;
    }
For example, I want to update a row in the table, I just get the row_array() then run ->populate($result->row_array()) instead of having to type ->text('name', 'Label', 'value') all the time.


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

[eluser]macigniter[/eluser]
[quote author="Mat-Moo" date="1256237771"]In the break_after, I was trying to get radiogroups to work, but I can only seem to get radio to work and ot radiobuttons. What and I missing?[/quote]

This will be addressed in the next update!


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

[eluser]macigniter[/eluser]
[quote author="hugle" date="1254830300"]
Imho, the upload path shouldn't be checked... if there is no 'required' ...

[edited]
confirmed, if I do for example:
Code:
$upload = array(
           'upload_path' => '/var/www/vhosts/invista.lt/subdomains/demo/httpdocs/test',
           'max_size'    => '20000',
           'allowed_types' => 'pdf|zip',
           'max_size' => 4000
        );
        
.....
and later:
->upload('file', 'Upload File', false, $upload)

Everything works just fine!
[end of edit]

thanks!Smile[/quote]

Great! You found the solution yourself Wink The library does need a valid upload_path (if your upload is required or not) because in case someone does upload something the path must be set.


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

[eluser]macigniter[/eluser]
[quote author="hugle" date="1258466711"]Hello everyone again,

I have found a little bug in Form library, I think, I'll explain:
I have simple form:
Code:
print_r($_POST);
    
        $this->form->open('/welcome/test')
        ->fieldset('Enter keywords to search for')
        ->text('search', 'Keywords:', '')->indent(150)
        ->submit('Submit')
        ->validate();
        
        if ($this->form->valid)
        {
            $post_data= array();
            $post_data = $this->form->get_post();
            print_r($post_data);
        }
And if I enter '&DEV;_0887&' as a search value(without quotes), and press submit, I get malicious data:
&DEV;_0887& becomes &DEV;_0887&

As you can see I'm not using any rules, but If I try to access $_POST before `if ($this->form->valid)` I have correct value = &DEV;_0887&.
Even $_POST inside if ($this->form->valid) has malicous data = &DEV;_0887&

I could not reproduce the bug... maybe some of you came to this problem?

Thank you![/quote]

I don't see a difference between &DEV;_0887& and &DEV;_0887&
Am I missing something!?!?


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

[eluser]macigniter[/eluser]
[quote author="dinhtrung" date="1258841860"]Form Generation is such a great library! Save a lot of time to write form elements.
[/quote]

Thanks! I love to hear that :-)

[quote author="dinhtrung" date="1258841860"]
But it'll be better if we can adjust it to generate various form structure.
[/quote]

I agree!

[quote author="dinhtrung" date="1258841860"]
So, can we add more config elements to form config file, like:
Code:
$config['label_prefix'] = "<p>";
$config['label_suffix'] = "<br>";
$config['input_prefix'] = "";
$config['input_suffix'] = "</p>";
[/quote]

Looks good! I will try to add it asap...

[quote author="dinhtrung" date="1258841860"]
Also, when I use button() instead of submit(), I can't submit my form. I couldn't understand why. Although output_profiler show that there are POST variables, but the form didn't call the model method. I prefer button with type='submit' rather than just input type='submit' for more theme attributes.[/quote]

This should work... did you find the solution?


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

[eluser]macigniter[/eluser]
[quote author="hugle" date="1255354846"]Hello everyone,
I think I'll start from the BUG maybe, i found .... Ireally hope it isn't
but then I must be doing something really wrongSad

So I'll start.... my controller:
Code:
$this->form->open('profile/post_ad')
->text('interest_rate', 'interest_rate', 'required|test1', '', 'class="text"')
->submit('Submit')
->model('Ads', 'post_ad')
->onsuccess('header', 'Location: /profile/success');

I have also extended the form_validation class, with:
Code:
function test1($str) { return 'test1_string'; }

my model ads.php is:
Code:
function post_ad(&$form, $post_data)
{
          echo '<pre>';
          print_r($post_data);
          echo '</pre>';  
}

the strange thing is ... that output I should get from model should be : 'test1_string'
since test1 rule returns it...

but output I get is always the value I put into form field...
I think something somewehre should be worng or I am doing somthing wrong

I thought maybe the function `test1` wasn't called - but I added echo there, and
it is working

Maybe some of you met similar problems and have some ideas?
I really like this library, it saves like lot of hours of creating labels etcSmile

Hope that helps you to understand my problem, and maybe help me fixing it.. thanksSmile

[edit]
I also wanted to add, that I was doing tests with basic form_validation with the same rules, and the result I got was
'test1_string'
[/edit]

cheers,
huglester[/quote]

I fixed this. Will be included with the next update. I am just now going through all the latest posts here trying to fix all issues and then push out the next release by tomorrow (it's already late here in Germany).

Thanks for your patience!