phphtmlzend-framework2zend-form2

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


I am following this example, but I need to add an additional textbox to "Other" option, which is activated when "Other" checkbox is clicked. I can add a separate text box to the form, and enable it through JS code, but how do I group it together with values of MultiCheckbox, or group of checkboxes for a specific property of a specific target_class ?

Form

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 column 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',
            ),
        ));

    ...
    }

Entity

<?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);
    }

}

Solution

  • I accomplished it this way:

     $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 column 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);
       });