ᴍᴇʜᴏᴠ
ᴍᴇʜᴏᴠ

Reputation: 5256

Unexpected field in POST data with a multiple choice fieldName

What I have

foreach ($statuses as $key=>$value) {
    echo $this->Form->control('Filter.statuses['.$key.']', array(
        'type' => 'checkbox',
        'value' => $key,
        'label' => $value,
    ));
}

What I'm getting

Unexpected field 'Filter.statuses[1' in POST data

Unexpected field 'Filter.statuses[2' in POST data

Unexpected field 'Filter.statuses[3' in POST data ...

What I have tried

$this->Form->unlockField('Filter.statuses');
$this->Form->unlockField('Filter.statuses[]');

If I remove the Filter. prefix, the errors are gone and I no longer need the unlockField() call.

References

Upvotes: 0

Views: 719

Answers (1)

ndm
ndm

Reputation: 60453

You're not supposed to use brackets in the field name, the form helper doesn't support that. If you ever need an unconventional name that the form helper doesn't support, then use the name option to specify it, while passing a compatible field name to the control() method's first argument.

Use the dot syntax all the way:

echo $this->Form->control("Filter.statuses.$key", /* ... */);

That way the form helper will be able to secure the fields, and create proper HTML name attribute values like Filter[statuses][1].

Upvotes: 2

Related Questions