Results 1 to 4 of 4

Thread: posted values in form are reindexed when a subform exist

  1. #1
    alexToader is offline Junior Member
    Join Date
    Jan 2010
    Posts
    8

    Default posted values in form are reindexed when a subform exist

    Hi,

    I have an form with a subform.
    My $_POST array looks like this:

    array
    167302 => string '167308' (length=6)
    167314 => string '167344' (length=6)
    167347 => string '5867521' (length=7)
    '3754952706' => string '3754952706' (length=10)
    '3744466946' => string '3744466946' (length=10)
    '3740271617' => string '3740271617' (length=10)
    '3749709826' => string '3749709826' (length=10)
    '3749708801' => string '1' (length=1)
    'meta' =>
    array
    'hasJavascript' => string '0' (length=1)
    'timeToComplete' => string '1264769572.6144' (length=15)
    'Submit' => string 'Next' (length=4)

    But when I try to get the $form->getValues();

    my values look like this:
    array
    0 => string '167308' (length=6)
    1 => string '167344' (length=6)
    2 => string '5867521' (length=7)
    '3754952706' => string '3754952706' (length=10)
    '3744466946' => string '3744466946' (length=10)
    '3740271617' => string '3740271617' (length=10)
    '3749709826' => string '3749709826' (length=10)
    '3749708801' => string '1' (length=1)
    'meta' =>
    array
    'hasJavascript' => string '0' (length=1)
    'timeToComplete' => string '1264769572.6144' (length=15)




    WHAT is happening?
    This:
    $values = array_merge($values, $fValues); //Zend/Form.php -> getValues

    if you merge those 2 arrays


    $arr1 = array(
    167302 => '167308',
    167314 => '167344',
    167347 => '5867521',
    '3754952706' => '3754952706',
    '3744466946' => '3744466946',
    '3740271617' => '3740271617',
    '3749709826' => '3749709826',
    '3749708801' => '1'
    );

    $arr2 = array(
    'meta' =>
    array (
    'hasJavascript' => '0',
    'timeToComplete' => '1264769572.6144'
    )
    );

    guess what will be the result? your numeric keys will be reindexed starting from 0.



    Now...
    Why will my form return int values? I thought all posted values are treated like string?

  2. #2
    alexToader is offline Junior Member
    Join Date
    Jan 2010
    Posts
    8

    Default

    Yes...

    So when you have a form with some radio:

    <form method='post'>
    <input type='radio' name='55' value='1' >
    <input type='radio' name='55' value='2' >
    <input type='radio' name='55' value='3' >
    <input type='radio' name='55' value='4' >

    <input type='submit' value='ok' >
    </form>


    if the name of the radio is a number will be returned by $_POST as INT!!!! not string as the manual says



    if the name of the radio is 'test' will be of course returned ad string.

  3. #3
    SirAdrian's Avatar
    SirAdrian is offline Member
    Join Date
    Apr 2008
    Posts
    87

    Default

    Post your Zend_Form code here please.

  4. #4
    alexToader is offline Junior Member
    Join Date
    Jan 2010
    Posts
    8

    Default

    This dont have much to do with zend.

    the $_POST is returning keys as int in some cases and array_merge from zend $form->getValues(); is altering those ints.

    I started the thread here because I thought is something zend specific but actually is a strange behavior of $_POST combined with some features of array_merge

    so really nothing to do with zend.
    My bad to accuse zend for this one



    anyawy something to be aware of when creating forms dynamically and if you could end with elements that have numbers as names.

    the solution I found was to append a string in front of the name of any element with a numeric name.

    so if you have
    <input type='radio' name='55' value='1' >

    you could avoid some problems by adding something in front of 55 to convert it somehow to string.

    <input type='radio' name='var_55' value='1' >
    is much safer from the start.

Similar Threads

  1. Mulitple form submit buttons lose their values
    By ramonhimera in forum Model-View-Controller (MVC)
    Replies: 1
    Last Post: 01-30-2010, 04:42 PM
  2. Replies: 1
    Last Post: 12-07-2009, 07:13 PM
  3. insert if doesn't exist
    By nickleplated in forum Databases
    Replies: 2
    Last Post: 12-01-2009, 06:07 AM
  4. Need help in getting values inside form
    By snsanju in forum General Q&A on Zend Framework
    Replies: 1
    Last Post: 08-12-2009, 06:54 PM
  5. Populating form: automatically generate multi-fields for values array
    By Exception e in forum Core Infrastructure
    Replies: 1
    Last Post: 05-14-2009, 12:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •