Welcome Guest, Not a member yet? Register   Sign In
Using Arrays as Field Names
#11

[eluser]moleculezz[/eluser]
That's much nicer.
Thanks a lot for your help!!
#12

[eluser]bkirkman[/eluser]
I found the following bug report that helped solve this issue for me.

Arrays as field names not working

Thanks, zdknudsen! Worked like a charm. I didn't have anything to do with it, but maybe this will help someone save some time and frustration in the future.
#13

[eluser]webdevguy[/eluser]
Can someone please post some working code. I'm going out of my mind trying to solve this problem that at least two people have solved here. Also, the link for the bug tracker with it's solution is a dead link.

I keep getting either the word 'Array' or blanks in my input fields on return from the server with invalid data.
#14

[eluser]bkirkman[/eluser]
Since the above link is dead, here's the fix that worked for me. I take no credit. It was posted in the bug fix. This is what the set_value function should look like. It can be modified in the Form_validation library, but it is recommended to override it in the custom project library MY_Form_validation. Here goes.

Code:
function set_value($field = '', $default = '')
{
    if ( ! isset($this->_field_data[$field]))
    {
        return $default;
    }

    $field = &$this->_field_data[$field]['postdata'];

    if (is_array($field))
    {
        $current = each($field);
        return $current['value'];
    }

    return $field;
}

Hope this helps.
#15

[eluser]bkirkman[/eluser]
To expound on the revision above, the following is the way I have set up the controller and the view. Set up validation in the controller as follows.

Controller:
Code:
...

$this->form_validation->set_rules('item_id[]', 'Item Id', 'trim');
$this->form_validation->set_rules('item_no[]', 'Item No.', 'trim');
$this->form_validation->set_rules('item_description[]', 'Item Description', 'trim');
$this->form_validation->set_rules('item_type[]', 'Item Type', 'trim');

...
item_id is the auto-incremented key value for items in a database and is hidden in the view. The other three fields are columns in the table and will be fields in the view.

Then in the view, I chose to dynamically set the field values depending on the existing condition. I suppose there's a more elegant way to refactor this code, but I, too, spent a lot of time pulling my hair out. I was just happy with a solution.

View:
Code:
...

<table id="tblItems">
    <tr>
    <td class="txtBase">Item No.</td>
    <td class="txtBase">Item Description</td>
    <td class="txtBase">Item Type</td>
    </tr>

&lt;?php if(isset($_POST['item_no'])) : ?&gt;  &lt;!-- this is to set values for forms that do not pass validation --&gt;
&lt;?php foreach($_POST['item_no'] as $row) : ?&gt;
<tr>
    &lt;input type="hidden" name="item_id[]" value="&lt;?php echo set_value('item_id[]'); ?&gt;" /&gt;
    <td>&lt;input type="text" size="10" name="item_no[]" value="&lt;?php echo set_value('item_no[]'); ?&gt;" /&gt;&lt;/td>
    <td>&lt;input type="text" size="40" name="item_description[]" value="&lt;?php echo set_value('item_description[]'); ?&gt;" /&gt;&lt;/td>
    <td>&lt;input type="text" size="10" name="item_type[]" value="&lt;?php echo set_value('item_type[]'); ?&gt;" /&gt;&lt;/td>
</tr>
&lt;?php endforeach; ?&gt;
&lt;?php elseif(isset($item_no)) : ?&gt;  &lt;!-- this is to set values for editing forms that have existing item data --&gt;
&lt;?php $i = 0; ?&gt;
&lt;?php foreach($item_no as $row) : ?&gt;
<tr>
    &lt;input type="hidden" name="item_id[]" value="&lt;?php echo set_value('item_id[]', $item_id[$i]); ?&gt;" /&gt;
    <td>&lt;input type="text" size="10" name="item_no[]" value="&lt;?php echo set_value('item_no[]', $item_no[$i]); ?&gt;" /&gt;&lt;/td>
    <td>&lt;input type="text" size="40" name="item_description[]" value="&lt;?php echo set_value('item_description[]', $item_description[$i]); ?&gt;" /&gt;&lt;/td>
    <td>&lt;input type="text" size="10" name="item_type[]" value="&lt;?php echo set_value('item_type[]', $item_type[$i]); ?&gt;" /&gt;&lt;/td>
</tr>
&lt;?php $i++; ?&gt;
&lt;?php endforeach; ?&gt;
&lt;?php else : ?&gt;  &lt;!-- this is to create new form fields if editing/adding a form with no items --&gt;
<tr>
    &lt;input type="hidden" name="item_id[]" value="&lt;?php echo set_value('item_id[]'); ?&gt;" /&gt;
    <td>&lt;input type="text" size="10" name="item_no[]" value="&lt;?php echo set_value('item_no[]'); ?&gt;" /&gt;&lt;/td>
    <td>&lt;input type="text" size="40" name="item_description[]" value="&lt;?php echo set_value('item_description[]'); ?&gt;" /&gt;&lt;/td>
    <td>&lt;input type="text" size="10" name="item_type[]" value="&lt;?php echo set_value('item_type[]'); ?&gt;" /&gt;&lt;/td>
</tr>
<tr>
    &lt;input type="hidden" name="item_id[]" value="&lt;?php echo set_value('item_id[]'); ?&gt;" /&gt;
    <td>&lt;input type="text" size="10" name="item_no[]" value="&lt;?php echo set_value('item_no[]'); ?&gt;" /&gt;&lt;/td>
    <td>&lt;input type="text" size="40" name="item_description[]" value="&lt;?php echo set_value('item_description[]'); ?&gt;" /&gt;&lt;/td>
    <td>&lt;input type="text" size="10" name="item_type[]" value="&lt;?php echo set_value('item_type[]'); ?&gt;" /&gt;&lt;/td>
</tr>
&lt;?php endif; ?&gt;

</table>

...

I'm not sure exactly how others are implementing this "feature." I'm using javascript to add blank fields to the form if the user needs to input more data. That's a discussion for another time. Also, writing the post data back to the database is not exactly trivial when dealing with edits and deletions. Perhaps that will also be a post for another time. Maybe a wiki is in order for forms such as this.
#16

[eluser]thomasp[/eluser]
With the fix posted above, default value does not apply when postdata is NULL.
So, a best fix for this issue seem to be :

Code:
function set_value($field = '', $default = '') {
        if (!isset($this->_field_data[$field])) {
            return $default;
        }
        $field = &$this->_field_data[$field]['postdata'];
        if(!isset($field)) {
            return $default;
        }
        else if (is_array($field)) {
            $current = each($field);
            return $current['value'];
        }else {
            return $field;
        }
    }
#17

[eluser]webdevguy[/eluser]
I just posted my solution to this problem on my blog at Expandable Input Block In CodeIgniter. It has a working demo and has the ability for useragents to return at a later date and update their entries.

I used really basic JavaScript validation in addition to CodeIgniter's validation since CI's error messages were not showing up inline (an absolute necessity in this case since the form could get very long if the users decided to add, say, 50 input blocks).

If someone wants to refactor my JQuery into a plug-in and/or improve the front-end validation I would be grateful.
#18

[eluser]helenth01[/eluser]
This conversation is going no where. It’s lacking the place of a good leader to head the things to come out on conclusion.
======================
PLR Private Label Rights
#19

[eluser]helenth01[/eluser]
Well, it’s amazing. The miracle has been done. Well done.
======================
[url="http://www.plrprivatelabelrights.com" rel="dofollow"]PLR Private Label Rights[/url]
#20

[eluser]Ninjabear[/eluser]
[quote author="webdevguy" date="1290703879"]I just posted my solution to this problem on my blog at Expandable Input Block In CodeIgniter. It has a working demo and has the ability for useragents to return at a later date and update their entries.

I used really basic JavaScript validation in addition to CodeIgniter's validation since CI's error messages were not showing up inline (an absolute necessity in this case since the form could get very long if the users decided to add, say, 50 input blocks).

If someone wants to refactor my JQuery into a plug-in and/or improve the front-end validation I would be grateful.[/quote]

It's funny you should write this actually. I needed this exact thing for Jquery when I built my site a few months ago (www.fixilink.com). I'll definitely have a look at your solution as well, could be simpiler.

I ended up with this piece of code:

Code:
$(function()
{
    //Add section
    var count = $(".duplicates").length;
    var addType=$("#addType").attr("value");
    
    $("#add_more").click(function() {
            if(count>4)//No more than 5
            {
                $(this).css("background-color","#e90189");
                $(this).css("color","#ffffff");
                $(this).text("Limit Reached");
                return false;
            }
            var existing = $("#duplicate_"+count);
            var theClone = existing.clone(1).hide().insertAfter(existing);//Clone elements//Hide duplicate
            count++;//Increment counter

            theClone.attr("id","duplicate_" + count);//Select item for cleaning / adding.

            $("#duplicate_"+count+" input").attr("value","");//Clear value for new input
            $("#duplicate_"+count+" h4").text(addType + " #" + count);
            
            $(".remove").each(function()
            {
                $(this).attr("class","remove off");
            });
            if(count>1)
                $(".remove").eq(count-1).attr("class","remove");
            
            theClone.slideDown("slow");
            return true;
    });    

    //Remove section    
    $(".remove").live("click",function() {
        if(count>1)//Keep one
            count--;//Decrement counter
        else
            return false;//Can't remove last block
        
        if(count<5)//No more than 5
        {
            $("#add_more").css("background-color","#b8d432");
            $("#add_more").css("color","#333333");
            $("#add_more").text("Add Another " + addType);
        }
        $(this).parent().slideUp("slow",function(){
             $(this).remove();
        });//Remove data from dom.

        //Reset the remove button
        $(".remove").each(function()
        {
            $(this).attr("class","remove off");
        });
        
        if(count>1)
            $(".remove").eq(count-1).attr("class","remove");

        return false;
    });            
    
})

Incidentally I never had to extend any libraries as you did and CI picks up the additional info here just fine. However I am just using a single form validation error at the top of the screen for a maximum of five fields.




Theme © iAndrew 2016 - Forum software by © MyBB