array input like name=“person[]” in zend form

前端 未结 1 1873
鱼传尺愫
鱼传尺愫 2021-01-12 05:06

In normal html, we could have an array field like person[]




        
相关标签:
1条回答
  • 2021-01-12 05:12

    I think that ZF does not allow for creation of individual input text fields named person[], although you could do it for the whole form or a subform. However, it allows for something similar. Specifically, you could create fields named person[0], person[1], etc.

    To do this, you could do the following:

    $in1 = $this->createElement('text', '0');
    $in2 = $this->createElement('text', '1');
    $in1->setBelongsTo('person');
    $in2->setBelongsTo('person');
    

    This way you could normally attach your validators, filters, etc. to $in1 or $in2 and they would work as expected. In your action, after form validation, you could get an array of the person's input text fields as:

    $values = $yourForm->getValues();
    var_dump($values['person']);
    

    Interestingly, the following will NOT work:

    $in1 = $this->createElement('text', 'person[0]');
    $in2 = $this->createElement('text', 'person[1]');
    

    Hope this will help you.

    0 讨论(0)
提交回复
热议问题