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 - 03-23-2011

[eluser]macigniter[/eluser]
[quote author="JonoB" date="1300918844"]I am in the process of trying to integrate model validations with the FormGen library. In essence, I am trying to remove all validation from the controller, and place all of them in the model.

I have this working fine, but I would like the actual error messages to be handled by FormGen - so the outputting/formatting of the error messages is handled automatically.

It means that in some way I have to populate any validation failures into FormGen, but I have no idea on how to do this.

Is this even possible?[/quote]

http://frankmichel.de/formgenlib/user_guide/methods/model.html


Form Generation Library - El Forum - 03-23-2011

[eluser]JonoB[/eluser]
Thanks Mac...I was almost there, but was missing the onsuccess part. Awesome.

Now, the last part is to autopopulate the errors from the validation, because I dont want to have to manually identify the errors that were generated.

UPDATE:
The core Form_validation has a private var called _errors_array that I needed to get so that I could auto-populate the errors with the default CI errors. So, instead of handcoding the FormGen errors (as follows), I want this to all happen automagically.
Code:
$form->add_error('field_name', 'My error message');

First step was to extend the Form_validation class, so we can get hold of the _errors_array

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* MY Form Validation Class that extends the core Form Validation Class
*
* @category    Validation
* @author        JonoB
*/
class MY_Form_validation extends CI_Form_validation
{

    function __construct()
    {
        parent::__construct();
    }

    /**
     * This method exposes the private _error_array from the
     * core Form_validation library
     * @return <type>
     */
    function get_errors_array()
    {
        return $this->_error_array;
    }

}

// END Form Validation Class

/* End of file MY_Form_validation.php */
/* Location: ./application/libraries/MY_Form_validation.php */

Then, in my Model, I do something like:

Code:
//try insert the data
$valid = $this->update($id, $data);

if ($valid)
{
    //validation has passed and update query has run successfully
    //do something else if you want
}
else
{
    //validation has failed
    //grab all the errors from MY_Form_validation as an array
    //and send them back to FormGen so that they can be displayed in the form
    $error_array = $this->form_validation->get_errors_array();
    foreach ($error_array as $field => $error)
    {
        $form->add_error($field, $error);
    }
}

Thanks again for the awesome lib!


Form Generation Library - El Forum - 03-23-2011

[eluser]JonoB[/eluser]
The one part that is missing: some validations need to be in the controller in order for apperance of the form fields to be correctly affected. The most obvious ones are 'required' (changes the label to bold and adds a *) and 'max_length' (prevents users from typing too many chars).

If these validations are only in the model, then the actual validation works properly, but they will not affect the appearance of the fields pre-submission.

(Apologies for the rambling; hopefully someone finds this exercise useful).


Form Generation Library - El Forum - 03-23-2011

[eluser]Boris Strahija[/eluser]
Do you plan to add support for HTML5 field types, an option to end the tags with ">" and not " />"?


Form Generation Library - El Forum - 03-28-2011

[eluser]negantre[/eluser]
Newbie here. I'm noticing when a submitted form fails validation, and is redisplayed, that previously selected radio and check groups are forgotten when the form is reloaded. I noticed that if the first check box in a check group was selected, it will be preserved on re-load, but not if the second or later check box was selected.

Does this make sense? Has anyone else seen this behavior? And if so, how do I rectify?

I'm using version v1.04.

Here's some sample code used for a radiogroup:

Code:
$radios_contactmethod[] = array('inperson|inperson', 'In Person');
$radios_contactmethod[] = array('telephone|telephone', 'Via Telephone');

$this->form->radiogroup('contactmethod', $radios_contactmethod, 'Contact method:');

.....
.....

$data['form'] = $this->form->get();
$data['errors'] = $this->form->errors;
$this->load->view('formname', $data);
If "Via Telephone" is selected and the form is submitted, and fails validation, it's no longer selected on re-load.


Thanks in advance!


Form Generation Library - El Forum - 03-28-2011

[eluser]Icehawg[/eluser]
[quote author="negantre" date="1301357504"]Newbie here. I'm noticing when a submitted form fails validation, and is redisplayed, that previously selected radio and check groups are forgotten when the form is reloaded. I noticed that if the first check box in a check group was selected, it will be preserved on re-load, but not if the second or later check box was selected.

Does this make sense? Has anyone else seen this behavior? And if so, how do I rectify?

I'm using version v1.04.

Here's some sample code used for a radiogroup:

Code:
$radios_contactmethod[] = array('inperson|inperson', 'In Person');
$radios_contactmethod[] = array('telephone|telephone', 'Via Telephone');

$this->form->radiogroup('contactmethod', $radios_contactmethod, 'Contact method:');

.....
.....

$data['form'] = $this->form->get();
$data['errors'] = $this->form->errors;
$this->load->view('formname', $data);
If "Via Telephone" is selected and the form is submitted, and fails validation, it's no longer selected on re-load.


Thanks in advance![/quote]

I also lost the ability to set a selected value to a radiogroup when the "in_array" check was fixed above. I can't even pass in values(string or array) any longer. Worked on the code for about an hour, and did a bunch of logging, and it seems to know the values but won't set it. So I have given up for the time being and waited to see if anyone else finds the fix. If not, I will give it a whirl again when I have some time.


Form Generation Library - El Forum - 03-29-2011

[eluser]Boris Strahija[/eluser]
You should really put the library on Github or Bitbucket so people can send you pull reqeusts Wink


Form Generation Library - El Forum - 03-29-2011

[eluser]Icehawg[/eluser]
Ya, Bitbucket would be good.


Form Generation Library - El Forum - 03-29-2011

[eluser]Infinitation[/eluser]
First, this is simply awesome! Just started really getting into this library. I do have a question about models though and how they are interacting with the form lib. For example, with a forgotten password field that checks to see if the users email exists, I would like to send back the users name if it is valid. I've been following this in your amazing help guide, but haven't quite made the connection on how to return a result back to the controller for use in sending an email.

http://frankmichel.de/formgenlib/user_guide/methods/model.html

Help from anyone would be awesome! Current model is like this:
Code:
function checkemail(&$form, $data)
    {
        $email = $data['email'];
        
        $this->db->where('userEmail', $email);
        $this->db->select('userFirst, userLast, userEmail');
        
        $query = $this->db->get('tblUser');
        
        if ($query->num_rows() == 0)
        {
            $form->add_error('email', 'That email address could not be found');
        }
        else
        {
            //Want to send back the result of the query to be used in the controller
        }

    }


The controller is very simple:
Code:
function forgotpassword()
    {
        $this->form
        ->open('login/forgotpassword', 'forgotForm|forgotForm')
        ->text('email', 'Your Email', 'trim|required|valid_email')
        ->submit($this->lang->line('login_send'))
        ->model('login_model', 'checkemail');
        
        $data['form'] = $this->form->get();
        
        if ($this->form->valid)
        {
                       //Would like to send an email in the controller rather than the model, should go right here if I can get the data back from the model.
            redirect('login/success');
        }
        else
        {
            $data['errors'] = $this->form->errors;
        }
        
        $this->load->view('login/login_forgot_password', $data);
    }

The easy way around this is to call another function from the model, but is that really necessary?

Thanks to anyone that can help.


Form Generation Library - El Forum - 03-29-2011

[eluser]Icehawg[/eluser]
There is a nice way to extend the form validation to make a "must be unique in database.table" tutorial at this site :

http://net.tutsplus.com/tutorials/php/6-codeigniter-hacks-for-the-masters/

With #3 you can specify, for example, the following instead in your code

Code:
->text('email', 'Your Email', 'trim|required|valid_email||unique[User.email]')

where "User" is the name of your table with registration information and email is the name of the field to verify against. Then when someone tries to register with a duplicate email address, the form validation rule will return FALSE and flag it as an error.