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 - 12-10-2009

[eluser]antonumia[/eluser]
Maybe a simple question but how do i convert the [recaptcha_challenge_field] so I can compare it with [recaptcha_response_field]?

Do I need to use the recaptcha_check_answer function on their server or can i check it locally?

Any example would be great!

anton


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

[eluser]antonumia[/eluser]
[quote author="antonumia" date="1260479003"]Maybe a simple question but how do i convert the [recaptcha_challenge_field] so I can compare it with [recaptcha_response_field]?

Do I need to use the recaptcha_check_answer function on their server or can i check it locally?

Any example would be great!

anton[/quote]


Duh, add ->validate() to your form constructor


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

[eluser]BaRzO[/eluser]
@mat-moo with model this error comes
Code:
An Error Was Encountered

Form Generation Library
add_error: Element name "fimage" does not exist
with new prerelase and the current relase i have tested and i found my worng :-S
i have used die function for to see what data comes from the form and it was killin all data from the form lib

i have changed the code like this and it's working very well thanks for your help..

Code:
class Test_class extends Controller {

    function Test_class()
    {
        parent::Controller();

        $this->load->library('form');
    }

    function index()
    {
        $this->form->open('test_class')
        ->fieldset('File Upload Test')
        ->upload('avatar', 'Avatar')
        ->indent(150)
        ->submit('Submit')
        ->onsuccess('redirect', 'test_class/success');
        $this->form->validate();

        if ($this->form->valid)
        {
            $post = $this->form->get_post();
            /*
            when i use this file does not uploaded...

            echo "<pre>";
            print_r($post);
            echo "</pre>";
            
            // or this...
            
            die("<pre>".print_r($post)."</pre>");
            */
        }
        $data['form'] = $this->form->get();
        $data['errors'] = $this->form->errors;

        $this->load->view("form", $data);
    }

     function success()
     {
         echo 'ok...';
     }



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

[eluser]dinhtrung[/eluser]
Tonight I played with widget_pi, Modular Separation and Form Generation, and found out the fact that only one the lastest model added is run if the form is valid. Please support multiple calls for methods and postprocess methods. An use case could be like this:
I create a form for admin to add a new user into the system. The user must be assign a profile data in another table, and I don't want to mix the basic info and profile info for an user in only one table. So I create User_profile widgets, to handle profile data and Users controller to handle basic information.
Code:
/* File : modules/users/controllers/users.php */
   ....
   function create()
   {
      $this->form->open()
           ->text('username', "Username")
           ->password('password', "Password");
      widget::run('profile/profilebox');
      $this->form->model('user_m', 'fcreate')
           ->onsuccess('redirect', $this->uri->uri_string());
   }
   ...
/* File : modules/profile/widgets/profilebox.php */
   class Profilebox extends Widget{
      function run(){
          $this->form->text('fullname', "Fullname", 'required|trim|max_length[30]')
               ->textarea('signature', "Signature", 'trim|max_length[255]')
               ->text('phonenumber', "Phone Number", 'trim|exact_length[10]')
               ->model('profile/profile_m', 'addinfo', array('userid' => $this->uri->segment(4, FALSE)));
      }
   }
/* File : modules/profile/models/profile_m.php */
   ...
   function addinfo(&$form, $data){
      if ($data['userid']) $this->update_profile_data($data['userid']);
      else $this->add_profile_data($data);
   }
   ...
Because I don't use English for most of my projects, please consider adding $label = $this->CI->lang->line($label) for Form elements. I usually extends CI native language library to return the original string if the replace string is not found. So, if $lang['form_label'] is undefined, lang('form_label') will return form_label.
Back to the multiple models issue, here is my modification:
Code:
--- ../devel/libraries/Form.php    2009-12-10 16:05:00.319910582 +0700
+++ libraries/Form.php    2009-12-11 02:19:54.910381004 +0700
@@ -853,9 +853,9 @@
     var $error_string; // for internal use only
     var $error_open = '<div>'; // must be provided, will be replaced
     var $error_close = '</div>';
-    var $model;
-    var $model_method;
-    var $model_data;
+    var $model = array();
+    var $model_method = array();
+    var $model_data = array();
     var $valid = FALSE;
     var $validated = FALSE;
     var $recaptcha = FALSE;
@@ -1244,7 +1244,7 @@
         $info = $this->_make_info($atts);
         if ($nameid) $this->_make_nameid($nameid, $info);
         $this->_check_name($info['name']);
-        $info['content'] = $this->CI->lang->line($content);
+        $info['content'] = $content;
         $info['type'] = 'button';
         $info['kind'] = $kind;
         $this->add($info);
@@ -1756,12 +1756,17 @@
      */
     function _load_model()
     {
-        if ($this->_posted && !$this->error_string && $this->model) {
+        if ($this->_posted && !$this->error_string && count($this->model)) {            
+            foreach ($this->model as $index => $model){
-             if ($this->_data) $this->model_data['uploads'] = $this->_data;
+             if ($this->_data) $this->model_data[$index]['uploads'] = $this->_data;
+                $m = "mod$index";
             $this->CI->load
-                        ->model($this->model, 'mod');
-            $this->CI->mod
-                        ->{$this->model_method}($this, $this->model_data);
+                        ->model($model, $m);
+                $this->CI->$m
+                        ->{$this->model_method[$index]}($this, $this->model_data[$index]);
+            }
         }
     }
     /**
@@ -2100,9 +2105,9 @@
         if (!$model) show_error("model: No model specified");
         $data = $this->_make_array($data);
         $data = array_merge($data, $this->get_post()); // post data validated at this time, combine data provided with POST data
-        $this->model = $model;
-        $this->model_method = $method;
-        $this->model_data = $data;
+        $this->model[] = $model;
+        $this->model_method[] = $method;
+        $this->model_data[] = $data;
         return $this;
     }
     /**



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

[eluser]happydude[/eluser]
I three questions:

1. What if I want the user to create form elements using javascript on the client side. (Something like "click here if you want to enter additional settings).

2. How do I integrate this library with datamapper. Can you please show an example?

3. How do I upload multiple images?


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

[eluser]dinhtrung[/eluser]
@happydude: 1 . I once create a dynamic data field like you said. Here is the code:
Code:
/* File : controllers/mycontroller.php */
$data['form'] =  $this->form->open()->fieldset("Dynamic Data")                      
                      ->html("<div id='source-elem'>")
                         ->text('mytextbox[]', 'My Text Box')
                         ->button('<em>Click Here</em>', '', 'button', 'class='add-elem')
                      ->html("</div>\n<div id='extra-elem'></div>")->model('mymodel_m', 'formprocess')->get();
$data['errors'] = $this->form->errors();
$this->load->view('form', $data);
Then in the view file, you fill in your form javascript.
Code:
/* File : views/form.php */
&lt;?php if (! empty($errors)) print $errors;
      if (! empty($form)) print $form;
?&gt;
[removed]
$(document).ready(function(){
   var form_element = $("#source-elem").html();
   $(".add-elem").live('click', function(){
      $("#extra-elem").append(form_element);
   });
});
[removed]
2 . Never heard of datamapper before. But if it's just a library, then I think there won't be any problem. You can pass your form data into a model, then call the library from this model and do as you like.
3 . Not try the upload feature of Formgen once. Will give it a try later.


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

[eluser]happydude[/eluser]
Thanks for your help man. That was greatly appreciated.

Datamapper is an ORM library for CodeIgniter... probably the best, since there are many spawns of it now.

The upload feature of formgen is really cool. It even has a special function iupload() for image upload. Give it a shot. You'll like it.

BTW, the split() function you used breaks in PHP 5.3. I just get deprecated messages all over. This PHP 5.3 is something else. Its just breaking things all over the world. I'm gonna change it to explode() and see if it works.


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

[eluser]happydude[/eluser]
Its not working at all now.


I am running CI 1.7.2 on P|HP 5.3 and all I get now is a white screen of death.



Any ides on what might be wrong?


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

[eluser]dinhtrung[/eluser]
I gave it a test lastnight. Well, you can't use multiple upload dynamically or use uploadify with FormGen. Sorry about this.
It is because every element in Formgen is automatically assigned an unique ID to check for various thing, like which is the last element user accessed, whether the form is submit or not.
For upload element, the upload data is assigned back into $data that send to your model by the field name. Say, if you do:
Code:
$this->form->upload('userfile', "Upload, Please", 'allowed_types=pdf|zip,upload_path=/tmp/,max_size=4000');
Then if the user upload the file, you get something like this:
Code:
$data['userfile'] = array('full_path' => '/tmp/yourfilename.zip', 'file_name' => 'yourfilename', 'file_ext' => 'zip'...)
So, if you use the method in my previous post, you use 'userfile[]' instead of 'userfile', you get error message : this element is already exists. Even if you change userfile[] to userfile_1, userfile_2... it won't work, because those extra fields is not 'registered' with Formgen.
I'll try to extend this feature as well. I'm in the need of it, too. Smile


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

[eluser]CtheB[/eluser]
Hi, i'm just starting with this form library.
Must commit you've really put some effort into this, well done.

I will give some comments here in this topic if i have some ideas or questions about the library.

When i just start with an example, with only a name and password field, i'll get this exception:

Code:
Caught Exception
...../******/libraries/Exceptions.php on line 34 ,
code: 9
Message: ...../******/libraries/Form.php Undefined variable: action on line 1521

The exception is for this line of code:
Code:
function _get_form_open()
    {
1519    $this->atts['method'] = $this->method;
1520    if ($this->multipart) return form_open_multipart($this->action, $this->atts);
1521    return form_open($action, $this->atts);
    }

When i replace $action with $this->action, everything works oke.

I'm using the 0.22 pre release version.