Skip to content

Fixed issue regarding widget form validation #23499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
8c38c66
Fixed issue regarding widget form validation
yash-magedelight Jun 29, 2019
525de5c
The method _addField() has 104 lines of code.
yash-magedelight Jul 1, 2019
2a18d24
Added translation for newly added validation for A positive non-decim…
yash-magedelight Jul 1, 2019
e4480a1
modified comment for function getCssClasses
yash-magedelight Jul 1, 2019
d5200a8
Solved Code Sniffer issues
yash-magedelight Jul 1, 2019
73119c5
Solved Code Sniffer issues
yash-magedelight Jul 1, 2019
75b878f
Solved Code Sniffer issues
yash-magedelight Jul 1, 2019
4cfba53
Solved Code Sniffer issues
yash-magedelight Jul 1, 2019
520c17a
Solved Code Sniffer issues
yash-magedelight Jul 1, 2019
4805fed
Solved Code Sniffer issues
yash-magedelight Jul 1, 2019
2ad1570
Solved Code Sniffer issues
yash-magedelight Jul 1, 2019
0fba6ff
Solved Code Sniffer issues
yash-magedelight Jul 1, 2019
924d3d7
Solved Code Sniffer issues
yash-magedelight Jul 2, 2019
36698de
Solved Code Sniffer issues
yash-magedelight Jul 3, 2019
e521187
Solved Code Sniffer issues
yash-magedelight Aug 28, 2019
119456e
Solved Code Sniffer issues
yash-magedelight Sep 6, 2019
b98a262
Solved Code Sniffer issues
yash-magedelight Sep 6, 2019
9945c37
Strict types added
sidolov Sep 23, 2019
c22e210
Strict types added
sidolov Sep 23, 2019
91fc99f
Merge branch '2.4-develop' of http://github.com/magento/magento2 into…
engcom-Echo Feb 10, 2020
6dc7c6d
Changes covered MFTF test
engcom-Echo Feb 10, 2020
e7e57b9
Fix static
engcom-Echo Feb 25, 2020
83cb554
Merge branch '2.4-develop' into issue-23422
VladimirZaets Aug 3, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/code/Magento/CatalogWidget/etc/widget.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</depends>
<value>5</value>
</parameter>
<parameter name="products_count" xsi:type="text" required="true" visible="true">
<parameter name="products_count" xsi:type="text" required="true" visible="true" additional_classes="positive-integer">
<label translate="true">Number of Products to Display</label>
<value>10</value>
</parameter>
Expand Down
19 changes: 18 additions & 1 deletion app/code/Magento/Widget/Block/Adminhtml/Widget/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ protected function _addField($parameter)
'name' => $form->addSuffixToName($fieldName, 'parameters'),
'label' => __($parameter->getLabel()),
'required' => $parameter->getRequired(),
'class' => 'widget-option',
'class' => $this->getCssClasses($parameter),
'note' => __($parameter->getDescription()),
];

Expand Down Expand Up @@ -236,6 +236,23 @@ protected function _addField($parameter)
return $field;
}

/**
* Get css classes from xml to apply for validation and/or css styles
*
* @param \Magento\Framework\DataObject $parameter
* @return string
*/
private function getCssClasses($parameter)
{
$cssClass = 'widget-option';
$additionalClasses = $parameter->getAdditionalClasses();
if ($additionalClasses) {
$cssClass .= ' '.$additionalClasses;
}

return $cssClass;
}

/**
* Checks whether $fieldType is a class name of custom renderer, and not just a type of input element
*
Expand Down
156 changes: 110 additions & 46 deletions app/code/Magento/Widget/Model/Config/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,27 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

/**
* Widget Block Converter
*
* @author Magento Core Team <[email protected]>
*/
namespace Magento\Widget\Model\Config;

/**
* Widget Converter Model
*
* @since 100.0.2
*/
class Converter implements \Magento\Framework\Config\ConverterInterface
{
/**
* {@inheritdoc}
* Convert dom node to Magento array
*
* @param \DOMNode $source
* @return array
* @throws \LogicException
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
Expand All @@ -34,56 +49,99 @@ public function convert($source)
$widgetId = $widgetAttributes->getNamedItem('id');
/** @var $widgetSubNode \DOMNode */
foreach ($widget->childNodes as $widgetSubNode) {
switch ($widgetSubNode->nodeName) {
case 'label':
$widgetArray['name'] = $widgetSubNode->nodeValue;
break;
case 'description':
$widgetArray['description'] = $widgetSubNode->nodeValue;
break;
case 'parameters':
/** @var $parameter \DOMNode */
foreach ($widgetSubNode->childNodes as $parameter) {
if ($parameter->nodeName === '#text' || $parameter->nodeName === '#comment') {
continue;
}
$subNodeAttributes = $parameter->attributes;
$parameterName = $subNodeAttributes->getNamedItem('name')->nodeValue;
$widgetArray['parameters'][$parameterName] = $this->_convertParameter($parameter);
}
break;
case 'containers':
if (!isset($widgetArray['supported_containers'])) {
$widgetArray['supported_containers'] = [];
}
foreach ($widgetSubNode->childNodes as $container) {
if ($container->nodeName === '#text' || $container->nodeName === '#comment') {
continue;
}
$widgetArray['supported_containers'] = array_merge(
$widgetArray['supported_containers'],
$this->_convertContainer($container)
);
}
break;
case "#text":
break;
case '#comment':
break;
default:
throw new \LogicException(
sprintf(
"Unsupported child xml node '%s' found in the 'widget' node",
$widgetSubNode->nodeName
)
);
}
$widgetArray = $this->processWidgetSubNode($widgetSubNode, $widgetArray);
}
$widgets[$widgetId->nodeValue] = $widgetArray;
}
return $widgets;
}

/**
* Convert dom sub node to Magento array
*
* @param \DOMNode $widgetSubNode
* @param array $widgetArray
* @return array
* @throws \LogicException
*/
protected function processWidgetSubNode($widgetSubNode, $widgetArray)
{
switch ($widgetSubNode->nodeName) {
case 'label':
$widgetArray['name'] = $widgetSubNode->nodeValue;
break;
case 'description':
$widgetArray['description'] = $widgetSubNode->nodeValue;
break;
case 'parameters':
$widgetArray = $this->processParameters($widgetSubNode, $widgetArray);
break;
case 'containers':
$widgetArray = $this->processContainers($widgetSubNode, $widgetArray);
break;
case "#text":
break;
case '#comment':
break;
default:
throw new \LogicException(
sprintf(
"Unsupported child xml node '%s' found in the 'widget' node",
$widgetSubNode->nodeName
)
);
}

return $widgetArray;
}

/**
* Convert dom sub node to Magento array
*
* @param \DOMNode $widgetSubNode
* @param array $widgetArray
* @return array
*/
protected function processParameters($widgetSubNode, $widgetArray)
{
/** @var $parameter \DOMNode */
foreach ($widgetSubNode->childNodes as $parameter) {
if ($parameter->nodeName === '#text' || $parameter->nodeName === '#comment') {
continue;
}
$subNodeAttributes = $parameter->attributes;
$parameterName = $subNodeAttributes->getNamedItem('name')->nodeValue;
$widgetArray['parameters'][$parameterName] = $this->_convertParameter($parameter);
}

return $widgetArray;
}

/**
* Convert dom sub node to Magento array
*
* @param \DOMNode $widgetSubNode
* @param array $widgetArray
* @return array
*/
protected function processContainers($widgetSubNode, $widgetArray)
{
if (!isset($widgetArray['supported_containers'])) {
$widgetArray['supported_containers'] = [];
}
foreach ($widgetSubNode->childNodes as $container) {
if ($container->nodeName === '#text' || $container->nodeName === '#comment') {
continue;
}
$widgetArray['supported_containers'] = array_merge(
$widgetArray['supported_containers'],
$this->_convertContainer($container)
);
}

return $widgetArray;
}

/**
* Convert dom Container node to Magento array
*
Expand Down Expand Up @@ -208,6 +266,12 @@ protected function _convertParameter($source)
break;
}
}

$additional_classes = $sourceAttributes->getNamedItem('additional_classes');
if ($additional_classes) {
$parameter['additional_classes'] = $additional_classes->nodeValue;
}

return $parameter;
}

Expand Down Expand Up @@ -240,7 +304,7 @@ protected function _convertDepends($source)
];

continue;
} else if (!isset($depends[$dependencyName]['values'])) {
} elseif (!isset($depends[$dependencyName]['values'])) {
$depends[$dependencyName]['values'] = [$depends[$dependencyName]['value']];
unset($depends[$dependencyName]['value']);
}
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/Widget/etc/widget.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<xs:attribute name="visible" type="xs:boolean" />
<xs:attribute name="sort_order" type="xs:int" />
<xs:attribute name="required" type="xs:string" />
<xs:attribute name="additional_classes" type="xs:string" />
</xs:complexType>

<xs:complexType name="dependsParameterType">
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/Widget/etc/widget_file.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<xs:attribute name="visible" type="xs:boolean" />
<xs:attribute name="sort_order" type="xs:int" />
<xs:attribute name="required" type="xs:string" />
<xs:attribute name="additional_classes" type="xs:string" />
</xs:complexType>

<xs:complexType name="dependsParameterType">
Expand Down
1 change: 1 addition & 0 deletions lib/web/i18n/en_US.csv
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Submit,Submit
"No marginal white space please","No marginal white space please"
"Your ZIP-code must be in the range 902xx-xxxx to 905-xx-xxxx","Your ZIP-code must be in the range 902xx-xxxx to 905-xx-xxxx"
"A positive or negative non-decimal number please","A positive or negative non-decimal number please"
"A positive non-decimal number please","A positive non-decimal number please"
"The specified vehicle identification number (VIN) is invalid.","The specified vehicle identification number (VIN) is invalid."
"Please enter a correct date","Please enter a correct date"
"Please enter a valid time, between 00:00 and 23:59","Please enter a valid time, between 00:00 and 23:59"
Expand Down
6 changes: 6 additions & 0 deletions lib/web/mage/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,12 @@
},
$.mage.__('A positive or negative non-decimal number please')
],
'positive-integer': [
function (value, element) {
return this.optional(element) || /^\d+$/.test(value);
},
$.mage.__('A positive non-decimal number please')
],
'vinUS': [
function (v) {
var i, n, d, f, cd, cdv, LL, VL, FL, rs;
Expand Down