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 - 08-17-2009

[eluser]12vunion[/eluser]
Sorry if this has been covered, but I'm not seeing it in the documentation. How do you get back the submitted data after the form passes validation? Is it returned somehow or are you limited to grabbing the post data? Not that it would be life ruining to grab all the post data, but it would be handy to have clean, validated data array that I can just pass back to my models.


Form Generation Library - El Forum - 08-17-2009

[eluser]pysco68[/eluser]
It's rather simple, you can access the data in the usual way (CI-Syntax), just make sure the the validation has allready been passed:

Example:
Code:
if ($form->valid)
{
    echo  $this->input->post('your_field');
}

Good night Wink


Form Generation Library - El Forum - 08-17-2009

[eluser]macigniter[/eluser]
@12vunion: it is not returned if you use the get() method. so after the form has been validated it is safe to just grab the data from the post values with $this->input->post('name')


Form Generation Library - El Forum - 08-17-2009

[eluser]12vunion[/eluser]
Yep, that's what I've been doing. Just wondering if there was another, more automated way (since it already knows all about my form). I just have a lot of fields and am trying to be lazy.

I was wishing maybe for something like this:

Code:
if($this->form->valid){
            
            $form_data = $this->form->get_post();

    //what it returns            
    $form_data == array(
                'first_name' => 'Billy Bob',
                'last_name' => 'Jim Bob',
                ...
            );



Form Generation Library - El Forum - 08-19-2009

[eluser]12vunion[/eluser]
I'm new to the form generator so I don't know every single thing to account for, but this is basically working. Please test it more thoroughly than I have and I'd love to see it worked into the next version. Thanks for the cool library. Smile

Here, put this in your Form to get this running (I put it on line 1390).
Code:
/**
     * Get Post
     *
     * Returns the submitted form post values as array
     */
    function get_post()
    {
        $post_values = array();
        foreach ($this->_elements as $el)
        {
            if ($el['name']){
                $name = str_replace("[]", "", $el['name']);
                $value = $this->CI->input->post($name, true);
                //fix for the annoying way selects, checkboxes and radios work
                if( is_array($value) && count($value) == 1){
                    $value = $value[0];
                }
                
                $post_values[ $name ] = $value;
            }
            
        }
        
        return $post_values;
    }



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

[eluser]seanloving[/eluser]
Hi can someone please help me understand how to use the model method, conditionally upon validation???

As you can see, the model and onsuccess methods are not implemented.

Code:
$this->load->library('form');        
        $this->form->open('login')
            ->fieldset('Enter Your Login Credentials')
            ->text('username', 'User Name', 'required|trim|max_length[40]|callback_username_check')
            ->password('password', 'Password', 'required|trim|max_length[40]|callback_password_check')    
                ->indent(150)
            ->submit('Submit', 'sub')
            ->reset('Reset', 'reset', "onclick=document.location.href='login'")
            ->margin(10)
            ->validate();
            //->model('users', 'checklogin', array($username,$password))
            //->onsuccess('redirect', 'member');

So this next part of code accomplished what I need but...

a. it's ugly
b. it does not use the Form Library methods for model and onsuccess.
c. seems more procedural than OO

Code:
if($this->form->valid)
{
            // the submitted form data was valid
            // now check to see if there is a record in the users table with matching username and password.
            $this->load->model('users');            
            switch( $this->users->checklogin( $username, $password) )
            {
                case '2':
                    // login success --> display the member page for the logged in user                
                case '1':
                    // bad password --> redisplay the login form with a bad password message                
                default:
                case '0':
                    // no such user --> redisplay the login form with a bad username message
            } // end switch
}
else
{
            // the password or username was not valid
}
Could someone please suggest a better way? Perhaps more in the style or tradition of this Form Generation Library?

Thanks

--Sean Loving


Form Generation Library - El Forum - 08-23-2009

[eluser]macigniter[/eluser]
[quote author="12vunion" date="1250724175"]I'm new to the form generator so I don't know every single thing to account for, but this is basically working. Please test it more thoroughly than I have and I'd love to see it worked into the next version. Thanks for the cool library. Smile[/quote]

Very good suggestion! I will add it to the next version!


Form Generation Library - El Forum - 08-23-2009

[eluser]macigniter[/eluser]
[quote author="seanloving" date="1251013151"]Hi can someone please help me understand how to use the model method, conditionally upon validation???[/quote]

This is how you would handle this with the ->model() method:

Controller

Code:
$this->load->library('form');        
$this->form->open('login')
->fieldset('Enter Your Login Credentials')
->text('username', 'User Name', 'required|trim|max_length[40]|callback_username_check')
->password('password', 'Password', 'required|trim|max_length[40]|callback_password_check')    
->indent(150)
->submit('Submit', 'sub')
->reset('Reset', 'reset', "onclick=document.location.href='login'")
->margin(10)
->model('users', 'checklogin')
->onsuccess('redirect', 'member');

$data['form'] = $this->form->get(); // this will validate the form automatically and will only call the model upon succesful validation

Model

Code:
class Users extends Model {

    function Users()
    {
        parent::Model();
    }
    
    function checklogin(&$form, $data)
    {
        // do the login check with
        // $data['username'] and
        // $data['password']

        // check and then output the result code in $outcome

        // then decide what to do:
        switch ($outcome)
        {
            case '2':
            // login success
            // do nothing and let onsuccess() handle handle the redirect to the member page                

            case '1':
            // bad password
            $form->add_error('password', 'You entered a bad password.');
              
            default:
            case '0':
            // no such user
            $form->add_error('username', 'No such user.');
        }
    }
}

Hope that helps!

(I guess this would be a good example to go in the user guide ;-))


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

[eluser]seanloving[/eluser]
Thanks. That worked. In case you want to use this example in the user guide, the model simplified to this :

Model
Code:
function checklogin(&$form, $data)
{
    $this->db->where('username', $data['username']);
    $this->db->select('password');
    $query = $this->db->get('users');
    if ($query->num_rows() == 0)
        $form->add_error('username', 'You entered a bad username.');
    else if ($data['password'] != $query->row()->password)
        $form->add_error('password', 'You entered a bad password.');
}

SL


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

[eluser]Jeffrey Lasut[/eluser]
Quote:Hope that helps!

(I guess this would be a good example to go in the user guide ;-))


First of all, thank you for the great library!

I'm testing your library and currently you can only call a model and model method, is there a way to call a library and his method's?

I tried to refractor your _load_model() method to a _load_library() but somehow the method of the library will not load.

Any suggestions?

Jeffrey


Code:
var $library;
    var $library_method;
    var $library_data;

function get()
    {
        $this->_output='';
        $this->_close_tags();
        if (!$this->validated) $this->validate();
        $this->_upload_files();
        $this->_load_model();
        $this->_load_library();
        if ($this->error_string) $this->errors = $this->config['error_string_open'].$this->error_string.$this->config['error_string_close'];
        $this->_do_onsuccess();
        $this->_elements_to_string();
        $form = $this->_get_form_open();
        $form .= $this->_output;
        $form .= form_close();
        return $form;
    }

    function get_array()
    {
        $this->_output='';
        $this->_close_tags();
        if (!$this->validated) $this->validate();
        $this->_upload_files();
        $this->_load_model();
        $this->_load_library();
        $this->_do_onsuccess();
        $form = array();
        $form = $this->_elements_to_string(TRUE);
        foreach ($this->_aliases as $old=>$new)
        {
            $form[$new] = $form[$old];
            $form[$new.'_error'] = $form[$old.'_error'];
            unset($form[$old]);
            unset($form[$old.'_error']);
        }
        $form['form_open'] = $this->_get_form_open();
        $form['form_close'] = form_close();
        $form['action'] = $this->action;
        $form['method'] = $this->method;
        return $form;
    }

function _load_library() {
        if ($this->_posted && !$this->error_string && $this->library)
        {
            if ($this->_data) $this->library_data['uploads'] = $this->_data;

            $this->CI->load->library($this->library, 'lib');
            $this->CI->lib->{$this->library_method}($this, $this->library_data);
        }
    }

function library($library='', $method='index', $data=array())
    {
        if (!$library) show_error("library: No library specified");
        $data = $this->_make_array($data);
        $data = array_merge($data, $_POST); // post data validated at this time, combine data provided with POST data
        $this->library = $library;
        $this->library_method = $method;
        $this->library_data = $data;
        return $this;
    }
[/quote]