XF 2.0 field-adder post to array

AndrewSimm

Well-known member
I am trying to submit into an array, but when using js to duplicate the row it doesn't work. If I just add the select twice it works fine. Field-adder dynamically adds the necessary input, but only the first input submits to post.

Help!

HTML:
<ul>
    <li data-xf-init="field-adder" class="field-adder">
        <xf:select name="school_interest[]" placeholder="Select College">
            <xf:option value=""></xf:option>
            <xf:foreach loop="$colleges" value="$college">
                <xf:option value="{$college.man_college_id}">{$college.manage_college_name}</xf:option>
            </xf:foreach>
        </xf:select>
    </li>
</ul>
 
We have field adder examples that work essentially the same way:
Code:
<li data-xf-init="field-adder">
   <xf:textbox name="answers[]" value="" placeholder="{{ phrase('answer...') }}" />
</li>
Though I don't think we've ever used it with a select.
 
We have field adder examples that work essentially the same way:
Code:
<li data-xf-init="field-adder">
   <xf:textbox name="answers[]" value="" placeholder="{{ phrase('answer...') }}" />
</li>
Though I don't think we've ever used it with a select.

The problem I am having is when I submit the form only the first value shows under post. If I just duplicate the selects than I get both values. For some reason when js is used to generate additional inputs or selects, only the first get's submitted. For what it's worth, I am not using ajax with the form. I submit the form and the send the values to an action.
 
Consider using the following:
HTML:
                    <li data-xf-init="field-adder" data-increment-format="school_interest[{counter}]">
                        <div class="inputGroup">
                            <xf:select name="school_interest[{$nextCounter}]" class="filterBlock-input">
                                <xf:option value=""></xf:option>
                                <xf:foreach loop="$colleges" value="$college">
                                    <xf:option value="{$college.man_college_id}">{$college.manage_college_name}</xf:option>
                                </xf:foreach>
                            </xf:select>
                        </div>
                    </li>

As for the $nextCounter, that's handled by the viewParams to the template:

PHP:
        $viewParams = [
            'player' => $player,
            'nextCounter' => count($existingSchools),
        ];

Obviously I'm just guessing at your variable names here but you know :p


Fillip
 
Consider using the following:
HTML:
                    <li data-xf-init="field-adder" data-increment-format="school_interest[{counter}]">
                        <div class="inputGroup">
                            <xf:select name="school_interest[{$nextCounter}]" class="filterBlock-input">
                                <xf:option value=""></xf:option>
                                <xf:foreach loop="$colleges" value="$college">
                                    <xf:option value="{$college.man_college_id}">{$college.manage_college_name}</xf:option>
                                </xf:foreach>
                            </xf:select>
                        </div>
                    </li>

As for the $nextCounter, that's handled by the viewParams to the template:

PHP:
        $viewParams = [
            'player' => $player,
            'nextCounter' => count($existingSchools),
        ];

Obviously I'm just guessing at your variable names here but you know :p


Fillip

Fillip, thanks for the recommendation.

It is still submit only the first select. Also what does {counter} do?

Here is how it looks to my browser

Screen Shot 2018-01-06 at 9.19.57 PM.webp
 
Why do you have multiple field-adder instances? You need to exclude it from any foreach loops.

{counter} is an incremental counter, so you’d get school_interest[0] etc.


Fillip
 
Why do you have multiple field-adder instances? You need to exclude it from any foreach loops.

{counter} is an incremental counter, so you’d get school_interest[0] etc.


Fillip

Because that is what field-adder does

Here is my code, it's what you recommended.

Code:
<li data-xf-init="field-adder" data-increment-format="school_interest[{counter}]">
    <div class="inputGroup">
        <xf:select name="school_interest[{$nextCounter}]" class="filterBlock-input">
            <xf:option value=""></xf:option>
            <xf:foreach loop="$colleges" value="$college">
                <xf:option value="{$college.man_college_id}">{$college.manage_college_name}</xf:option>
            </xf:foreach>
        </xf:select>
    </div>
</li>

It only produces the first value in the array.
PHP:
$school_interest = $this->filter('school_interest', 'array');
var_dump($school_interest);
 
Top Bottom