[~TASK] Fluid (ViewHelpers): Added argument "objectName" to form ViewHelper. This...
authorBastian Waidelich <bastian@typo3.org>
Mon, 26 Jul 2010 14:24:22 +0000 (14:24 +0000)
committerBastian Waidelich <bastian@typo3.org>
Mon, 26 Jul 2010 14:24:22 +0000 (14:24 +0000)
typo3/sysext/fluid/Classes/ViewHelpers/Form/AbstractFormFieldViewHelper.php
typo3/sysext/fluid/Classes/ViewHelpers/FormViewHelper.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/FormViewHelperTest.php

index 75aaa5a..1609d51 100644 (file)
@@ -60,14 +60,14 @@ abstract class Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper extends Tx_
         */
        protected function getName() {
                if ($this->isObjectAccessorMode()) {
-                       $formName = $this->viewHelperVariableContainer->get('Tx_Fluid_ViewHelpers_FormViewHelper', 'formName');
-                       if (!empty($formName)) {
+                       $formObjectName = $this->viewHelperVariableContainer->get('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObjectName');
+                       if (!empty($formObjectName)) {
                                $propertySegments = explode('.', $this->arguments['property']);
                                $properties = '';
                                foreach ($propertySegments as $segment) {
                                        $properties .= '[' . $segment . ']';
                                }
-                               $name = $formName . $properties;
+                               $name = $formObjectName . $properties;
                        } else {
                                $name = $this->arguments['property'];
                        }
@@ -122,7 +122,7 @@ abstract class Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper extends Tx_
                        // hierarchical property. If there is no "." inside (thus $propertySegments == 1), we do not need to do anything
                        $formObject = $this->viewHelperVariableContainer->get('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObject');
 
-                       $objectName = $this->viewHelperVariableContainer->get('Tx_Fluid_ViewHelpers_FormViewHelper', 'formName');
+                       $objectName = $this->viewHelperVariableContainer->get('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObjectName');
                        // If Count == 2 -> we need to go through the for-loop exactly once
                        for ($i=1; $i < count($propertySegments); $i++) {
                                $object = Tx_Extbase_Reflection_ObjectAccess::getPropertyPath($formObject, implode('.', array_slice($propertySegments, 0, $i)));
@@ -161,7 +161,7 @@ abstract class Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper extends Tx_
         */
        protected function isObjectAccessorMode() {
                return $this->arguments->hasArgument('property')
-                       && $this->viewHelperVariableContainer->exists('Tx_Fluid_ViewHelpers_FormViewHelper', 'formName');
+                       && $this->viewHelperVariableContainer->exists('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObjectName');
        }
 
        /**
@@ -200,11 +200,11 @@ abstract class Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper extends Tx_
                        return array();
                }
                $errors = $this->controllerContext->getRequest()->getErrors();
-               $formName = $this->viewHelperVariableContainer->get('Tx_Fluid_ViewHelpers_FormViewHelper', 'formName');
+               $formObjectName = $this->viewHelperVariableContainer->get('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObjectName');
                $propertyName = $this->arguments['property'];
                $formErrors = array();
                foreach ($errors as $error) {
-                       if ($error instanceof Tx_Extbase_Validation_PropertyError && $error->getPropertyName() === $formName) {
+                       if ($error instanceof Tx_Extbase_Validation_PropertyError && $error->getPropertyName() === $formObjectName) {
                                $formErrors = $error->getErrors();
                                foreach ($formErrors as $formError) {
                                        if ($formError instanceof Tx_Extbase_Validation_PropertyError && $formError->getPropertyName() === $propertyName) {
index 8bf49fb..7e20aac 100644 (file)
@@ -120,9 +120,10 @@ class Tx_Fluid_ViewHelpers_FormViewHelper extends Tx_Fluid_ViewHelpers_Form_Abst
         * @param array $argumentsToBeExcludedFromQueryString arguments to be removed from the action URI. Only active if $addQueryString = TRUE and $actionUri is not set
         * @param string $fieldNamePrefix Prefix that will be added to all field names within this form. If not set the prefix will be tx_yourExtension_plugin
         * @param string $actionUri can be used to overwrite the "action" attribute of the form tag
+        * @param string $objectName name of the object that is bound to this form. If this argument is not specified, the name attribute of this form is used to determine the FormObjectName
         * @return string rendered form
         */
-       public function render($action = NULL, array $arguments = array(), $controller = NULL, $extensionName = NULL, $pluginName = NULL, $pageUid = NULL, $object = NULL, $pageType = 0, $noCache = FALSE, $noCacheHash = FALSE, $section = '', $format = '', array $additionalParams = array(), $absolute = FALSE, $addQueryString = FALSE, array $argumentsToBeExcludedFromQueryString = array(), $fieldNamePrefix = NULL, $actionUri = NULL) {
+       public function render($action = NULL, array $arguments = array(), $controller = NULL, $extensionName = NULL, $pluginName = NULL, $pageUid = NULL, $object = NULL, $pageType = 0, $noCache = FALSE, $noCacheHash = FALSE, $section = '', $format = '', array $additionalParams = array(), $absolute = FALSE, $addQueryString = FALSE, array $argumentsToBeExcludedFromQueryString = array(), $fieldNamePrefix = NULL, $actionUri = NULL, $objectName = NULL) {
                $this->setFormActionUri();
 
                if (strtolower($this->arguments['method']) === 'get') {
@@ -131,7 +132,7 @@ class Tx_Fluid_ViewHelpers_FormViewHelper extends Tx_Fluid_ViewHelpers_Form_Abst
                        $this->tag->addAttribute('method', 'post');
                }
 
-               $this->addFormNameToViewHelperVariableContainer();
+               $this->addFormObjectNameToViewHelperVariableContainer();
                $this->addFormObjectToViewHelperVariableContainer();
                $this->addFieldNamePrefixToViewHelperVariableContainer();
                $this->addFormFieldNamesToViewHelperVariableContainer();
@@ -150,7 +151,7 @@ class Tx_Fluid_ViewHelpers_FormViewHelper extends Tx_Fluid_ViewHelpers_Form_Abst
 
                $this->removeFieldNamePrefixFromViewHelperVariableContainer();
                $this->removeFormObjectFromViewHelperVariableContainer();
-               $this->removeFormNameFromViewHelperVariableContainer();
+               $this->removeFormObjectNameFromViewHelperVariableContainer();
                $this->removeFormFieldNamesFromViewHelperVariableContainer();
                $this->removeCheckboxFieldNamesFromViewHelperVariableContainer();
 
@@ -225,13 +226,14 @@ class Tx_Fluid_ViewHelpers_FormViewHelper extends Tx_Fluid_ViewHelpers_Form_Abst
        }
 
        /**
-        * Adds the form name to the ViewHelperVariableContainer if the name attribute is specified.
+        * Adds the form object name to the ViewHelperVariableContainer if "objectName" argument or "name" attribute is specified.
         *
         * @return void
         */
-       protected function addFormNameToViewHelperVariableContainer() {
-               if ($this->arguments->hasArgument('name')) {
-                       $this->viewHelperVariableContainer->add('Tx_Fluid_ViewHelpers_FormViewHelper', 'formName', $this->arguments['name']);
+       protected function addFormObjectNameToViewHelperVariableContainer() {
+               $formObjectName = $this->getFormObjectName();
+               if ($formObjectName !== NULL) {
+                       $this->viewHelperVariableContainer->add('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObjectName', $formObjectName);
                }
        }
 
@@ -240,13 +242,31 @@ class Tx_Fluid_ViewHelpers_FormViewHelper extends Tx_Fluid_ViewHelpers_Form_Abst
         *
         * @return void
         */
-       protected function removeFormNameFromViewHelperVariableContainer() {
-               if ($this->arguments->hasArgument('name')) {
-                       $this->viewHelperVariableContainer->remove('Tx_Fluid_ViewHelpers_FormViewHelper', 'formName');
+       protected function removeFormObjectNameFromViewHelperVariableContainer() {
+               $formObjectName = $this->getFormObjectName();
+               if ($formObjectName !== NULL) {
+                       $this->viewHelperVariableContainer->remove('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObjectName');
                }
        }
 
        /**
+        * Returns the name of the object that is bound to this form.
+        * If the "objectName" argument has been specified, this is returned. Otherwise the name attribute of this form.
+        * If neither objectName nor name arguments have been set, NULL is returned.
+        *
+        * @return string specified Form name or NULL if neither $objectName nor $name arguments have been specified
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       protected function getFormObjectName() {
+               $formObjectName = NULL;
+               if ($this->arguments->hasArgument('objectName')) {
+                       $formObjectName = $this->arguments['objectName'];
+               } elseif ($this->arguments->hasArgument('name')) {
+                       $formObjectName = $this->arguments['name'];
+               }
+               return $formObjectName;
+       }
+       /**
         * Adds the object that is bound to this form to the ViewHelperVariableContainer if the formObject attribute is specified.
         *
         * @return void
index 4a31043..87a6eb1 100644 (file)
@@ -36,7 +36,7 @@ class Tx_Fluid_ViewHelpers_FormViewHelperTest extends Tx_Fluid_ViewHelpers_ViewH
        public function renderAddsObjectToViewHelperVariableContainer() {
                $formObject = new stdClass();
 
-               $viewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_FormViewHelper', array('renderChildren', 'renderHiddenIdentityField', 'renderAdditionalIdentityFields', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormNameToViewHelperVariableContainer', 'addFieldNamePrefixToViewHelperVariableContainer', 'removeFormNameFromViewHelperVariableContainer', 'removeFieldNamePrefixFromViewHelperVariableContainer', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer'), array(), '', FALSE);
+               $viewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_FormViewHelper', array('renderChildren', 'renderHiddenIdentityField', 'renderAdditionalIdentityFields', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormObjectNameToViewHelperVariableContainer', 'addFieldNamePrefixToViewHelperVariableContainer', 'removeFormObjectNameFromViewHelperVariableContainer', 'removeFieldNamePrefixFromViewHelperVariableContainer', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer'), array(), '', FALSE);
                $this->injectDependenciesIntoViewHelper($viewHelper);
 
                $viewHelper->setArguments(new Tx_Fluid_Core_ViewHelper_Arguments(array('object' => $formObject)));
@@ -50,17 +50,35 @@ class Tx_Fluid_ViewHelpers_FormViewHelperTest extends Tx_Fluid_ViewHelpers_ViewH
        /**
         * @test
         * @author Sebastian Kurf├╝rst <sebastian@typo3.org>
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function renderAddsObjectNameToTemplateVariableContainer() {
+               $objectName = 'someObjectName';
+
+               $viewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_FormViewHelper', array('renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormObjectToViewHelperVariableContainer', 'addFieldNamePrefixToViewHelperVariableContainer', 'removeFormObjectFromViewHelperVariableContainer', 'removeFieldNamePrefixFromViewHelperVariableContainer', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer'), array(), '', FALSE);
+               $this->injectDependenciesIntoViewHelper($viewHelper);
+
+               $viewHelper->setArguments(new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => $objectName)));
+
+               $this->viewHelperVariableContainer->expects($this->once())->method('add')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObjectName', $objectName);
+               $this->viewHelperVariableContainer->expects($this->once())->method('remove')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObjectName');
+               $viewHelper->render();
+       }
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
         */
-       public function renderAddsFormNameToTemplateVariableContainer() {
-               $formName = 'someFormName';
+       public function formObjectNameArgumentOverrulesNameArgument() {
+               $objectName = 'someObjectName';
 
                $viewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_FormViewHelper', array('renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormObjectToViewHelperVariableContainer', 'addFieldNamePrefixToViewHelperVariableContainer', 'removeFormObjectFromViewHelperVariableContainer', 'removeFieldNamePrefixFromViewHelperVariableContainer', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer'), array(), '', FALSE);
                $this->injectDependenciesIntoViewHelper($viewHelper);
 
-               $viewHelper->setArguments(new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => $formName)));
+               $viewHelper->setArguments(new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => 'formName', 'objectName' => $objectName)));
 
-               $this->viewHelperVariableContainer->expects($this->once())->method('add')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formName', $formName);
-               $this->viewHelperVariableContainer->expects($this->once())->method('remove')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formName');
+               $this->viewHelperVariableContainer->expects($this->once())->method('add')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObjectName', $objectName);
+               $this->viewHelperVariableContainer->expects($this->once())->method('remove')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObjectName');
                $viewHelper->render();
        }