From b7425b4f14074e35fc713d7995413a99d688f7a3 Mon Sep 17 00:00:00 2001 From: Jan Linhart Date: Thu, 23 Jul 2015 11:26:54 +0200 Subject: [PATCH] Multiple form on one page validation fix --- app/bundles/FormBundle/Model/FormModel.php | 5 +++-- .../FormBundle/Views/Builder/form.html.php | 4 ++-- .../FormBundle/Views/Field/button.html.php | 3 ++- .../FormBundle/Views/Field/captcha.html.php | 5 +++-- .../FormBundle/Views/Field/checkboxgrp.html.php | 5 +++-- .../FormBundle/Views/Field/country.html.php | 5 +++-- app/bundles/FormBundle/Views/Field/date.html.php | 5 +++-- app/bundles/FormBundle/Views/Field/email.html.php | 5 +++-- .../FormBundle/Views/Field/field_helper.php | 14 +++++++++----- .../FormBundle/Views/Field/freetext.html.php | 15 +++++++++------ app/bundles/FormBundle/Views/Field/group.html.php | 7 ++++--- .../FormBundle/Views/Field/hidden.html.php | 9 +++++---- .../FormBundle/Views/Field/number.html.php | 15 ++++++++------- .../FormBundle/Views/Field/radiogrp.html.php | 15 ++++++++------- .../FormBundle/Views/Field/select.html.php | 9 +++++---- app/bundles/FormBundle/Views/Field/tel.html.php | 15 ++++++++------- app/bundles/FormBundle/Views/Field/text.html.php | 2 +- .../FormBundle/Views/Field/textarea.html.php | 3 ++- app/bundles/FormBundle/Views/Field/url.html.php | 15 ++++++++------- media/js/mautic-form-src.js | 6 +++--- 20 files changed, 92 insertions(+), 70 deletions(-) diff --git a/app/bundles/FormBundle/Model/FormModel.php b/app/bundles/FormBundle/Model/FormModel.php index 062ca8dedc3..3248406b58b 100644 --- a/app/bundles/FormBundle/Model/FormModel.php +++ b/app/bundles/FormBundle/Model/FormModel.php @@ -446,16 +446,17 @@ public function getAutomaticJavascript(Form $form) public function populateValuesWithGetParameters($form, &$formHtml) { $request = $this->factory->getRequest(); + $formName = strtolower(\Mautic\CoreBundle\Helper\InputHelper::alphanum($form->getName())); $fields = $form->getFields(); foreach ($fields as $f) { $alias = $f->getAlias(); if ($request->query->has($alias)) { - preg_match('//i', $formHtml, $match); + preg_match('//i', $formHtml, $match); if (!empty($match)) { //replace value with GET - $replace = ''; + $replace = ''; $formHtml = str_replace($match[0], $replace, $formHtml); } } diff --git a/app/bundles/FormBundle/Views/Builder/form.html.php b/app/bundles/FormBundle/Views/Builder/form.html.php index 87a17d1f847..ee9a0d73671 100644 --- a/app/bundles/FormBundle/Views/Builder/form.html.php +++ b/app/bundles/FormBundle/Views/Builder/form.html.php @@ -30,7 +30,7 @@ $template = 'MauticFormBundle:Field:' . $f->getType() . '.html.php'; endif; - echo $view->render($theme.$template, array('field' => $f->convertToArray(), 'id' => $f->getAlias())); + echo $view->render($theme.$template, array('field' => $f->convertToArray(), 'id' => $f->getAlias(), 'formName' => $formName)); endforeach; ?> @@ -43,4 +43,4 @@ - \ No newline at end of file + diff --git a/app/bundles/FormBundle/Views/Field/button.html.php b/app/bundles/FormBundle/Views/Field/button.html.php index 1b247930a0f..6886a9395ac 100644 --- a/app/bundles/FormBundle/Views/Field/button.html.php +++ b/app/bundles/FormBundle/Views/Field/button.html.php @@ -17,6 +17,7 @@ 'deleted' => false, 'id' => $id, 'formId' => $formId, + 'formName' => $formName, 'disallowDelete' => true) ) : ''; @@ -29,4 +30,4 @@ HTML; -echo $html; \ No newline at end of file +echo $html; diff --git a/app/bundles/FormBundle/Views/Field/captcha.html.php b/app/bundles/FormBundle/Views/Field/captcha.html.php index 7a75d14cd19..cdc933b3e29 100644 --- a/app/bundles/FormBundle/Views/Field/captcha.html.php +++ b/app/bundles/FormBundle/Views/Field/captcha.html.php @@ -16,6 +16,7 @@ 'id' => $id, 'deleted' => (!empty($deleted)) ? true : false, 'required' => true, - 'formId' => (isset($formId)) ? $formId : 0 + 'formId' => (isset($formId)) ? $formId : 0, + 'formName' => (isset($formName)) ? $formName : '' ) -); \ No newline at end of file +); diff --git a/app/bundles/FormBundle/Views/Field/checkboxgrp.html.php b/app/bundles/FormBundle/Views/Field/checkboxgrp.html.php index 1affd4cb21a..3e2afa6a0fc 100644 --- a/app/bundles/FormBundle/Views/Field/checkboxgrp.html.php +++ b/app/bundles/FormBundle/Views/Field/checkboxgrp.html.php @@ -15,6 +15,7 @@ 'id' => $id, 'deleted' => (!empty($deleted)) ? true : false, 'formId' => (isset($formId)) ? $formId : 0, - 'type' => 'checkbox' + 'type' => 'checkbox', + 'formName' => (isset($formName)) ? $formName : '' ) -); \ No newline at end of file +); diff --git a/app/bundles/FormBundle/Views/Field/country.html.php b/app/bundles/FormBundle/Views/Field/country.html.php index 8eb3534bdc1..7650d700860 100644 --- a/app/bundles/FormBundle/Views/Field/country.html.php +++ b/app/bundles/FormBundle/Views/Field/country.html.php @@ -15,6 +15,7 @@ 'list' => \Symfony\Component\Intl\Intl::getRegionBundle()->getCountryNames(), 'id' => $id, 'deleted' => (!empty($deleted)) ? true : false, - 'formId' => (isset($formId)) ? $formId : 0 + 'formId' => (isset($formId)) ? $formId : 0, + 'formName' => (isset($formName)) ? $formName : '' ) -); \ No newline at end of file +); diff --git a/app/bundles/FormBundle/Views/Field/date.html.php b/app/bundles/FormBundle/Views/Field/date.html.php index 964b29aab8a..0945644c2cf 100644 --- a/app/bundles/FormBundle/Views/Field/date.html.php +++ b/app/bundles/FormBundle/Views/Field/date.html.php @@ -15,6 +15,7 @@ 'type' => 'date', 'id' => $id, 'deleted' => (!empty($deleted)) ? true : false, - 'formId' => (isset($formId)) ? $formId : 0 + 'formId' => (isset($formId)) ? $formId : 0, + 'formName' => (isset($formName)) ? $formName : '' ) -); \ No newline at end of file +); diff --git a/app/bundles/FormBundle/Views/Field/email.html.php b/app/bundles/FormBundle/Views/Field/email.html.php index 2513787dc5c..9921f25fc7c 100644 --- a/app/bundles/FormBundle/Views/Field/email.html.php +++ b/app/bundles/FormBundle/Views/Field/email.html.php @@ -15,6 +15,7 @@ 'type' => 'email', 'id' => $id, 'deleted' => (!empty($deleted)) ? true : false, - 'formId' => (isset($formId)) ? $formId : 0 + 'formId' => (isset($formId)) ? $formId : 0, + 'formName' => (isset($formName)) ? $formName : '' ) -); \ No newline at end of file +); diff --git a/app/bundles/FormBundle/Views/Field/field_helper.php b/app/bundles/FormBundle/Views/Field/field_helper.php index b261b538657..82f07c5b3f9 100644 --- a/app/bundles/FormBundle/Views/Field/field_helper.php +++ b/app/bundles/FormBundle/Views/Field/field_helper.php @@ -17,14 +17,18 @@ $defaultLabelClass = 'label'; } +if (!isset($formName)) { + $formName = ''; +} + $defaultInputClass = 'mauticform-'.$defaultInputClass; $defaultLabelClass = 'mauticform-'.$defaultLabelClass; $name = (empty($ignoreName)) ? ' name="mauticform['.$field['alias'].']"' : ''; -$value = (isset($field['defaultValue'])) ? ' value="'.$field['defaultValue'].'"' : ''; +$value = (isset($field['defaultValue'])) ? ' value="'.$field['defaultValue'].'"' : ' value=""'; if (empty($ignoreId)) { - $inputId = 'id="mauticform_input_'.$field['alias'].'"'; - $labelId = 'id="mauticform_label_'.$field['alias'].'" for="mauticform_input_'.$field['alias'].'"'; + $inputId = 'id="mauticform_input_'.$formName.'_'.$field['alias'].'"'; + $labelId = 'id="mauticform_label_'.$formName.'_'.$field['alias'].'" for="mauticform_input_'.$formName.'_'.$field['alias'].'"'; } else { $inputId = $labelId = ''; } @@ -63,7 +67,7 @@ } // Container -$containerAttr = 'id="mauticform_'.$id.'" '.htmlspecialchars_decode($field['containerAttributes']); +$containerAttr = 'id="mauticform_'.$formName.'_'.$id.'" '.htmlspecialchars_decode($field['containerAttributes']); if (!isset($containerClass)) $containerClass = $containerType; $defaultContainerClass = 'mauticform-row mauticform-'.$containerClass; @@ -86,4 +90,4 @@ $containerAttr .= ' class="'.$defaultContainerClass.'"'; } else { $containerAttr = str_ireplace('class="', 'class="'.$defaultContainerClass.' ', $containerAttr); -} \ No newline at end of file +} diff --git a/app/bundles/FormBundle/Views/Field/freetext.html.php b/app/bundles/FormBundle/Views/Field/freetext.html.php index 5d049f09bd0..b3196754e09 100644 --- a/app/bundles/FormBundle/Views/Field/freetext.html.php +++ b/app/bundles/FormBundle/Views/Field/freetext.html.php @@ -14,26 +14,29 @@ $formButtons = (!empty($inForm)) ? $view->render('MauticFormBundle:Builder:actions.html.php', array( - 'deleted' => (!empty($deleted)) ? $deleted : false, - 'id' => $id, - 'formId' => $formId + 'deleted' => (!empty($deleted)) ? $deleted : false, + 'id' => $id, + 'formId' => $formId, + 'formName' => $formName )) : ''; $label = (!$field['showLabel']) ? '' : <<{$view->escape($field['label'])} +

+ {$view->escape($field['label'])} +

HTML; $html = <<{$formButtons}{$label} -
+
$text
HTML; -echo $html; \ No newline at end of file +echo $html; diff --git a/app/bundles/FormBundle/Views/Field/group.html.php b/app/bundles/FormBundle/Views/Field/group.html.php index c7144903ef3..fbe6c65e111 100644 --- a/app/bundles/FormBundle/Views/Field/group.html.php +++ b/app/bundles/FormBundle/Views/Field/group.html.php @@ -33,9 +33,10 @@ $formButtons = (!empty($inForm)) ? $view->render('MauticFormBundle:Builder:actions.html.php', array( - 'deleted' => (!empty($deleted)) ? $deleted : false, - 'id' => $id, - 'formId' => $formId + 'deleted' => (!empty($deleted)) ? $deleted : false, + 'id' => $id, + 'formId' => $formId, + 'formName' => $formName )) : ''; $label = (!$field['showLabel']) ? '' : <<render('MauticFormBundle:Builder:actions.html.php', array( - 'deleted' => (!empty($deleted)) ? $deleted : false, - 'id' => $id, - 'formId' => $formId + 'deleted' => (!empty($deleted)) ? $deleted : false, + 'id' => $id, + 'formId' => $formId, + 'formName' => $formName )) : ''; if (!empty($inForm)): @@ -32,4 +33,4 @@ HTML; endif; -echo $html; \ No newline at end of file +echo $html; diff --git a/app/bundles/FormBundle/Views/Field/number.html.php b/app/bundles/FormBundle/Views/Field/number.html.php index 793461184f5..83fc27b2019 100644 --- a/app/bundles/FormBundle/Views/Field/number.html.php +++ b/app/bundles/FormBundle/Views/Field/number.html.php @@ -10,11 +10,12 @@ echo $view->render( 'MauticFormBundle:Field:text.html.php', array( - 'field' => $field, - 'inForm' => (isset($inForm)) ? $inForm : false, - 'type' => 'number', - 'id' => $id, - 'deleted' => (!empty($deleted)) ? true : false, - 'formId' => (isset($formId)) ? $formId : 0 + 'field' => $field, + 'inForm' => (isset($inForm)) ? $inForm : false, + 'type' => 'number', + 'id' => $id, + 'deleted' => (!empty($deleted)) ? true : false, + 'formId' => (isset($formId)) ? $formId : 0, + 'formName' => (isset($formName)) ? $formName : '' ) -); \ No newline at end of file +); diff --git a/app/bundles/FormBundle/Views/Field/radiogrp.html.php b/app/bundles/FormBundle/Views/Field/radiogrp.html.php index e7c94acbfb8..bd86444ffaf 100644 --- a/app/bundles/FormBundle/Views/Field/radiogrp.html.php +++ b/app/bundles/FormBundle/Views/Field/radiogrp.html.php @@ -10,11 +10,12 @@ echo $view->render( 'MauticFormBundle:Field:group.html.php', array( - 'field' => $field, - 'inForm' => (isset($inForm)) ? $inForm : false, - 'id' => $id, - 'deleted' => (!empty($deleted)) ? true : false, - 'formId' => (isset($formId)) ? $formId : 0, - 'type' => 'radio' + 'field' => $field, + 'inForm' => (isset($inForm)) ? $inForm : false, + 'id' => $id, + 'deleted' => (!empty($deleted)) ? true : false, + 'formId' => (isset($formId)) ? $formId : 0, + 'formName' => (isset($formName)) ? $formName : '', + 'type' => 'radio' ) -); \ No newline at end of file +); diff --git a/app/bundles/FormBundle/Views/Field/select.html.php b/app/bundles/FormBundle/Views/Field/select.html.php index e2dda5bce88..737fc4ef907 100644 --- a/app/bundles/FormBundle/Views/Field/select.html.php +++ b/app/bundles/FormBundle/Views/Field/select.html.php @@ -30,9 +30,10 @@ $formButtons = (!empty($inForm)) ? $view->render('MauticFormBundle:Builder:actions.html.php', array( - 'deleted' => (!empty($deleted)) ? $deleted : false, - 'id' => $id, - 'formId' => $formId + 'deleted' => (!empty($deleted)) ? $deleted : false, + 'id' => $id, + 'formId' => $formId, + 'formName' => $formName )) : ''; @@ -73,4 +74,4 @@ HTML; -echo $html; \ No newline at end of file +echo $html; diff --git a/app/bundles/FormBundle/Views/Field/tel.html.php b/app/bundles/FormBundle/Views/Field/tel.html.php index 755b695e495..a906653ca48 100644 --- a/app/bundles/FormBundle/Views/Field/tel.html.php +++ b/app/bundles/FormBundle/Views/Field/tel.html.php @@ -10,11 +10,12 @@ echo $view->render( 'MauticFormBundle:Field:text.html.php', array( - 'field' => $field, - 'inForm' => (isset($inForm)) ? $inForm : false, - 'type' => 'tel', - 'id' => $id, - 'deleted' => (!empty($deleted)) ? true : false, - 'formId' => (isset($formId)) ? $formId : 0 + 'field' => $field, + 'inForm' => (isset($inForm)) ? $inForm : false, + 'type' => 'tel', + 'id' => $id, + 'deleted' => (!empty($deleted)) ? true : false, + 'formId' => (isset($formId)) ? $formId : 0, + 'formName' => (isset($formName)) ? $formName : '' ) -); \ No newline at end of file +); diff --git a/app/bundles/FormBundle/Views/Field/text.html.php b/app/bundles/FormBundle/Views/Field/text.html.php index 721432d7485..0d6d825a895 100644 --- a/app/bundles/FormBundle/Views/Field/text.html.php +++ b/app/bundles/FormBundle/Views/Field/text.html.php @@ -53,4 +53,4 @@ HTML; -echo $html; \ No newline at end of file +echo $html; diff --git a/app/bundles/FormBundle/Views/Field/textarea.html.php b/app/bundles/FormBundle/Views/Field/textarea.html.php index f3218f76c05..669f021ae92 100644 --- a/app/bundles/FormBundle/Views/Field/textarea.html.php +++ b/app/bundles/FormBundle/Views/Field/textarea.html.php @@ -17,6 +17,7 @@ 'containerClass' => 'text', 'id' => $id, 'deleted' => (!empty($deleted)) ? true : false, - 'formId' => (isset($formId)) ? $formId : 0 + 'formId' => (isset($formId)) ? $formId : 0, + 'formName' => (isset($formName)) ? $formName : '' ) ); diff --git a/app/bundles/FormBundle/Views/Field/url.html.php b/app/bundles/FormBundle/Views/Field/url.html.php index 6a6a7c3840d..80f06e70f16 100644 --- a/app/bundles/FormBundle/Views/Field/url.html.php +++ b/app/bundles/FormBundle/Views/Field/url.html.php @@ -10,11 +10,12 @@ echo $view->render( 'MauticFormBundle:Field:text.html.php', array( - 'field' => $field, - 'inForm' => (isset($inForm)) ? $inForm : false, - 'type' => 'url', - 'id' => $id, - 'deleted' => (!empty($deleted)) ? true : false, - 'formId' => (isset($formId)) ? $formId : 0 + 'field' => $field, + 'inForm' => (isset($inForm)) ? $inForm : false, + 'type' => 'url', + 'id' => $id, + 'deleted' => (!empty($deleted)) ? true : false, + 'formId' => (isset($formId)) ? $formId : 0, + 'formName' => (isset($formName)) ? $formName : '', ) -); \ No newline at end of file +); diff --git a/media/js/mautic-form-src.js b/media/js/mautic-form-src.js index 7aed78b75be..2123ad17193 100644 --- a/media/js/mautic-form-src.js +++ b/media/js/mautic-form-src.js @@ -218,12 +218,12 @@ } if (!valid) { - validator.markError('mauticform_' + fieldKey, valid); + validator.markError('mauticform_' + formId + '_' + fieldKey, valid); formValid = false; validator.enableSubmitButton(); } else { - validator.clearError('mauticform_' + fieldKey); + validator.clearError('mauticform_' + formId + '_' + fieldKey); } } @@ -570,4 +570,4 @@ break; } } -})( window ); \ No newline at end of file +})( window );