Symfony\Component\Form\Extension\DataCollector\FormDataCollector::collectSubmittedData PHP Method

collectSubmittedData() public method

public collectSubmittedData ( Symfony\Component\Form\FormInterface $form )
$form Symfony\Component\Form\FormInterface
    public function collectSubmittedData(FormInterface $form)
    {
        $hash = spl_object_hash($form);

        if (!isset($this->dataByForm[$hash])) {
            // field was created by form event
            $this->collectConfiguration($form);
            $this->collectDefaultData($form);
        }

        $this->dataByForm[$hash] = array_replace(
            $this->dataByForm[$hash],
            $this->dataExtractor->extractSubmittedData($form)
        );

        // Count errors
        if (isset($this->dataByForm[$hash]['errors'])) {
            $this->data['nb_errors'] += count($this->dataByForm[$hash]['errors']);
        }

        foreach ($form as $child) {
            $this->collectSubmittedData($child);

            // Expand current form if there are children with errors
            if (empty($this->dataByForm[$hash]['has_children_error'])) {
                $childData = $this->dataByForm[spl_object_hash($child)];
                $this->dataByForm[$hash]['has_children_error'] = !empty($childData['has_children_error']) || !empty($childData['errors']);
            }
        }
    }

Usage Example

Example #1
0
 public function testCollectSubmittedDataExpandedFormsErrors()
 {
     $child1Form = $this->createForm('child1');
     $child11Form = $this->createForm('child11');
     $child2Form = $this->createForm('child2');
     $child21Form = $this->createForm('child21');
     $child1Form->add($child11Form);
     $child2Form->add($child21Form);
     $this->form->add($child1Form);
     $this->form->add($child2Form);
     $this->dataExtractor->method('extractConfiguration')->will($this->returnValue(array()));
     $this->dataExtractor->method('extractDefaultData')->will($this->returnValue(array()));
     $this->dataExtractor->expects($this->at(10))->method('extractSubmittedData')->with($this->form)->will($this->returnValue(array('errors' => array())));
     $this->dataExtractor->expects($this->at(11))->method('extractSubmittedData')->with($child1Form)->will($this->returnValue(array('errors' => array())));
     $this->dataExtractor->expects($this->at(12))->method('extractSubmittedData')->with($child11Form)->will($this->returnValue(array('errors' => array('foo'))));
     $this->dataExtractor->expects($this->at(13))->method('extractSubmittedData')->with($child2Form)->will($this->returnValue(array('errors' => array())));
     $this->dataExtractor->expects($this->at(14))->method('extractSubmittedData')->with($child21Form)->will($this->returnValue(array('errors' => array())));
     $this->dataCollector->collectSubmittedData($this->form);
     $this->dataCollector->buildPreliminaryFormTree($this->form);
     $data = $this->dataCollector->getData();
     $formData = $data['forms']['name'];
     $child1Data = $formData['children']['child1'];
     $child11Data = $child1Data['children']['child11'];
     $child2Data = $formData['children']['child2'];
     $child21Data = $child2Data['children']['child21'];
     $this->assertTrue($formData['has_children_error']);
     $this->assertTrue($child1Data['has_children_error']);
     $this->assertFalse(isset($child11Data['has_children_error']), 'The leaf data does not contains "has_children_error" property.');
     $this->assertFalse($child2Data['has_children_error']);
     $this->assertFalse(isset($child21Data['has_children_error']), 'The leaf data does not contains "has_children_error" property.');
 }
All Usage Examples Of Symfony\Component\Form\Extension\DataCollector\FormDataCollector::collectSubmittedData