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 - 05-19-2009

[eluser]macigniter[/eluser]
[quote author="got 2 doodle" date="1242241400"]Great stuff here, just working through it to see it it's appropriate for a project

small bug here
Line 994 change $name to $nameid
Code:
function iupload($nameid, $label='', $required=FALSE, $atts=array())
    {
        $info = $this->_make_info($atts);
        if (!@$info['allowed_types']) $info['allowed_types'] = 'jpg|png|gif';
        $this->upload($nameid, $label, $required, $info);
        return $this;
    }

Having a problem with the upload function

A small error in the docs
Code:
$form->upload('file', 'Upload File', 'allowed_types=pdf|zip, max_size=4000')

should read
Code:
$this->form->upload('file', 'Upload File','','allowed_types=pdf|zip, max_size=4000');

note third passed parameter is missing in docs

Very very cool! can't wait to see the docs finished.

doodle[/quote]

Thanks! I fixed it and will update the source files immediately.


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

[eluser]macigniter[/eluser]
[quote author="got 2 doodle" date="1242334666"]Um..
a little more playing around and I think this lib is going to be really useful.

I uploaded a file with an ->upload control and it went to my defined folder all is well

but umm.. what happend to the $_POST data??

How can I retrieve the uploaded file name etc.?

I see about one hundred functions in the library, now I know why the documentation is unfinished.

I need a hint.. please

doodle[/quote]

If you use the ->onsuccess() function your $_POST data will not be submitted with the redirect. All the validating of the $_POST data happens in your controller and if the form is actually valid it will do the redirect.

As I said there's still much to do about this library and some things might not be thought through very well ;-)

BUT, I would suggest not to use the ->onsuccess() function for now and rather do something like this:

Code:
/****************************    restrict   **************************************/    

    function upload_data()

/*******************************************************************************/    

    {
        $this->load->library('form'); // first of all we have to load the library

        $form = new Form();
        $_view = 'upload';

        $form
        ->open('/res/upload_data')
        ->text('text1|text1','Text')
        ->upload('file', 'Upload File','','allowed_types=csv, max_size=8000')
        ->submit()
        ->validate(); // validate the form before we actually get it

        if ($form->valid)
        {
           // this is where you can access all the $_POST data
           // so do some stuff with your data and then redirect

           redirect('/res/inventory');
        }

        // if the form isn't valid the code below will be executed
        // since the redirect didn't happen

        // the next line just grabs some data and a menu (array) based on a named 'page'
        // $this->prefix = 'res'

        $data = $this->get_pagedata($this->prefix.'_contact',$this->prefix.'_contact');

        $data['form'] = $form->get(); // this returns the validated form as a string
        $data['errors'] = $form->errors;  // this returns validation errors as a string

        // the next line loads a collection of view segments
        // ie. header + content + footer
        // content would be replaced with the value of $_view

        $this->load_views($data,$_view);
    }



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

[eluser]tdktank59[/eluser]
Looks good!

Going to be using it for our project.


Form Generation Library - El Forum - 05-28-2009

[eluser]macigniter[/eluser]
Hi Everyone!

I just released a minor update of the library. From now on you can always download the latest version at the following location of the user guide:

Download Form Generation Library

...and review all changes in the changelog.

The latest release now includes all the submitted form data in the $data array of the model() function. This means that you can now use the validated form data to do some database queries in your model before the onsuccess function is called. You can also still throw custom errors by using $form->add_error('Error flagged in model') within your model function.

Have fun with the library and stay tuned for a full user guide coming very soon (I am currently working hard on it).

Please don't hesitate to contact me with feedback or improvements!


Form Generation Library - El Forum - 05-28-2009

[eluser]macigniter[/eluser]
[quote author="abmcr" date="1240854445"]I have not understand how this lines
Code:
->model('example', 'do_db_stuff')

        // do a redirect
        ->onsuccess('redirect', 'welcome/success');
        
        /*
         * write the validated form to the $data array
         *
         * NOTE: instead of getting the entire form at
         * once you can also do something like this:
         *
         * $form->validate();
         *
         * if ($form->valid)
         * {
         *        do something
         * }
         *
         * $data['form'] = $form->get();
         */
        $data['form'] = $form->get()

In the model i have not the data_array but only the data of the uploaded files?

The code of model is
Code:
function do_db_stuff(&$form, $data)
    {
        // do db stuff after the form was submit and validated
        // $data will contain $data['uploads'] with all the upload data
        var_dump($data);
        var_dump($form);
        die();
    }
and in the $form i get all information aboit form and not only the data submitted[/quote]

In the newest release of the library you will now have ALL the form data available in the $data array as well as the data you have provided with the model() function. More on the usage of this function will be provided in the user guide very soon!


Form Generation Library - El Forum - 05-28-2009

[eluser]abmcr[/eluser]
Thank you: i try as soon possible.... Ciao


Form Generation Library - El Forum - 06-05-2009

[eluser]got 2 doodle[/eluser]
@macigniter
Thanks for your help and your library is <strong> Frikkin Awesome!</strong>

I love it! :coolsmile:

I'll be working with this over the next few days, I have a small project that this will be perfect for.

doodle


Form Generation Library - El Forum - 06-09-2009

[eluser]Paul T[/eluser]
First of all, thank you for this great time saver! It has already saved me numerous headaches.

I have a feature request: Please add an easy way to include a reCAPTCHA. I would try it myself but I'd just end up mangling the code. Or just point me in the right direction so I know what to mangle?

Thanks!
Paul


Form Generation Library - El Forum - 06-12-2009

[eluser]Robert May[/eluser]
Finally gotten around to finishing my conversion of my auth system to this library. It works well, though I have no idea how to make a field required!
Here's the code for my controller, library, and model so you can get a gist of a working system using it. Feel free to point out any holes or modifications you'd make.

The controller
Code:
&lt;?php

class Auth extends Controller {

    function Auth()
    {
        parent::Controller();    
        $this->load->helper('url');
    }
    
    function index()
    {
        $this->session->keep_flashdata('refer');
        $logindetails = $this->session->flashdata('logindetails');
        $this->load->library('form'); // first of all we have to load the library
        $form = new Form(); // then we instantiate a new form object
        
        $form // and fill it with elements
        ->open('auth')
        ->text('author_name', 'Username:', 'trim|alpha_numeric|max_length[255]|xss_clean', $logindetails['author_name'])
        ->password('author_password', 'Password:', 'trim|alpha_numeric|max_length[255]|xss_clean', $logindetails['author_password'])
        ->indent(290)
        ->submit();
        
        $form->validate();
        
        if($form->valid){
            $logindetails = array(
                                    'author_name' => $this->input->post('author_name'),
                                    'author_password' => $this->input->post('author_password')
                                );
            $this->session->set_flashdata('logindetails', $logindetails);
            redirect('auth/login');
        }
        
        $data['form'] = $form->get(); // this returns the validated form as a string
        $data['errors'] = $form->errors;  // this returns validation errors as a string
        
        $content    = $this->load->view('auth', $data, TRUE);
        
        $data        = array(
                            'page_title'    =>    'Login',
                            'sub_heading'    =>    'Please login to continue',
                            'content'        =>    $content,
                        );
        $this->templater->build($data);
    }
    
    function login()
    {
            $this->session->keep_flashdata('refer');
            $this->session->keep_flashdata('logindetails');
            
            $query = $this->authlogin->login($this->session->flashdata('logindetails'));
            if($query == FALSE || $query == NULL){
                $this->session->keep_flashdata('refer');
                $this->session->keep_flashdata('logindetails');
                $error =    "Username/email or password incorrect. <br />
                             <a href='".site_url()."/auth' alt='Click here to try again'>Please click here to try again.</a>";
                show_error($error);    
                exit();
            }
            else
            {
                foreach($query as $data)
                {
                    $this->authlogin->set($data['author_name'], $data['author_email'], NULL);
                    $referto = $this->session->flashdata('refer');
                    // Forward to secured area
                    echo "Success!<br />";
                    echo "Thank you for signing in, ".$this->session->userdata('name').". You will now be redirected.";
                    header('Location: '.site_url().''.$referto);
                            
                }
            }
    }
    
    function logout()
    {
        $this->authlogin->logout();
        $data = array(
                        'title'        => 'You have been logged out.',
                        'heading'    => 'Logout successful.',
                        'message'   => 'You have been successfully logged out, have a nice day!'
                     );
        $this->load->view('message', $data);
    }
}

/* End of file auth.php */
/* Location: ./system/application/controllers/auth.php */



Form Generation Library - El Forum - 06-12-2009

[eluser]Robert May[/eluser]
The library
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class AuthLogin {

    var $author_id        = '';
    var $author_name      = '';
    var $author_email      = '';
    var $author_dob          = '';
    var $author_password  = '';
    var $author_lastlogin = '';
    var $author_key       = '';
    var $author_group      = '';
    
    
    function Login($logindetails)
    {
        $CI =& get_instance();
        $CI->load->library('form_validation');
        $CI->load->model('auth_model');
        $query = $CI->auth_model->login($logindetails);
        return $query;
    }
    
    function Logout()
    {
        $CI =& get_instance();
        $data = array(
                        'name'        => '',
                        'email'        => '',
                        'logged_in' => ''
                     );
        $CI->session->unset_userdata($data);
    }

    
    function Set($name, $email, $duration)
    {
        $CI =& get_instance();
        $data = array(
                        'name'        => $name,
                        'email'        => $email,
                        'logged_in' => TRUE
                     );
                    
           if(!($CI->session->set_userdata($data)))
        {
            $success = FALSE;
        }
        else
        {
            $success = TRUE;
        }
        
        return $success;
    }
    
    function Require_access($group=null)
    {
        $CI =& get_instance();
        $CI->load->model('auth_model');
        $CI->load->library('encrypt');
        $logged_in    = $CI->session->userdata('logged_in');
        if($logged_in == FALSE)
        {
            $CI->load->helper('url');
            $refer = uri_string();
            $CI->session->set_flashdata('refer', $refer);
            header('Location: '.site_url().'/auth/');
            exit();
        }
        elseif($logged_in == TRUE)
        {
            // Renew the session
            $name        = $CI->session->userdata('name');
            $email        = $CI->session->userdata('email');
            $data = array(
                        'name'        => $name,
                        'email'        => $email,
                        'logged_in' => TRUE
                     );
            // Check that user exists first. Safeguard against session changes.
            $query         = $CI->auth_model->level_check($name);
            if($query == NULL){
                $error = "You are not authorised to view this page.";
                show_error($error);
                die();
            }
            // Set the session again
            $CI->session->set_userdata($data);
            // If a usergroup is required, run a check to ensure user is in that group
            if($group != NULL)
            {
                $name  = $CI->session->userdata('name');
                $query = $CI->auth_model->level_check($name);
                
                if($query != NULL)
                {
                    // If the user is not in the group
                    if($query[0]['group_name'] != $group)
                    {
                        $error = "You are not authorised to view this page.";
                        show_error($error);
                        exit();
                    }
                    else {
                        // The group check was successful, allow access. Extra code can go here.
                    }
                }
                else
                {
                    // The user doesn't exist, disallow access. Safeguard against session changes.
                    $error = "You are not authorised to view this page.";
                    show_error($error);
                    exit();
                }
            }
            else{
                // No group check neccessary, user is logged in. Extra code can go here.
            }
        }
    }
}
/* End of file AuthLogin.php */
/* Location: ./system/application/libraries/AuthLogin.php */