Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
[TASK] Prevent adding valid class to fields with empty values
Browse files Browse the repository at this point in the history
  • Loading branch information
romm committed Mar 23, 2017
1 parent b73c5e2 commit ffc7137
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
9 changes: 7 additions & 2 deletions Classes/ViewHelpers/ClassViewHelper.php
Expand Up @@ -241,10 +241,15 @@ protected function getPropertyValidClass(Result $propertyResult)
$formObject = $this->formService->getFormObject();
$field = $formObject->getConfiguration()->getField($this->fieldName);

if (false === $propertyResult->hasErrors()
if ($formObject->hasForm()
&& false === $propertyResult->hasErrors()
&& false === $formObject->getFormResult()->fieldIsDeactivated($field)
) {
$result .= ' ' . $this->classValue;
$fieldValue = ObjectAccess::getProperty($formObject->getForm(), $this->fieldName);

if (false === empty($fieldValue)) {
$result .= ' ' . $this->classValue;
}
}

return $result;
Expand Down
14 changes: 11 additions & 3 deletions Resources/Public/JavaScript/Field/Formz.Field.js
Expand Up @@ -494,9 +494,17 @@ Fz.Field = (function () {
Fz.addClass(element, className);
});
} else {
loopOnClasses('valid', function (element, className) {
Fz.addClass(element, className);
});
var value = states.field.getValue();

if (typeof value === 'string'
&& value !== ''
|| typeof value === 'object'
&& value.length > 0
) {
loopOnClasses('valid', function (element, className) {
Fz.addClass(element, className);
});
}
}
});

Expand Down
6 changes: 5 additions & 1 deletion Tests/Unit/ViewHelpers/ClassViewHelperTest.php
Expand Up @@ -8,6 +8,7 @@
use Romm\Formz\Exceptions\UnregisteredConfigurationException;
use Romm\Formz\Service\ViewHelper\FieldViewHelperService;
use Romm\Formz\Service\ViewHelper\FormViewHelperService;
use Romm\Formz\Tests\Fixture\Form\DefaultForm;
use Romm\Formz\ViewHelpers\ClassViewHelper;
use TYPO3\CMS\Extbase\Validation\Error;

Expand Down Expand Up @@ -206,8 +207,11 @@ protected function getDefaultFormService()
->setMethods(['getFormObject'])
->getMock();

$formObject = $this->getDefaultFormObject();
$service->method('getFormObject')
->willReturn($this->getDefaultFormObject());
->willReturn($formObject);

$formObject->setForm(new DefaultForm);

return $service;
}
Expand Down

0 comments on commit ffc7137

Please sign in to comment.