Use form id to identification error container

[MAILPOET-3875]
This commit is contained in:
Jan Lysý
2021-12-02 14:09:04 +01:00
committed by Veljko V
parent 09c94577a9
commit f1d3135de0
10 changed files with 38 additions and 32 deletions

View File

@@ -26,7 +26,7 @@ class BlockRendererHelper {
$this->wp = $wp;
}
public function getInputValidation(array $block, array $extraRules = []): string {
public function getInputValidation(array $block, array $extraRules = [], ?int $formId = null): string {
$rules = [];
$blockId = $this->wp->escAttr($block['id']);
@@ -51,7 +51,7 @@ class BlockRendererHelper {
$rules['required'] = true;
$rules['mincheck'] = 1;
$rules['group'] = $blockId;
$rules['errors-container'] = '.mailpoet_error_' . $blockId;
$rules['errors-container'] = '.mailpoet_error_' . $blockId . ($formId ? '_' . $formId : '');
$rules['required-message'] = __('Please select a list.', 'mailpoet');
}
@@ -71,13 +71,13 @@ class BlockRendererHelper {
if (in_array($block['type'], ['radio', 'checkbox'])) {
$rules['group'] = 'custom_field_' . $blockId;
$rules['errors-container'] = '.mailpoet_error_' . $blockId;
$rules['errors-container'] = '.mailpoet_error_' . $blockId . ($formId ? '_' . $formId : '');
$rules['required-message'] = __('Please select at least one option.', 'mailpoet');
}
if ($block['type'] === 'date') {
$rules['group'] = 'custom_field_' . $blockId;
$rules['errors-container'] = '.mailpoet_error_' . $blockId;
$rules['errors-container'] = '.mailpoet_error_' . $blockId . ($formId ? '_' . $formId : '');
}
$validation = [];

View File

@@ -26,11 +26,11 @@ class Checkbox {
$this->wp = $wp;
}
public function render(array $block, array $formSettings): string {
public function render(array $block, array $formSettings, ?int $formId = null): string {
$html = '';
$fieldName = 'data[' . $this->rendererHelper->getFieldName($block) . ']';
$fieldValidation = $this->rendererHelper->getInputValidation($block);
$fieldValidation = $this->rendererHelper->getInputValidation($block, [], $formId);
$html .= $this->rendererHelper->renderLabel($block, $formSettings);
@@ -70,7 +70,7 @@ class Checkbox {
$html .= '</label>';
}
$html .= '<span class="mailpoet_error_' . $this->wp->escAttr($block['id']) . '"></span>';
$html .= '<span class="mailpoet_error_' . $this->wp->escAttr($block['id']) . ($formId ? '_' . $formId : '') . '"></span>';
return $this->wrapper->render($block, $html);
}

View File

@@ -33,14 +33,14 @@ class Date {
$this->wp = $wp;
}
public function render(array $block, array $formSettings): string {
public function render(array $block, array $formSettings, ?int $formId = null): string {
$html = '';
$html .= $this->rendererHelper->renderLabel($block, $formSettings);
$html .= $this->renderDateSelect($block, $formSettings);
$html .= $this->renderDateSelect($formId, $block, $formSettings);
return $this->wrapper->render($block, $html);
}
private function renderDateSelect(array $block = [], $formSettings = []): string {
private function renderDateSelect(?int $formId, array $block = [], $formSettings = []): string {
$html = '';
$fieldName = 'data[' . $this->rendererHelper->getFieldName($block) . ']';
@@ -65,7 +65,7 @@ class Date {
$html .= ' style="' . $this->wp->escAttr($this->blockStylesRenderer->renderForSelect([], $formSettings)) . '"';
$html .= $this->rendererHelper->getInputValidation($block, [
'required-message' => __('Please select a day', 'mailpoet'),
]);
], $formId);
$html .= 'name="' . $fieldName . '[day]" placeholder="' . __('Day', 'mailpoet') . '">';
$html .= $this->getDays($block);
$html .= '</select>';
@@ -74,7 +74,7 @@ class Date {
$html .= ' style="' . $this->wp->escAttr($this->blockStylesRenderer->renderForSelect([], $formSettings)) . '"';
$html .= $this->rendererHelper->getInputValidation($block, [
'required-message' => __('Please select a month', 'mailpoet'),
]);
], $formId);
$html .= 'name="' . $fieldName . '[month]" placeholder="' . __('Month', 'mailpoet') . '">';
$html .= $this->getMonths($block);
$html .= '</select>';
@@ -83,14 +83,14 @@ class Date {
$html .= ' style="' . $this->wp->escAttr($this->blockStylesRenderer->renderForSelect([], $formSettings)) . '"';
$html .= $this->rendererHelper->getInputValidation($block, [
'required-message' => __('Please select a year', 'mailpoet'),
]);
], $formId);
$html .= 'name="' . $fieldName . '[year]" placeholder="' . __('Year', 'mailpoet') . '">';
$html .= $this->getYears($block);
$html .= '</select>';
}
}
$html .= '<span class="mailpoet_error_' . $this->wp->escAttr($block['id']) . '"></span>';
$html .= '<span class="mailpoet_error_' . $this->wp->escAttr($block['id']) . ($formId ? '_' . $formId : '') . '"></span>';
return $html;
}

View File

@@ -26,11 +26,11 @@ class Radio {
$this->wp = $wp;
}
public function render(array $block, array $formSettings): string {
public function render(array $block, array $formSettings, ?int $formId = null): string {
$html = '';
$fieldName = 'data[' . $this->rendererHelper->getFieldName($block) . ']';
$fieldValidation = $this->rendererHelper->getInputValidation($block);
$fieldValidation = $this->rendererHelper->getInputValidation($block, [], $formId);
$html .= $this->rendererHelper->renderLabel($block, $formSettings);
@@ -73,7 +73,7 @@ class Radio {
$html .= '</label>';
}
$html .= '<span class="mailpoet_error_' . $block['id'] . '"></span>';
$html .= '<span class="mailpoet_error_' . $block['id'] . ($formId ? '_' . $formId : '') . '"></span>';
return $this->wrapper->render($block, $html);
}

View File

@@ -32,11 +32,11 @@ class Segment {
$this->segmentsRepository = $segmentsRepository;
}
public function render(array $block, array $formSettings): string {
public function render(array $block, array $formSettings, ?int $formId = null): string {
$html = '';
$fieldName = 'data[' . $this->rendererHelper->getFieldName($block) . ']';
$fieldValidation = $this->rendererHelper->getInputValidation($block);
$fieldValidation = $this->rendererHelper->getInputValidation($block, [], $formId);
$html .= $this->rendererHelper->renderLabel($block, $formSettings);
@@ -67,7 +67,7 @@ class Segment {
$html .= '</label>';
}
$html .= '<span class="mailpoet_error_' . $block['id'] . '"></span>';
$html .= '<span class="mailpoet_error_' . $block['id'] . ($formId ? '_' . $formId : '') . '"></span>';
return $this->wrapper->render($block, $html);
}

View File

@@ -99,7 +99,7 @@ class BlocksRenderer {
$this->paragraph = $paragraph;
}
public function renderBlock(array $block, array $formSettings): string {
public function renderBlock(array $block, array $formSettings, ?int $formId): string {
$html = '';
switch ($block['type']) {
case FormEntity::HTML_BLOCK_TYPE:
@@ -123,19 +123,19 @@ class BlocksRenderer {
break;
case FormEntity::CHECKBOX_BLOCK_TYPE:
$html .= $this->checkbox->render($block, $formSettings);
$html .= $this->checkbox->render($block, $formSettings, $formId);
break;
case FormEntity::RADIO_BLOCK_TYPE:
$html .= $this->radio->render($block, $formSettings);
$html .= $this->radio->render($block, $formSettings, $formId);
break;
case FormEntity::SEGMENT_SELECTION_BLOCK_TYPE:
$html .= $this->segment->render($block, $formSettings);
$html .= $this->segment->render($block, $formSettings, $formId);
break;
case FormEntity::DATE_BLOCK_TYPE:
$html .= $this->date->render($block, $formSettings);
$html .= $this->date->render($block, $formSettings, $formId);
break;
case FormEntity::SELECT_BLOCK_TYPE:

View File

@@ -44,7 +44,7 @@ class Renderer {
public function renderHTML(FormEntity $form = null): string {
if (($form instanceof FormEntity) && !empty($form->getBody()) && is_array($form->getSettings())) {
return $this->renderBlocks($form->getBody(), $form->getSettings() ?? []);
return $this->renderBlocks($form->getBody(), $form->getSettings() ?? [], $form->getId());
}
return '';
}
@@ -57,7 +57,13 @@ class Renderer {
}
}
public function renderBlocks(array $blocks = [], array $formSettings = [], bool $honeypotEnabled = true, bool $captchaEnabled = true): string {
public function renderBlocks(
array $blocks = [],
array $formSettings = [],
?int $formId = null,
bool $honeypotEnabled = true,
bool $captchaEnabled = true
): string {
// add honeypot for spambots
$html = ($honeypotEnabled) ? $this->renderHoneypot() : '';
foreach ($blocks as $key => $block) {
@@ -69,9 +75,9 @@ class Renderer {
}
if (in_array($block['type'], [FormEntity::COLUMN_BLOCK_TYPE, FormEntity::COLUMNS_BLOCK_TYPE])) {
$blocks = $block['body'] ?? [];
$html .= $this->blocksRenderer->renderContainerBlock($block, $this->renderBlocks($blocks, $formSettings, false)) . PHP_EOL;
$html .= $this->blocksRenderer->renderContainerBlock($block, $this->renderBlocks($blocks, $formSettings, $formId, false)) . PHP_EOL;
} else {
$html .= $this->blocksRenderer->renderBlock($block, $formSettings) . PHP_EOL;
$html .= $this->blocksRenderer->renderBlock($block, $formSettings, $formId) . PHP_EOL;
}
}
return $html;

View File

@@ -120,7 +120,7 @@ class CaptchaRenderer {
$formHtml .= '</p>';
// subscription form
$formHtml .= $this->formRenderer->renderBlocks($form, [], $honeypot = false);
$formHtml .= $this->formRenderer->renderBlocks($form, [], null, $honeypot = false);
$formHtml .= '</div>';
$formHtml .= $this->renderFormMessages($formModel, $showSuccessMessage, $showErrorMessage);
$formHtml .= '</form>';

View File

@@ -106,7 +106,7 @@ class ManageSubscriptionFormRenderer {
'email' => $subscriber->email,
'token' => $this->linkTokens->getToken($subscriberEntity),
'editEmailInfo' => __('Need to change your email address? Unsubscribe here, then simply sign up again.', 'mailpoet'),
'formHtml' => $this->formRenderer->renderBlocks($form, [], $honeypot = false, $captcha = false),
'formHtml' => $this->formRenderer->renderBlocks($form, [], null, $honeypot = false, $captcha = false),
'formState' => $formState,
];

View File

@@ -97,7 +97,7 @@ class RendererTest extends \MailPoetUnitTest {
->method('get')
->with('captcha.type')
->willReturn(Captcha::TYPE_DISABLED);
$html = $this->renderer->renderBlocks(Fixtures::get('simple_form_body'), [], false);
$html = $this->renderer->renderBlocks(Fixtures::get('simple_form_body'), [], null, false);
$hpLabel = $this->htmlParser->findByXpath($html, "//label[@class='mailpoet_hp_email_label']");
expect($hpLabel->length)->equals(0);
$recaptcha = $this->htmlParser->findByXpath($html, "//div[@class='mailpoet_recaptcha']");