سؤال

PFBC is a simple yet totally undocumented framework that is really useful to get started, it is less complex than other frameworks and looks really nice Out of the box.

http://code.google.com/p/php-form-builder-class/

I have the following two arrays in PHP:

$area = [0=>"name", 10=>"name2", 11=>"name3"];
$ppl = [0=>"name", 1=>"name2", 2=>"name3"];

I want to use them as select, where the user will be able to choose between the names. This is the code I use for each:

$form->addElement(new Element\Select(htmlentities("Area type:"), 
    "area", $area, array("required" => 1)
));

$form->addElement(new Element\Select(htmlentities("Person:"), 
    "ppl", $ppl, array("required" => 1)
));

I was expecting to have this:

<select id="area" required="" name="area">
    <option value="1">
       name
    </option>
    <option value="10">
        name2
    </option>
    <option value="11">
        name3
    </option>
</select>

Wich i got for the first array ($area) but for the second array ($ppl) i've got:}

<select id="ppl" required="" name="ppl">
    <option value="name">
       name
    </option>
    <option value="name2">
        name2
    </option>
    <option value="name3">
        name3
    </option>
</select>

-- I need the numeric code as value since i will use what the user chooses to query a database by that id

Any ideas of what might happen?

هل كانت مفيدة؟

المحلول 2

So i found the problem:

Somwhere along the way while PFBC creates the Select element, it uses the third parameter (in my example, $area or $ppl) to generate an internal property called options, probably to cover itself against single arrays of the type ["name", "name2", "name3"], inside OptionElement.php the following code is causing problems.

public function __construct($label, $name, array $options, array $properties = null) {
    $this->options = $options;
    if(!empty($this->options) && array_values($this->options) === $this->options)
        $this->options = array_combine($this->options, $this->options);
        parent::__construct($label, $name, $properties);
}

The error is here: array_values($this->options) === $this->options in my second array, $ppl, i have a perfectly indexed table starting from zero

$ppl = [
    0 => "name",
    1 => "name2",
    2 => "name3"
]

this triggers the control to think i have a simple array instead of custom keys, as: $array_values($ppl) === $ppl returns boolean(true)

The second array, $area is different, since it has missing keys:

$area = [
    0 => "name",
    10=> "name2",
    11=> "name3"
]

So $array_values($area) === $area returns boolean(false)

My solution, before touching PFBC (might be a bug, there has to be a better way to detect this case):

Change the database so $ppl starts from 1 instead of 0

$ppl = [
    1 => "name",
    2 => "name2",
    3 => "name3"
]

Incidentally then $array_values($ppl) === $ppl returns boolean(false)

And now my Select works as i expected:

<select id="person" required="" name="person">
    <option value="1">
       name
    </option>
    <option value="2">
        name2
    </option>
    <option value="3">
        name3
    </option>
</select>

Hope this helps!

نصائح أخرى

PFBC does have something to handle this - if you change the array key from 0 to "0:pfbc" the PFBC\OptionElement::getOptionValue() recognises it as an associative array.

I wrote a function to convert the array to a PFBC suitable array:

function getPfbcSafeArray(array $array){

    // This looks overcomplicated but we want to ensure we don't accidently re-order this array
    // Doing 
    //      $array['0:pfbc'] = $array[0];
    //      unset($array[0]); 
    // would put 0 at end


    $keys = array_keys($array);
    $index = array_search(0, $keys);

    if ($index !== false) {
        $keys[$index] = "0:pfbc";
        $array = array_combine($keys, $array);
    }
    return $array;
}

//An array that has first item as 0
$area = [0=>"name", 10=>"name2", 11=>"name3"];

//pass $area through getPfbcSafeArray() as we pass it to $form->addElement()
$form->addElement(new Element\Select(htmlentities("Area type:"), 
    "area", getPfbcSafeArray($area), array("required" => 1)
));

Would generate

<select id="area" required="" name="area">
    <option value="0">
       name
    </option>
    <option value="10">
        name2
    </option>
    <option value="11">
        name3
    </option>
</select>

Technically this is overkill - PFBC only does it wrong (in my opinion) if the first item in the array is 0 index. If the array has a 0 index in it after the start then the select box elements would render as expected. The function above is 'correcting' any occurrence of 0 index.

Thank you so much for the answer. Was having the same problem. It seems I got it working by simply changing the index of the array to start from 1 instead of 0.

My case:

$ppl = array("1" =>"Bring Client Through Questionnaire","0" =>"Send Questionnaire to Client");

Solution: For a select on a pull down list add blank value at the start of the list.

Just ran into that same issue. All the values in my database always start with 1 or greater as 0 is equivalent to not found for objects. I don't allow 0 as a valid objectID.

I was adding the line "-----Select on of the values-----" as an entry and had it as [0] which then gave me the 0,1,2... issue. Change the [0] to [''] blank and the PFBC code will work correctly. Agree another option is to modify the PFBC code but this is a work around solution.

Now both of these will work:

$ppl = [
    '' => "-----Select on of the values-----",
    0 => "name",
    1 => "name2",
    2 => "name3"
]

and

$ppl = [
    '' => "-----Select on of the values-----",
    1 => "name",
    2 => "name2",
    3 => "name3"
]

As mentioned, the issue is in class:

\PFBC\OptionElement

this class is extended by the following classes

  • Checkbox
  • Radio
  • Select
  • Sort
  • UnorderedList

This solution is probably not useful for classes like Radio or Checkbox where one would be less likely to want a menu option in the list.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top