0

Im working on Zend Framework 2 especially with Zend Forms. I have declared a Select dropdown box in

Form:

$selectElement = new Element\Select('selectElement');
    $selectElement->setAttribute('title', 'Select a Value')
            ->setAttribute('id', 'id');

    $data = array(
        array(
           //Fetching the values from database
            ),
    );

   $selectElement->setAttribute('multiple', 'multiple')      
        ->setValueOptions($data);

   $this->add($selectElement);

InputFilter:

$inputFilter->add($factory->createInput(array(
                'name'     => 'selectElement',
                'required' => false,
                'filters'  => array(
                    array(
                        'name' => 'Int'
                        ),
                    ),
           )));

I have used Zend Debug to get the values which are in the selectElement dropbox in this fashion:

$dataSelectElements = $this->getRequest()->getPost('selectElement');
 \Zend\Debug\Debug::dump($dataSelectElements);

Debug Result:

array(4) {
  [0] => string(2) "20"
  [1] => string(2) "22"
  [2] => string(2) "23"
  [3] => string(2) "75"
}

Basically Im getting the id's from the selectElement form to store it in the database. Right now Im getting a notice and zend form error:

Notice Error:

Notice: Array to string conversion in ..\zendframework\zendframework\library\Zend\Filter\Int.php on line 29

And a form invalid error:

array(1) {
  [0] => array(1) {
    ["selectElement "] => array(1) {
      ["explodeInvalid"] => string(35) "Invalid type given. String expected"
    }
  }
}

Is there a solution to over come this problem. Any help would be appreciated.

3 Answers 3

1

The Int filter will attempt to make an Integer out of your array of data, which is not going to work.

Previously I've used the Callback filter, which can be used to loop through the data and check if each value is an Int.

For example:

'filters' => array(
    array(
        'name' => 'Callback',
        'options' => array(
            'callback' => function($values) {
                return array_filter($values, function($value) {
                    return ((int)$value == $value);
                });
            }
        )
    ),
),
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your post, it helped to certain extent, but right now Im getting ["notInArray"] error. This is the error im getting for the same code as mentioned above, the only difference is that in my inputFilter I have removed the Int filter and replaced it with Callback filter. Here is the error array(1) { [0] => array(1) { ["selectElement "] => array(1) { [0] => array(1) { ["notInArray"] => string(39) "The input was not found in the haystack" } } } }. Any idea how to overcome this
Does the code work without the filter applied? If so it may be worth simply filtering the values after the form validation has occurred. Otherwise it seems to be the Checkbox/Radio elements that apply the InArray validator to test for true or false on the inputs. That error message suggests that the InArray validator is not getting the correct data. Sorry I cannot be of further help with this.
0

I did bit differently, something like this

form

class Companyform extends Form

    {
        public function __construct()
        {
            // we want to ignore the name passed
            parent::__construct('company');

            $this->setAttribute ('method', 'post');
            $this->setAttribute ('class', 'form-horizontal');
    $this->add ( array (
                'name' => 'parentID',
                'type' => 'Zend\Form\Element\Select',        
                'attributes' => array(
                    'id' => 'parentID',
                    'type' => 'select',
                    'placeholder' => "Parent Company",
                ),
                'options' => array(
                    'label' => 'Parent Company'
                )
            ));

            $this->add(array(
                'name' => 'btnsubmit',
                'attributes' => array(
                    'id' => 'btnsubmit',
                    'type' => 'submit',
                    'value' => 'Add',   
                    'class' => 'btn btn-primary'
                ),
            ));

        }

    }

controller

public function addAction() 
{        
    $request = $this->getRequest();

    $companyList = $this->_getCompanyList();         

    $form = new Companyform();                  

    $form->get('parentID')->setAttribute('options',$companyList);


    if ($request->isPost()) 
    {
        $company = new Company();   
            $form->setInputFilter($company->getInputFilter());            

            $form->setData($request->getPost());

            if ($form->isvalid()) 
            {                 
            }

    }
}

public function _getCompanyList()
{
     $companies = $this->Em()->getEntityManager()->getRepository('XXXX\Entity\Company')->findBy(array('isDeleted'=>'0'));
     $companyIDList = array();
     $companyIDList[0] = "No Parent";
     foreach ($companies as $company) 
     {        
       $companyIDList[$company->id] = $company->companyName;
     }         
     return $companyIDList;        
}

Entity class

 protected $inputFilter;

    public function setInputFilter(InputFilterInterface $inputFilter)
    {
        throw new \Exception("Not used");
    }

    public function getInputFilter()
    {
        if (!$this->inputFilter) {
            $inputFilter = new InputFilter();

            $factory = new InputFactory();

            $inputFilter->add($factory->createInput(array(
                'name' => 'companyName',
                'required' => true,
                'filters' => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name' => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min' => 2,
                            'max' => 255,
                        ),
                    ),
                ),
            )));

            $this->inputFilter = $inputFilter;
        }

        return $this->inputFilter;
    }

You may need to add following library in entity

use Zend\InputFilter\InputFilter;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;

Comments

0

In ZendFramework 2, when you creating a (add) element from your Form file, Check the attribute: inarrayvalidator is true.

        $this->add(array(
           'name' => 'select_name',
           'type' => 'select',
           'id' => 'select_name',
           'options' => array(
                 'label' => 'Select Name',
            ),
            'attributes' => array(
                 'id' => 'select_id',
                  'inarrayvalidator' => true,
             ),
        )); 

I hope, this works...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.