ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Using Arrays as Field Names

May 29, 2010 9:50am

Subscribe [10]
  • #16 / Oct 13, 2010 9:33am

    thomasp

    3 posts

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

    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 / Nov 25, 2010 6:51am

    webdevguy

    9 posts

    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 / Nov 25, 2010 7:14am

    helenth01

    3 posts

    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 / Nov 25, 2010 7:15am

    helenth01

    3 posts

    Well, it’s amazing. The miracle has been done. Well done.
    ======================
    PLR Private Label Rights

  • #20 / Apr 28, 2011 9:44am

    Ninjabear

    54 posts

    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.

    It’s funny you should write this actually. I needed this exact thing for Jquery when I built my site a few months ago (http://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:

    $(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.

  • #21 / Aug 03, 2011 12:48pm

    Krzysiaczek

    15 posts

    Guess, I never had tried this before. I tried this and it worked.

    <?php 
          $options = set_value('options[]');
          $i = 0;
          foreach($ids as $id) : ?>
    
             <input type="text" name="options[]" id="options_<?=$id;?>" value="<?=$options[$i];?>" />
    
    <?php 
          ++$i;
          endforeach; ?>

    ...cut…

    Hi

    Pickupman’s solution has worked for me to set_value() (thanks for that) but form_error() was returning only the last found error (for each field in an array). I’ve found the solution and added my few cents to make it work better. It’s not optimal but works for me 😊

    You can find it here:
    http://pgmmer.blogspot.com/2011/05/codeigniter-formvalidation-errors-with.html

    Thanks
    Chris

  • #22 / Sep 22, 2011 1:25pm

    Ionut

    1 posts

    In case wanted to be able to provide a specific index also, you can try this override:
    e.g.

    set_value('my_array[color_index]', 'some_default');
    /**
         * Overwrite CI method. 
         * Has the same functionality with some added hacks to allow array indexes
         * e.g. set_value('someArray[color]') works as expected if you used name="someArray[color]"
         *
         * @param    string    the field name
         * @param    string
         * @return    void
         */
        function set_value($field = '', $default = '')
        {
            preg_match('~([^\[]+)(\[(.*)\])?~', $field, $matches);
            $varName = $matches[1];
    
            // check if we're provided with an array name
            if (!empty($matches[2])) {
                // rebuild the array name expected by CI
                $varName = $matches[1] . '[]';
                $arrKey = '';
                // check if we also have a specific array key
                if (!empty($matches[3])) {
                    $arrKey = $matches[3];
                }
            }
            if (!isset($this->_field_data[$varName])) {
                return $default;
            }
    
            if (is_array($this->_field_data[$varName]['postdata'])) {
                
                if (!empty($arrKey)) {
                    if (isset($this->_field_data[$varName]['postdata'][$arrKey])) {
                        $result = $this->_field_data[$varName]['postdata'][$arrKey];
                    } else {
    //                    $result = print_r($default, true);
                        $result = $default;
                    }
                } else {
                    // allow standard CI behaviour
                    $result = array_shift($this->_field_data[$field]['postdata']);
                }
                return $result;
            }
    
            return $this->_field_data[$field]['postdata'];
        }
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases