Welcome Guest, Not a member yet? Register   Sign In
how can we generate multiple text inputs with same name using formgenlib ?
#1

[eluser]Bhupal Sapkota[/eluser]
i am using macigniter's formgenlib, http://ellislab.com/forums/viewthread/107861/
sorry if i am repeating the question, couldn't find answer searching throughout the forum.

i need to generate multiple text inputs with same name like this,

Code:
<input type="text" name="ideas[]" value=""/>
<input type="text" name="ideas[]" value=""/>
<input type="text" name="ideas[]" value=""/>

as far as i googled this code is perfectly valid but what's the case with formgenlib? it shows error.

Quote:An Error Was Encountered
Form Generation Library
Element name "ideas[]" already exists.

i am using following code to populate the form with database values:

Code:
foreach ($ideas as $idea){
$this->form            
->text('ideas[]', '', 'max_length[40]', $idea['text']);    
}


"$ideas" have few array elements, print_r shows it. but formgenlib is giving me troubles.
also i need wrapper <div> around the inputs if that is possible with formgenlib.

Code:
<div id="ideas-wrapper">
&lt;input type="text" name="ideas[]" value=""/&gt;
</div>
please help.
#2

[eluser]Cristian Gilè[/eluser]
Quote:i need to generate multiple text inputs with same name like this,
&lt;input type="text" name="ideas[]" value=""/&gt;
&lt;input type="text" name="ideas[]" value=""/&gt;
&lt;input type="text" name="ideas[]" value=""/&gt;

as far as i googled this code is perfectly valid but what’s the case with formgenlib? it shows error.

Quote: An Error Was Encountered
Form Generation Library
Element name “ideas[]” already exists.

You should hack the formgenlib library to achieve what you want. It doesn't allow to have two form elements with the same name.

Quote:also i need wrapper <div> around the inputs if that is possible with formgenlib.
<div id="ideas-wrapper">
&lt;input type="text" name="ideas[]" value=""/&gt;
</div>

Look inside the formgenlib config file for prefix and suffix or use the html method as follow:

Code:
$this->form
->html('<div id="ideas-wrapper">')
->text(.....)
->html('</div>')


Cristian Gilè
#3

[eluser]Bhupal Sapkota[/eluser]
thanks for the reply, wrapper shows now. great!

Quote:You should hack the formgenlib library to achieve what you want. It doesn’t allow to have two form elements with the same name.

in Form.php, on function _check_name() i commented the middle line:

Code:
function _check_name($name, $type='') {
if ($type && !$name) show_error(FGL_ERR.'No name specified for '.$type.' element.');
//if (in_array($name, $this->_names)) show_error(FGL_ERR.'Element name "'.$name.'" already exists.');
if ($name) $this->_names[] = $name;
}

the error "Element name ideas[] already exists." is gone now. but now i am getting same values in both of the input elements.

let me explain,

$ideas have few values:

Quote:Array
(
[0] => Array
(
[id] => 8
[text] => idea1
)

[1] => Array
(
[id] => 9
[text] => idea2
)

)

but the loop:

Code:
foreach ($ideas as $idea){
$this->form            
->text('ideas[]', '', 'max_length[40]', $idea['text']);    
}


gives me two text inputs, both with the value of last array item
Code:
&lt;input type="text" maxlength="40" value="idea2" name="ideas[]"&gt;
&lt;input type="text" maxlength="40" value="idea2" name="ideas[]"&gt;

i tried,

Code:
foreach ($ideas as $idea){
$this->form  
->html($idea['text'])          
->text('ideas[]', '', 'max_length[40]', $idea['text']);    
}

Quote:html($idea['text']) is giving correct values,
however the $idea['text'] in
->text('ideas[]', '', 'max_length[40]', $idea['text']);
produces two inputs with same value of last $ideas array element.

am i missing something?
do i need to modify formgenlib in other places too?
please help.
#4

[eluser]Cristian Gilè[/eluser]
It's not so simple but not impossible. You have to hack more in depth. The text value is always the same because the foreach updates the same input. FGL extract the input by name from its internal list so the first occurence is updated.

I hope I was clear.


Cristian Gilè
#5

[eluser]Bhupal Sapkota[/eluser]
@cristian

this is my first hand experience with forgenlib, sorry couldn't get the clue.

one hour went by, and i am now left starring at the code.
the whole thread seems satisfied with this library.
can you please list the changes that i need to make in order to accomplish this?

would really appreciate your help.
#6

[eluser]Cristian Gilè[/eluser]
[quote author="Bhupal Sapkota" date="1295311256"]@cristian

this is my first hand experience with forgenlib, sorry couldn't get the clue.

one hour went by, and i am now left starring at the code.
the whole thread seems satisfied with this library.
can you please list the changes that i need to make in order to accomplish this?

would really appreciate your help.[/quote]

I have no time to dig deeper into the formgenlib code but I think we could find a simpler solution.

Why do you need to generate multiple text inputs with same name? Please, post some code if needed.


Cristian Gilè
#7

[eluser]Bhupal Sapkota[/eluser]
@cristian,

Quote:Why do you need to generate multiple text inputs with same name? Please, post some code if needed

i am using multiple text inputs with same name because these input represent one entity, like i will have "interest" section in the form and i would take all user inputs "interest1, interest2, interest3..." in "interest[]" text element,

also text input elements can be added/removed dynamically in the frontend, user can add as many as they want. using same name, the frontend part would be easier to handle, further we can receive all input data in an array in backend. is there any simpler approach to handle this case?

using unique name for each element would put you in hassle to maintain the "uniqueness" of the element name in frontend javascript and backend form generation code too. i tried it. i wonder why formgenlib ignored this.

this needs to be done asap. if you have fixed the multiple same name input issue in formgenlib, i would really be grateful if you could share the code.

thanks.
#8

[eluser]macigniter[/eluser]
[quote author="Bhupal Sapkota" date="1295344566"]
this needs to be done asap.[/quote]

Seriously...? Do you know how it feels giving so much to the community with this - quote - great lib that helps tons of people out there increase production time dramatically reading something like this? yes, you've found a bug (or let's call it a missing feature since no one ever complained about this, thus I guess it's not such a common feature). but does that mean you need to be demanding? it sounds as like i am making money with this and live a life of wealth and freedom... well... i don't :-) and as long as i have a day job and hardly anyone gives back for all the work on this I will update the lib whenever I have the time. OR you dig into it and send me the changes. Now that's what I would call real open source teamwork!

sorry everyone... needed to get this out... still collecting bugs for the next version. this one will be on the list!
#9

[eluser]Bhupal Sapkota[/eluser]
@macigniter
this library is awesome, thanks a lot for this work.


actually, with "this" in the line below

Quote:this needs to be done asap.

i was pointing to the problem i am currently working on. i have clearly mentioned the issues i am facing above.


Quote: you’ve found a bug (or let’s call it a missing feature since no one ever complained about this, thus I guess it’s not such a common feature). but does that mean you need to be demanding?

and i didn't mean to be demanding. i am sorry.

"i need to work out this problem asap". i am digging in the formgenlib code for more than 10hrs now, but got no where. do we have any development docs for formgenlib? talking about teamwork, i would like to work on this "feature" if you can share some docs on the code flow of formgenlib.

thanks for adding this to "feature list" for next version.
also, can we make it easy to assign multiple classes to a element?

like below or other way

Code:
->text('foo', '', 'max_length[40]','')    
->add_class('js-class')
->add_class('css-class')


Cheers.
#10

[eluser]Unknown[/eluser]
I realise this is a bit of a old thread, but just in case someone with the same problem comes across it:

I think I've found a solution. The below diff allows the use of array fields in a form.

The first problem is that FGL rejects any fields with duplicate names. I have adjusted the code to allow duplicate field names as long as they end with [].
The second problem (and the reason for the first) is that the field ID is used, unchecked, as a unique reference for the field. Once the whole form has been processed, the only field of the array with a reference is the last, causing the value in the last field on submit to be filled in for all the inputs in the array. I have solved this by checking that the reference is unique and adding a sequential number if not.

I have NOT tested this particularly well. It seems to work for my use case but it might need someone with better knowledge of the code to check it. I don't know if there are any unforeseen consequences of changing the unique identifier.

USAGE:
Add the inputs to the form as normal, ensuring the field name ends with [] as it should to produce valid HTML.
In the case of variable length arrays of inputs, you need to make sure you add enough fields to hold all the entered data (check the length of $this->input->post('yourfield')), the library won't do it for you!

DIFF:
The following diff is applied to the current release of the library:

Code:
# This patch file was generated by NetBeans IDE
# It uses platform neutral UTF-8 encoding and \n newlines.
--- \\192.168.0.101\files\src\shopping\application\libraries\Form_1.php
+++ \\192.168.0.101\files\src\shopping\application\libraries\Form.php
@@ -1050,7 +1050,7 @@
   */  
  function _check_name($name, $type='') {
   if ($type && !$name) show_error(FGL_ERR.'No name specified for '.$type.' element.');
-  if (in_array($name, $this->_names)) show_error(FGL_ERR.'Element name "'.$name.'" already exists.');
+  if (in_array($name, $this->_names) && substr($name, strlen($name) - 2) != '[]') show_error(FGL_ERR.'Element name "'.$name.'" already exists.');
   if ($name) $this->_names[] = $name;
  }

@@ -1526,6 +1526,15 @@
   $type = $el->type;
   $name = (isset($el->name)) ? $el->name : NULL;
  
+                if( isset($this->{$unique}) ) {
+                    $i = 0;
+                    while( isset($this->{$unique.'['.$i.']'}) ) {
+                        $i++;
+                    }
+                    
+                    $unique = $unique.'['.$i.']';
+                }
+                
\ No newline at end of file
   $this->config['unique'][] = $unique;
   $this->{$unique} = $el;  
   $this->_elements[] = array('unique'=>$unique, 'name'=>$name, 'type'=>$type);

Gary Steinert




Theme © iAndrew 2016 - Forum software by © MyBB