Question

So what i want is to validate my Checkbox in my form and check if it's checked so i should save it as 1 in my database else as 0, like this :

$this->add(array(
            'name' => 'publication',
            'type' => 'Zend\Form\Element\Checkbox',

            'options' => array(
                    'checked_value' => 1,
                    'unchecked_value' => 0,
            ),
    ));

But when i tried i got Null in my database !!, for more details i will write some of my codes,

This is my view :

    <label>Publication:</label>
    <?php
         echo $this->formInput($form->get('publication'));
    ?>

And this is my Entity :

/**
 * @ORM\Column(name="publication", type="boolean")
 */
protected  $publication;

/**
 * Set publication
 *
 * @param boolean $publication
 * @return Article
 */
public function setPublication($publication)
{
    $this->publication = $publication;

    return $this;
}

/**
 * Get publication
 *
 * @return boolean 
 */
public function getPublication()
{
    return $this->publication;
}

public function exchangeArray($data)
{
   $this->publication  = (isset($data['publication']))  ? $data['publication']  : null;
}

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

            $inputFilter->add(array(
                'name'     => 'publication',
                'required' => false,
        ));

So please if someone has any solution i will be so appreciative :)

Was it helpful?

Solution

When a checkbox is not ticked, it is not included in the post. In your exchangeArray, you are setting it null in this case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top