Skip to content

Commit

Permalink
Pass whole type hierarchy to model (#111)
Browse files Browse the repository at this point in the history
* Pass whole type hierarchy to model

* Fix array construction (PHP 5.3 compatibility)
  • Loading branch information
pepamartinec authored and 66Ton99 committed Jun 1, 2016
1 parent c6b9614 commit 32ba8cc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
19 changes: 17 additions & 2 deletions Factory/JsFormValidatorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Symfony\Component\Form\Extension\Core\DataTransformer\ChoiceToBooleanArrayTransformer;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\ResolvedFormTypeInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Mapping\ClassMetadata;
Expand Down Expand Up @@ -65,7 +67,7 @@ class JsFormValidatorFactory
/**
* @param ValidatorInterface $validator
* @param TranslatorInterface $translator
* @param \Symfony\Component\Routing\Generator\UrlGeneratorInterface $router
* @param UrlGeneratorInterface $router
* @param array $config
* @param string $domain
*/
Expand Down Expand Up @@ -244,7 +246,7 @@ public function createJsModel(Form $form)
$model = new JsFormElement;
$model->id = $this->getElementId($form);
$model->name = $form->getName();
$model->type = $conf->getType()->getInnerType()->getName();
$model->type = $this->getFormTypeHierarchy($conf->getType());
$model->invalidMessage = $this->translateMessage(
$conf->getOption('invalid_message'),
$conf->getOption('invalid_message_parameters')
Expand All @@ -264,6 +266,19 @@ public function createJsModel(Form $form)
return $model;
}

private function getFormTypeHierarchy(ResolvedFormTypeInterface $formType)
{
$type = array(
$formType->getName(),
);

while ($formType = $formType->getParent()) {
$type[] = $formType->getName();
}

return $type;
}

/**
* Create the JsFormElement for all the children of specified element
*
Expand Down
10 changes: 7 additions & 3 deletions Resources/public/js/FpJsFormValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,10 @@ var FpJsFormValidator = new function () {
this.customizeMethods = new FpJsCustomizeMethods();
this.constraintsCounter = 0;

function elementIsType(element, type) {
return element.type.indexOf(type) >= 0;
}

//noinspection JSUnusedGlobalSymbols
this.addModel = function (model, onLoad) {
var self = this;
Expand Down Expand Up @@ -517,7 +521,7 @@ var FpJsFormValidator = new function () {

this.checkParentCascadeOption = function (element) {
var result = true;
if (element.parent && !element.parent.cascade && 'collection' != element.parent.type) {
if (element.parent && !element.parent.cascade && !elementIsType(element.parent, 'collection')) {
result = false;
} else if (element.parent) {
result = this.checkParentCascadeOption(element.parent);
Expand Down Expand Up @@ -575,7 +579,7 @@ var FpJsFormValidator = new function () {

if (i && undefined === value) {
value = this.getMappedValue(element);
} else if ('collection' == element.type) {
} else if (elementIsType(element, 'collection')) {
value = {};
for (var childName in element.children) {
value[childName] = this.getMappedValue(element.children[childName]);
Expand Down Expand Up @@ -615,7 +619,7 @@ var FpJsFormValidator = new function () {
}

var value;
if ('checkbox' == element.type || 'radio' == element.type) {
if (elementIsType(element, 'checkbox') || elementIsType(element, 'radio')) {
value = element.domNode.checked;
} else if ('select' === element.domNode.tagName.toLowerCase()) {
value = [];
Expand Down

0 comments on commit 32ba8cc

Please sign in to comment.