Skip to content

Instantly share code, notes, and snippets.

Created May 21, 2012 13:50
Show Gist options
  • Save anonymous/2762421 to your computer and use it in GitHub Desktop.
Save anonymous/2762421 to your computer and use it in GitHub Desktop.
Buidling nested Subform part
{namespace vh=BLEICKER\Fluid\ViewHelpers}
<f:form name="party" objectName="updateParty" object="{account.party}">
<vh:nestedForm name="name" objectName="name" object="{account.party.name}">
<f:render partial="NameFieldset" />
</vh:nestedForm>
</f:form>
{namespace vh=BLEICKER\Fluid\ViewHelpers}
<f:form.textfield property="firstName" />
<vh:form.propertyValidationResults property="tax">
<f:render partial="Validation/PropertyErrors" arguments="{_all}" />
</vh:form.propertyValidationResults>
<?php
namespace BLEICKER\Fluid\ViewHelpers;
use TYPO3\FLOW3\Annotations as FLOW3;
/*
* This script belongs to the FLOW3 package "Fluid". *
* *
* It is free software; you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License, either version 3 *
* of the License, or (at your option) any later version. *
* *
* The TYPO3 project - inspiring people to share! *
* */
/**
* Render the inner parts of a nestedForm.
* This ViewHelper can only be used in a template which belongs to a parent form.
*
*/
class NestedFormViewHelper extends \TYPO3\Fluid\Core\ViewHelper\AbstractViewHelper {
/**
* @var \TYPO3\FLOW3\Persistence\PersistenceManagerInterface
* @FLOW3\Inject
*/
protected $persistenceManager;
/**
* Render the form content.
* @param string $name Name of the form
* @param array $arguments additional arguments
* @param mixed $object object to use for the form. Use in conjunction with the "property" attribute on the sub tags
* @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 nestedform
*/
public function render($name="", $arguments = array(), $object = NULL, $objectName = NULL) {
$this->storeFormObjectNameToViewHelperVariableContainer();
$this->addFormObjectNameToViewHelperVariableContainer();
$this->storeFormObjectToViewHelperVariableContainer();
$this->addFormObjectToViewHelperVariableContainer();
$formContent = $this->renderChildren();
$content = chr(10) . '<div style="display: none">';
$content .= $this->renderHiddenIdentityField($this->arguments['object'], $this->getFormObjectName());
$content .= $this->renderAdditionalIdentityFields();
$content .= chr(10) . '</div>' . chr(10);
$content .= $formContent;
$this->restoreFormObjectNameToViewHelperVariableContainer();
$this->restoreFormObjectToViewHelperVariableContainer();
return $content;
}
/**
* Prefixes / namespaces the given name with the form field prefix
*
* @param string $fieldName field name to be prefixed
* @return string namespaced field name
*/
protected function prefixFieldName($fieldName) {
if ($fieldName === NULL || $fieldName === '') {
return '';
}
if (!$this->viewHelperVariableContainer->exists('TYPO3\Fluid\ViewHelpers\FormViewHelper', 'fieldNamePrefix')) {
return $fieldName;
}
$fieldNamePrefix = (string)$this->viewHelperVariableContainer->get('TYPO3\Fluid\ViewHelpers\FormViewHelper', 'fieldNamePrefix');
if ($fieldNamePrefix === '') {
return $fieldName;
}
$fieldNameSegments = explode('[', $fieldName, 2);
$fieldName = $fieldNamePrefix . '[' . $fieldNameSegments[0] . ']';
if (count($fieldNameSegments) > 1) {
$fieldName .= '[' . $fieldNameSegments[1];
}
return $fieldName;
}
/**
* Renders a hidden form field containing the technical identity of the given object.
*
* @param object $object Object to create the identity field for
* @param string $name Name
* @return string A hidden field containing the Identity (UUID in FLOW3, uid in Extbase) of the given object or NULL if the object is unknown to the persistence framework
* @see \TYPO3\FLOW3\Mvc\Controller\Argument::setValue()
*/
protected function renderHiddenIdentityField($object, $name) {
if (!is_object($object) || $this->persistenceManager->isNewObject($object)) {
return '';
}
$identifier = $this->persistenceManager->getIdentifierByObject($object);
if ($identifier === NULL) {
return chr(10) . '<!-- Object of type ' . get_class($object) . ' is without identity -->' . chr(10);
}
$name = $this->prefixFieldName($name) . '[__identity]';
$this->registerFieldNameForFormTokenGeneration($name);
return chr(10) . '<input type="hidden" name="'. $name . '" value="' . $identifier .'" />' . chr(10);
}
/**
* Render additional identity fields which were registered by form elements.
* This happens if a form field is defined like property="bla.blubb" - then we might need an identity property for the sub-object "bla".
*
* @return string HTML-string for the additional identity properties
*/
protected function renderAdditionalIdentityFields() {
if ($this->viewHelperVariableContainer->exists('TYPO3\Fluid\ViewHelpers\FormViewHelper', 'additionalIdentityProperties')) {
$additionalIdentityProperties = $this->viewHelperVariableContainer->get('TYPO3\Fluid\ViewHelpers\FormViewHelper', 'additionalIdentityProperties');
$output = '';
foreach ($additionalIdentityProperties as $identity) {
$output .= chr(10) . $identity;
}
return $output;
}
return '';
}
/**
* Register a field name for inclusion in the HMAC / Form Token generation
*
* @param string $fieldName name of the field to register
* @return void
*/
protected function registerFieldNameForFormTokenGeneration($fieldName) {
if ($this->viewHelperVariableContainer->exists('TYPO3\Fluid\ViewHelpers\FormViewHelper', 'formFieldNames')) {
$formFieldNames = $this->viewHelperVariableContainer->get('TYPO3\Fluid\ViewHelpers\FormViewHelper', 'formFieldNames');
} else {
$formFieldNames = array();
}
$formFieldNames[] = $fieldName;
$this->viewHelperVariableContainer->addOrUpdate('TYPO3\Fluid\ViewHelpers\FormViewHelper', 'formFieldNames', $formFieldNames);
}
/**
* Adds the form object name to the ViewHelperVariableContainer if "objectName" argument or "name" attribute is specified.
*
* @return void
*/
private function storeFormObjectNameToViewHelperVariableContainer() {
try{
$storage = $this->viewHelperVariableContainer->get('TYPO3\Fluid\ViewHelpers\FormViewHelper', 'formObjectName');
$this->viewHelperVariableContainer->add('TYPO3\Fluid\ViewHelpers\FormViewHelperStorage', 'formObjectName', $storage);
}catch(\TYPO3\Fluid\Core\ViewHelper\Exception\InvalidVariableException $exception){}
}
/**
* Adds the form object name to the ViewHelperVariableContainer if "objectName" argument or "name" attribute is specified.
*
* @return void
*/
protected function restoreFormObjectNameToViewHelperVariableContainer() {
try{
$this->viewHelperVariableContainer->remove('TYPO3\Fluid\ViewHelpers\FormViewHelper', 'formObjectName');
$storage = $this->viewHelperVariableContainer->get('TYPO3\Fluid\ViewHelpers\FormViewHelperStorage', 'formObjectName');
$this->viewHelperVariableContainer->add('TYPO3\Fluid\ViewHelpers\FormViewHelper', 'formObjectName', $storage);
}catch(\TYPO3\Fluid\Core\ViewHelper\Exception\InvalidVariableException $exception){}
}
/**
* Adds the form object name to the ViewHelperVariableContainer if "objectName" argument or "name" attribute is specified.
*
* @return void
*/
protected function addFormObjectNameToViewHelperVariableContainer() {
try{
$this->viewHelperVariableContainer->remove('TYPO3\Fluid\ViewHelpers\FormViewHelper', 'formObjectName');
}catch(\TYPO3\Fluid\Core\ViewHelper\Exception\InvalidVariableException $exception){}
$formObjectName = $this->getFormObjectName();
if ($formObjectName !== NULL) {
$this->viewHelperVariableContainer->add('TYPO3\Fluid\ViewHelpers\FormViewHelper', 'formObjectName', $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
*/
protected function getFormObjectName() {
$formObjectName = NULL;
if ($this->hasArgument('objectName')) {
$formObjectName = $this->arguments['objectName'];
} elseif ($this->hasArgument('name')) {
$formObjectName = $this->arguments['name'];
}
try{
$parentFormObjectName = $this->viewHelperVariableContainer->get('TYPO3\Fluid\ViewHelpers\FormViewHelperStorage', 'formObjectName');
$formObjectName = $parentFormObjectName.'['.$formObjectName.']';
}catch(\TYPO3\Fluid\Core\ViewHelper\Exception\InvalidVariableException $exception){
}
return $formObjectName;
}
/**
* Adds the form object name to the ViewHelperVariableContainer if "objectName" argument or "name" attribute is specified.
*
* @return void
*/
private function storeFormObjectToViewHelperVariableContainer() {
try{
$storage = $this->viewHelperVariableContainer->get('TYPO3\Fluid\ViewHelpers\FormViewHelper', 'formObject');
$this->viewHelperVariableContainer->add('TYPO3\Fluid\ViewHelpers\FormViewHelperStorage', 'formObject', $storage);
$storage = $this->viewHelperVariableContainer->get('TYPO3\Fluid\ViewHelpers\FormViewHelper', 'additionalIdentityProperties');
$this->viewHelperVariableContainer->add('TYPO3\Fluid\ViewHelpers\FormViewHelperStorage', 'additionalIdentityProperties', $storage);
}catch(\TYPO3\Fluid\Core\ViewHelper\Exception\InvalidVariableException $exception){}
}
/**
* Adds the form object name to the ViewHelperVariableContainer if "objectName" argument or "name" attribute is specified.
*
* @return void
*/
protected function restoreFormObjectToViewHelperVariableContainer() {
try{
$this->viewHelperVariableContainer->remove('TYPO3\Fluid\ViewHelpers\FormViewHelper', 'formObject');
$storage = $this->viewHelperVariableContainer->get('TYPO3\Fluid\ViewHelpers\FormViewHelperStorage', 'formObject');
$this->viewHelperVariableContainer->add('TYPO3\Fluid\ViewHelpers\FormViewHelper', 'formObject', $storage);
$this->viewHelperVariableContainer->remove('TYPO3\Fluid\ViewHelpers\FormViewHelper', 'additionalIdentityProperties');
$storage = $this->viewHelperVariableContainer->get('TYPO3\Fluid\ViewHelpers\FormViewHelperStorage', 'additionalIdentityProperties');
$this->viewHelperVariableContainer->add('TYPO3\Fluid\ViewHelpers\FormViewHelper', 'additionalIdentityProperties', $storage);
}catch(\TYPO3\Fluid\Core\ViewHelper\Exception\InvalidVariableException $exception){}
}
/**
* Adds the object that is bound to this form to the ViewHelperVariableContainer if the formObject attribute is specified.
*
* @return void
*/
protected function addFormObjectToViewHelperVariableContainer() {
try{
$this->viewHelperVariableContainer->remove('TYPO3\Fluid\ViewHelpers\FormViewHelper', 'formObject');
$this->viewHelperVariableContainer->remove('TYPO3\Fluid\ViewHelpers\FormViewHelper', 'additionalIdentityProperties');
}catch(\TYPO3\Fluid\Core\ViewHelper\Exception\InvalidVariableException $exception){}
if ($this->hasArgument('object')) {
$this->viewHelperVariableContainer->add('TYPO3\Fluid\ViewHelpers\FormViewHelper', 'formObject', $this->arguments['object']);
$this->viewHelperVariableContainer->add('TYPO3\Fluid\ViewHelpers\FormViewHelper', 'additionalIdentityProperties', array());
}
}
}
?>
<?php
namespace BLEICKER\Fluid\ViewHelpers\Form;
use TYPO3\FLOW3\Annotations as FLOW3;
/* *
* This script belongs to the FLOW3 package "Fluid". *
* *
* It is free software; you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License, either version 3 *
* of the License, or (at your option) any later version. *
* *
* The TYPO3 project - inspiring people to share! *
* */
/**
* Property Validation results view helper
*/
class PropertyValidationResultsViewHelper extends \TYPO3\Fluid\Core\ViewHelper\AbstractViewHelper {
/**
* Initialize arguments.
*
* @return void
* @api
*/
public function initializeArguments() {
parent::initializeArguments();
$this->registerArgument('property', 'string', 'Name of Object Property', TRUE);
$this->registerArgument('disablePrefix', 'boolean', 'Do not add prefixes automaticaly to Property', FALSE, FALSE);
}
/**
* Iterates through selected errors of the request.
*
* @param string $as
* @return string Rendered string
*/
public function render($as = 'validationResults') {
$for = $this->getPropertyPath();
$validationResults = $this->controllerContext->getRequest()->getInternalArgument('__submittedArgumentValidationResults');
if ($validationResults === NULL) {
return '';
}
if ($for !== '') {
$validationResults = $validationResults->forProperty($for);
}
$this->templateVariableContainer->add($as, $validationResults);
$output = $this->renderChildren();
$this->templateVariableContainer->remove($as);
return $output;
}
/**
* Get the name of this form element.
* Either returns arguments['name'], or the correct name for Object Access.
*
* In case property is something like bla.blubb (hierarchical), then [bla][blubb] is generated.
*
* @return string Name
*/
protected function getPropertyPath(){
if($this->arguments['disablePrefix'])
return $this->arguments['property'];
$name = $this->getPropertyPathWithoutPrefix();
return $this->prefixPropertyPath($name);
}
/**
* Get the name of this form element, without prefix.
*
* @return string name
*/
protected function getPropertyPathWithoutPrefix() {
$propertySegments = explode('.', $this->arguments['property']);
$formObjectName = $this->viewHelperVariableContainer->get('TYPO3\Fluid\ViewHelpers\FormViewHelper', 'formObjectName');
$formObjectName = rtrim(preg_replace('/(\]\[|\[|\])/', '.', $formObjectName), '.');
if (!empty($formObjectName)) {
array_unshift($propertySegments, $formObjectName);
}
$name = array_shift($propertySegments);
foreach ($propertySegments as $segment) {
$name .= '.'.$segment;
}
return $name;
}
/**
* Prefixes / namespaces the given name with the form field prefix
*
* @param string $fieldName field name to be prefixed
* @return string namespaced field name
*/
protected function prefixPropertyPath($fieldName) {
return $fieldName;
if ($fieldName === NULL || $fieldName === '') {
return '';
}
if (!$this->viewHelperVariableContainer->exists('TYPO3\Fluid\ViewHelpers\FormViewHelper', 'fieldNamePrefix')) {
return $fieldName;
}
$fieldNamePrefix = (string)$this->viewHelperVariableContainer->get('TYPO3\Fluid\ViewHelpers\FormViewHelper', 'fieldNamePrefix');
if ($fieldNamePrefix === '') {
return $fieldName;
}
$fieldNameSegments = explode('.', $fieldName, 2);
$fieldName = $fieldNamePrefix . '.' . $fieldNameSegments[0];
if (count($fieldNameSegments) > 1) {
$fieldName .= '.' . $fieldNameSegments[1];
}
return $fieldName;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment