Note that there are some explanatory texts on larger screens.

plurals
  1. POValidate Zend Form with empty mapped association between entities
    primarykey
    data
    text
    <p>First of all, sorry for my English.</p> <p><strong>My Orders entity</strong></p> <pre><code>* @ORM\Entity * @ORM\Table(name="orders") class Order implements InputFilterAwareInterface { ... /** * Order status * * @ORM\OneToOne(targetEntity="Admin\Entity\OrdersStatus") * @ORM\JoinColumn(name="status_id", referencedColumnName="id") **/ private $status; ... } </code></pre> <p><strong>My Order's Statues Entity</strong></p> <pre><code>* @ORM\Entity * @ORM\Table(name="orders_statuses") class OrdersStatus implements InputFilterAwareInterface { /** * @var Zend\InputFilter\InputFilter */ protected $inputFilter; /** * @ORM\Id * @ORM\Column(type="integer"); * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\Column(type="string") */ protected $title; } </code></pre> <p><strong>My OrderForm</strong></p> <pre><code>class OrderForm extends Form { public function __construct(EntityManager $entityManager) { parent::__construct('Order'); $this-&gt;setAttribute('method', 'post'); $this-&gt;setAttribute('class', 'itemForm'); $this-&gt;add(array( 'name' =&gt; 'id', 'attributes' =&gt; array( 'type' =&gt; 'hidden', ), )); $this-&gt;add(array( 'name' =&gt; 'status', 'type' =&gt; 'DoctrineModule\Form\Element\ObjectSelect', 'options' =&gt; array( 'label' =&gt; 'Status', 'object_manager' =&gt; $entityManager, 'empty_option' =&gt; '--- Select status ---', 'target_class' =&gt; 'Admin\Entity\OrdersStatus', 'property' =&gt; 'title' ), 'attributes' =&gt; array( 'required' =&gt; false ) )); } } </code></pre> <p>My OrderController</p> <pre><code>class OrderController extends AbstractActionController { ... public function editAction() { $em = $this-&gt;getEntityManager(); $id = (int) $this-&gt;getEvent()-&gt;getRouteMatch()-&gt;getParam('id'); $item = new Stock\Entity\Order; if ($id) { $item = $em-&gt;getRepository($this-&gt;entity)-&gt;find($id); } $form = new Stock\Form\OrderForm($em); $form-&gt;setHydrator(new DoctrineEntity($em, Stock\Entity\Order, false)); $form-&gt;bind($item); $request = $this-&gt;getRequest(); if ($request-&gt;isPost()) { $post = $request-&gt;getPost(); $form-&gt;setInputFilter($item-&gt;getInputFilter()); $form-&gt;setData($post); if ($form-&gt;isValid()) { $em-&gt;persist($item); $em-&gt;flush(); return $this-&gt;redirect()-&gt;toRoute('stock/order', array('action' =&gt; 'index')); } } ... } </code></pre> <p>When I'm trying to validate form without selecting order status the following error occurred: "<strong>Value is required and can't be empty</strong>".</p> <p>When I'm using this input filter:</p> <pre><code>public function getInputFilter() { if (!$this-&gt;inputFilter) { $inputFilter = new InputFilter(); $factory = new InputFactory(); $inputFilter-&gt;add($factory-&gt;createInput(array( 'name' =&gt; 'client', 'required' =&gt; false, ))); $inputFilter-&gt;add($factory-&gt;createInput(array( 'name' =&gt; 'status', 'allow_empty' =&gt; true, 'required' =&gt; false, 'filters' =&gt; array( array('name' =&gt; 'Null'), ), ))); } } </code></pre> <p>I got this: <strong>The identifier id is missing for a query of Admin\Entity\OrdersStatus</strong></p> <p>Without Null filter:</p> <blockquote> <p>An exception occurred while executing 'SELECT t0.id AS id1, t0.title AS title2, t0.description AS description3 FROM orders_statuses t0 WHERE t0.id = ?' with params [""]:</p> <p>SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: ""</p> </blockquote> <p>What is the right way to allow empty values in order's statuses?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload