ZF2:我可以将文本框附加到多选框吗?如何使用 MultiCheckbox 或 ZF2 中的复选框组创建 "Other" 或 "Comment" 之类的文本框字段?

ZF2: Can i attach textbox to multicheckbox ? How to create textbox field like "Other" or "Comment" with MultiCheckbox or group of checkboxes in ZF2?

我正在关注此 example,但我需要向 "Other" 选项添加一个额外的文本框,该选项在单击 "Other" 复选框时激活。我可以在表单中添加一个单独的文本框,并通过 JS 代码启用它,但是如何将它与 MultiCheckbox 的值或特定 属性 或特定 target_class 的复选框组组合在一起?

表格

class FieldEducationAssignmentsForm extends Form
{
    /**
     * @var EntityManager
     */
    protected $em;

    public function __construct($name = null)
    {
        parent::__construct('Field Education Assignments');

        $this->setAttribute('method', 'post');

        $this->add(array(
            'name' => 'id',
          //'type' => 'Hidden',
            'options' => array(
                'label' => 'Id',
            ),

        ));

        $this->add(array(
            'type' => 'Hidden',
            'name' => 'buildName',
            'attributes' => array(
                'value' => 'unknown'
            )
        ));

        $this->add(array(
            'type' => 'Zend\Form\Element\MultiCheckbox',
            'name' => 'directPractice',
            'options' => array(
                'label' => 'A. Check all direct practice field education assignments',
                'object_manager' => $this->getEntityManager(),
                'target_class' => ' OnlineFieldEvaluation\Entity\FieldEducationAssignments', //'YOUR ENTITY NAMESPACE'
                'property' => 'directPractice', //'your db collumn name'
                'empty_option' => '--- please choose ---',
                'value_options' => array(
                    '1' => 'Adults',
                    '2' => 'Individuals',
                    '3' => 'Information and Referral',
                    '4' => 'Families',
                    '5' => 'Advocacy',
                    '6' => 'Treatment Planning',
                    '7' => 'Children',
                    '8' => 'Groups',
                    '9' => 'Community Networking Linkages',
                    '10' => 'Adolescents',
                    '11' => 'Couples',
                    '12' => 'Case Management',
                    '13' => 'Discharge Planning',
                    '14' => 'Diagnostic Assessment',
                    '15' => 'Older Adults',
                    '16' => 'Psychosocial Assessment',
                    '17' => 'Short Term Intervention',
                    '18' => 'Crisis Intervention',
                    '19' => 'Long Term Intervention',
                    '20' => 'Inter/Multidisciplinary Team Meetings',
                    '21' => 'Other (specify)'
                ),
            ),
            'attributes' => array(
                'value' => '1', //set checked to '1'
                'multiple' => true,
            )
        ));

        $this->add(array(
            'name' => 'otherDirectPracticeTxt',
            'type' => 'Text',
            'attributes' => array(
                'disabled' => 'disabled',
            ),
        ));

    ...
    }

实体

<?php

namespace OnlineFieldEvaluation\Entity;

// Add these import statements
use Doctrine\ORM\Mapping as ORM;

/**
 * General Time Management.
 *
 * @ORM\Entity
 * @ORM\Table(name="form_general_time_management")
 *
 **/
class FieldEducationAssignments{

    /**
     * @ORM\id
     * @ORM\Column(type="integer");
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="integer");
     */
    protected $studEvalId;
    /**
     * @ORM\Column(type="string", length=1000)
     */
    protected $directPractice;

    /**
     * @param mixed $directPractice
     */
    public function setDirectPractice($directPractice)
    {
        $this->directPractice = $directPractice;
    }

    /**
     * @return mixed
     */
    public function getDirectPractice()
    {
        return $this->directPractice;
    }

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param mixed $studEvalId
     */
    public function setStudEvalId($studEvalId)
    {
        $this->studEvalId = $studEvalId;
    }

    /**
     * @return mixed
     */
    public function getStudEvalId()
    {
        return $this->studEvalId;
    }


    /**
     * Magic getter to expose protected properties.
     *
     * @param string $property
     * @return mixed
     */
    public function __get($property)
    {
        return $this->$property;
    }

    /**
     * Magic setter to save protected properties.
     *
     * @param string $property
     * @param mixed $value
     */
    public function __set($property, $value)
    {
        $this->$property = $value;
    }


    /**
     * Convert the object to an array.
     *
     * @return array
     */
    public function getArrayCopy()
    {
        return get_object_vars($this);
    }

}

我是这样完成的:

 $this->add(array(
        'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',

        'name' => 'directPractice',

        'options' => array(
            'label' => 'A. Check all direct practice field education assignments',
            'label_attributes' => array(
                'class' => 'label-multicheckbox-group'
            ),

            'required' => false,
            'allow_empty' => true,
            'continue_if_empty' => false,

            'object_manager' => $this->getObjectManager(),
            'target_class' => 'OnlineFieldEvaluation\Entity\FieldEducationAssignments', //'YOUR ENTITY NAMESPACE'
            'property' => 'directPractice', //'your db collumn name'
            'disable_inarray_validator' => true,
            'value_options' => array(
                '1' => 'Adults',
                '2' => 'Individuals',
                '3' => 'Information and Referral',
                '4' => 'Families',
                array(
                    'value' => 'Other',
                    'label' => 'Other (specify)',
                    'label_attributes' => array(
                        'class' => 'bindto',
                        'data-bindit_id' => 'otherDirectPracticeTxt_ID'
                    ),
                    'attributes' => array(
                        'id' => 'otherDirectPractice_ID',
                    ),
                )
            ),
        ),
        'attributes' => array(
            'value' => '1', //set checked to '1'
            'multiple' => true,

        )
    ));

    $this->add(array(
        'name' => 'otherDirectPracticeTxt',
        'type' => 'Text',
        'attributes' => array(
            'id' => 'otherDirectPracticeTxt_ID',
            'disabled' => 'disabled',
        ),
    ));

JQuery

    $(".bindto").each(function(){
        debugger;
        var binditId = $(this).attr("data-bindit_id");
        var $txtBoxToBind = $(this.form).find('input[id=' + binditId + ']');
    $(this).after($txtBoxToBind);
   });