Skip to content

Instantly share code, notes, and snippets.

@lorenzulrich
Last active August 16, 2019 08:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lorenzulrich/5b3930fad396c2de28a774580fa25917 to your computer and use it in GitHub Desktop.
Save lorenzulrich/5b3930fad396c2de28a774580fa25917 to your computer and use it in GitHub Desktop.
In order to use frontend validation in a Neos.FormBuilder form, the properties "required", "maxlength" as well as the validators in use need to be evaluated to generate the respective attributes for validation. This ViewHelper implements some common validators and generates `data-` attributes for ParsleyJS (http://parsleyjs.org/).
<?php
namespace Acme\Site\ViewHelpers\Form\FrontendValidation;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Validation\Validator\AbstractValidator;
use Neos\Flow\Validation\Validator\AlphanumericValidator;
use Neos\Flow\Validation\Validator\EmailAddressValidator;
use Neos\Flow\Validation\Validator\IntegerValidator;
use Neos\Flow\Validation\Validator\NotEmptyValidator;
use Neos\Flow\Validation\Validator\StringLengthValidator;
use Neos\Flow\Validation\Validator\TextValidator;
use Neos\FluidAdaptor\Core\ViewHelper\AbstractViewHelper;
use Neos\Form\Core\Model\Renderable\RenderableInterface;
use Neos\Utility\ObjectAccess;
class ElementConfigurationViewHelper extends AbstractViewHelper
{
/**
* @var boolean
*/
protected $escapeOutput = false;
/**
* Evaluates all validators and validation attributes in the Node properties added to a
* Form Element and generates an array containing all data attributes for configuring
* ParsleyJS accordingly
*
* Accepts an array with "additionalDataAttributes" that will be merged with the validator
* configuration
*
* Example:
* <f:form.textfield property="foo" id="foo" data="{vi:form.frontendValidation.elementConfiguration(element:element, additionalDataAttributes: {foo: 'bar', bar: 'baz'})}" />
*
*
* @param RenderableInterface $element
* @param array $additionalDataAttributes
* @return array
*/
public function render(RenderableInterface $element, array $additionalDataAttributes = [])
{
$validators = [];
$maxLength = ObjectAccess::getPropertyPath($element, 'properties.maxlength');
if ($maxLength) {
$validators['parsley-maxlength'] = $maxLength;
}
$required = ObjectAccess::getPropertyPath($element, 'properties.required');
if ($required) {
$validators['parsley-required'] = 'true';
}
/** @var AbstractValidator $validator */
foreach ($element->getValidators() as $validator) {
$validatorClassName = get_class($validator);
switch ($validatorClassName) {
case AlphanumericValidator::class:
$validators['parsley-type'] = 'alphanum';
break;
case EmailAddressValidator::class:
$validators['parsley-type'] = 'email';
break;
case IntegerValidator::class:
$validators['parsley-type'] = 'integer';
break;
case NotEmptyValidator::class:
$validators['parsley-required'] = 'true';
break;
case StringLengthValidator::class:
$minimum = ObjectAccess::getPropertyPath($validator, 'options.minimum');
$maximum = ObjectAccess::getPropertyPath($validator, 'options.maximum');
$validators['parsley-length'] = '[' . $minimum . ',' . $maximum . ']';
break;
case TextValidator::class:
$validators['parsley-pattern'] = '^[a-zA-Z]+$';
break;
}
}
// Merge the parsley data attributes with the additional data attributes
return array_merge($validators, $additionalDataAttributes);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment