Development

Changeset 8695

You must first sign up to be able to contribute.

Changeset 8695

Show
Ignore:
Timestamp:
04/30/08 18:21:59 (6 months ago)
Author:
FabianLange
Message:

sf1.0: made sfFillInForm work with checkbox arrays as well like checkbox_many[]. including unit tests. fixes #1776, #3399

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.0/lib/util/sfFillInForm.class.php

    r4883 r8695  
    126126          // checkbox and radio 
    127127          $element->removeAttribute('checked'); 
    128           if ($this->hasValue($values, $name) && ($this->getValue($values, $name) == $value || !$element->hasAttribute('value'))) 
     128          if (is_array($this->getValue($values, $name)) && ($this->hasValue($values, $name) || !$element->hasAttribute('value'))) 
     129          { 
     130            if (in_array($value, $this->getValue($values, $name))) 
     131            { 
     132              $element->setAttribute('checked', 'checked'); 
     133            } 
     134          } 
     135          else if ($this->hasValue($values, $name) && ($this->getValue($values, $name) == $value || !$element->hasAttribute('value'))) 
    129136          { 
    130137            $element->setAttribute('checked', 'checked'); 
  • branches/1.0/test/unit/util/sfFillInFormTest.php

    r4883 r8695  
    1212require_once($_test_dir.'/../lib/util/sfFillInForm.class.php'); 
    1313 
    14 $t = new lime_test(50, new lime_output_color()); 
     14$t = new lime_test(53, new lime_output_color()); 
    1515 
    1616$html = <<<EOF 
     
    2323    <input type="checkbox" name="input_checkbox" value="1" checked="checked" /> 
    2424    <input type="checkbox" name="input_checkbox_not_checked" value="1" /> 
     25    <input type="checkbox" name="input_checkbox_multiple[]" value="1" checked="checked" /> 
     26    <input type="checkbox" name="input_checkbox_multiple[]" value="2" /> 
     27    <input type="radio" name="input_radio" value="1" checked="checked" /> 
    2528    <input type="password" name="password" value="" /> 
    2629    <textarea name="textarea">content</textarea> 
     
    7174$t->is(get_input_value($xml, 'input_checkbox', 'checked'), 'checked', '->fillInDom() preserves default values for checkbox'); 
    7275$t->is(get_input_value($xml, 'input_checkbox_not_checked', 'checked'), '', '->fillInDom() preserves default values for checkbox'); 
     76$t->is(get_input_value($xml, 'input_radio', 'checked'), 'checked', '->fillInDom() preserves default values for radio'); 
     77$t->is(get_input_value($xml, 'input_checkbox_multiple[]', 'checked'), array('checked', null), '->fillInDom() preserves default values for multiple checkboxes'); 
    7378$t->is($xml->xpath('//form[@name="form1"]/textarea'), array('content'), '->fillInDom() preserves default values for textarea'); 
    7479$t->is($xml->xpath('//form[@name="form1"]/select[@name="select"]/option[@selected="selected"]'), array('selected'), '->fillInDom() preserves default values for select'); 
     
    123128  'input_checkbox' => false, 
    124129  'input_checkbox_not_checked' => true, 
     130  'input_checkbox_multiple[]' => array(2), 
    125131  'password' => 'mypassword', 
    126132  'select' => 'first', 
     
    139145$t->is(get_input_value($xml, 'input_checkbox', 'checked'), '', '->fillInDom() fills in values for checkbox'); 
    140146$t->is(get_input_value($xml, 'input_checkbox_not_checked', 'checked'), 'checked', '->fillInDom() fills in values for checkbox'); 
     147$t->is(get_input_value($xml, 'input_checkbox_multiple[]', 'checked'), array(null, 'checked'), '->fillInDom() fills in values for multiple checkboxes'); 
    141148$t->is($xml->xpath('//form[@name="form1"]/textarea'), array('my content'), '->fillInDom() fills in values for textarea'); 
    142149$t->is($xml->xpath('//form[@name="form1"]/select[@name="select"]/option[@selected="selected"]'), array('first'), '->fillInDom() fills in values for select'); 
     
    183190function get_input_value($xml, $name, $attribute = 'value', $form = null) 
    184191{ 
     192  $value = ""; 
     193 
    185194  $xpath = ($form ? '//form[@name="'.$form.'"]' : '//form').sprintf('/input[@name="%s"]', $name); 
    186195 
    187196  $values = $xml->xpath($xpath); 
    188197 
    189   return (string) $values[0][$attribute]; 
     198  if (count($values) > 1) 
     199  { 
     200    foreach($values as $val) 
     201    { 
     202      $value[] = $val[$attribute]; 
     203    } 
     204  } 
     205  else 
     206  { 
     207    $value = (string) $values[0][$attribute]; 
     208  } 
     209 
     210  return $value; 
    190211} 
    191212