Validation Error with jQuery-UI Autocomplete and KnockoutJs - Magento 2Jquery-Ui Autocomplete with KnockoutJs Magento 2Autocomplete form field using Knockout Js on Magento2 CheckoutMagento validation and jQuery reloadjQuery autocomplete select box - must press “Enter” KeyMagento 2 - jQuery validation custom error messageBlock with jQuery autocomplete in checkout pagejQuery-ui autocomplete: Cannot read property 'element' of undefinedJquery-Ui Autocomplete with KnockoutJs Magento 2onestepcheckout and chrome autocompleteMagento 2 Add new field to Magento_User admin formMagento 2.2.1: Add Custom Upload file attribute in CheckoutAutocomplete with ul li input in Magento2.2.5?
meaning of に in 本当に?
What typically incentivizes a professor to change jobs to a lower ranking university?
What does it mean to describe someone as a butt steak?
Is it possible to run Internet Explorer on OS X El Capitan?
How can bays and straits be determined in a procedurally generated map?
Accidentally leaked the solution to an assignment, what to do now? (I'm the prof)
Can you really stack all of this on an Opportunity Attack?
What's that red-plus icon near a text?
Did Shadowfax go to Valinor?
Alternative to sending password over mail?
Java Casting: Java 11 throws LambdaConversionException while 1.8 does not
How much RAM could one put in a typical 80386 setup?
How is it possible to have an ability score that is less than 3?
Modeling an IP Address
Horror movie about a virus at the prom; beginning and end are stylized as a cartoon
Why is Minecraft giving an OpenGL error?
Is it legal for company to use my work email to pretend I still work there?
How is the claim "I am in New York only if I am in America" the same as "If I am in New York, then I am in America?
Is it possible to do 50 km distance without any previous training?
Unable to deploy metadata from Partner Developer scratch org because of extra fields
Replacing matching entries in one column of a file by another column from a different file
Why is consensus so controversial in Britain?
What is the word for reserving something for yourself before others do?
Could an aircraft fly or hover using only jets of compressed air?
Validation Error with jQuery-UI Autocomplete and KnockoutJs - Magento 2
Jquery-Ui Autocomplete with KnockoutJs Magento 2Autocomplete form field using Knockout Js on Magento2 CheckoutMagento validation and jQuery reloadjQuery autocomplete select box - must press “Enter” KeyMagento 2 - jQuery validation custom error messageBlock with jQuery autocomplete in checkout pagejQuery-ui autocomplete: Cannot read property 'element' of undefinedJquery-Ui Autocomplete with KnockoutJs Magento 2onestepcheckout and chrome autocompleteMagento 2 Add new field to Magento_User admin formMagento 2.2.1: Add Custom Upload file attribute in CheckoutAutocomplete with ul li input in Magento2.2.5?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
The Problem:
I've implemented jQuery-ui Autocomplete with Knockout Js in Checkout Page but some input fields that are binded to my Component throw a validation error ("Required Field") when i update the input's value programmatically.
The Context:
I've asked already a question for the way of implementing the Autocomplete functionality but taking into account that the new problems are more specific i've posted this question. This is the link for the other one: Jquery-Ui Autocomplete with KnockoutJs Magento 2
The result 'till now is this:
What I've Done:
In Vendor/Module/Plugin/Checkout/Block/Checkout/LayoutProcessorPlugin.php
public function afterProcess(
MagentoCheckoutBlockCheckoutLayoutProcessor $subject,
array $jsLayout
)
$departmentField = [
'component' => 'Vendor_Module/js/shippingAutocomplete',
'config' => [
// customScope is used to group elements within a single form (e.g. they can be validated separately)
'customScope' => 'shippingAddress',
'template' => 'ui/form/field',
'elementTmpl' => 'Vendor_Module/autocomplete/shippingAddress/regionInput',
'id' => 'region',
'tooltip' => [
'description' => 'Select a Department',
],
],
'dataScope' => 'shippingAddress.region',
'label' => 'Department',
'provider' => 'checkoutProvider',
'sortOrder' => 51,
'validation' => [
'required-entry' => true
],
'options' => [],
'visible' => true,
];
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['shipping-address-fieldset']['children']['region'] = $departmentField;
$departmentIdField = [
'component' => 'Vendor_Module/js/shippingAutocomplete',
'config' => [
// customScope is used to group elements within a single form (e.g. they can be validated separately)
'customScope' => 'shippingAddress',
'template' => 'ui/form/field',
'elementTmpl' => 'Vendor_Module/autocomplete/regionIdInput',
'id' => 'region_id',
],
'dataScope' => 'shippingAddress.region_id',
'provider' => 'checkoutProvider',
'sortOrder' => 50,
'validation' => [
'required-entry' => true
],
'options' => [],
'visible' => true,
];
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['shipping-address-fieldset']['children']['region_id'] = $departmentIdField;
$cityField = [
'component' => 'Vendor_Module/js/shippingAutocomplete',
'config' => [
// customScope is used to group elements within a single form (e.g. they can be validated separately)
'customScope' => 'shippingAddress',
'template' => 'ui/form/field',
'elementTmpl' => 'Vendor_Module/autocomplete/shippingAddress/cityInput',
'id' => 'city',
'tooltip' => [
'description' => 'Select a City.',
],
],
'dataScope' => 'shippingAddress.city',
'label' => 'City',
'provider' => 'checkoutProvider',
'sortOrder' => 53,
'validation' => [
'required-entry' => true
],
'options' => [],
'visible' => true,
];
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['shipping-address-fieldset']['children']['city'] = $cityField;
$cityIdField = [
'component' => 'Vendor_Module/js/shippingAutocomplete',
'config' => [
// customScope is used to group elements within a single form (e.g. they can be validated separately)
'customScope' => 'shippingAddress',
'template' => 'ui/form/field',
'elementTmpl' => 'Vendor_Module/autocomplete/cityIdInput',
'id' => 'city_id',
],
'dataScope' => 'shippingAddress.city_id',
'provider' => 'checkoutProvider',
'sortOrder' => 52,
'validation' => [
'required-entry' => true
],
'options' => [],
'visible' => true,
];
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['shipping-address-fieldset']['children']['city_id'] = $cityIdField;
$postcodeField = [
'component' => 'Vendor_Module/js/shippingAutocomplete',
'config' => [
// customScope is used to group elements within a single form (e.g. they can be validated separately)
'customScope' => 'shippingAddress',
'template' => 'ui/form/field',
'elementTmpl' => 'Vendor_Module/autocomplete/postcodeInput',
'id' => 'postcode',
],
'dataScope' => 'shippingAddress.postcode',
'label' => 'Postal Code',
'provider' => 'checkoutProvider',
'sortOrder' => 54,
'validation' => [
'required-entry' => true
],
'options' => [],
'visible' => true,
];
return $jsLayout;
My Vendor/Module/view/frontend/web/js/shippingAutocomplete.js looks like this:
define([
'Magento_Ui/js/form/element/abstract',
'mage/url',
'ko',
'jquery',
'jquery/ui'
], function (Abstract, url, ko, $)
'use strict';
ko.bindingHandlers.shippingAutoComplete =
init: function (element, valueAccessor)
console.log("Init - Custom Binding Shipping");
console.log(element);
console.log(valueAccessor);
// valueAccessor = selected: mySelectedOptionObservable, options: myArrayOfLabelValuePairs
var settings = valueAccessor();
var selectedOption = settings.selected;
var options = settings.options;
var updateElementValueWithLabel = function (event, ui)
// Stop the default behavior
event.preventDefault();
// Update the value of the html element with the label
// of the activated option in the list (ui.item)
$(element).val(ui.item.label);
if (ui.item.type == 'city')
var cityId = $('[name="city_id"]');
cityId.val(ui.item.value);
var postcode = $('[name="region_id"]').val()+cityId.val();
$('[name="postcode"]').val(postcode);
else if (ui.item.type == 'department')
$('[name="region_id"]').val(ui.item.value);
// Update our SelectedOption observable
if(typeof ui.item !== "undefined")
// ui.item - id
;
$(element).autocomplete(
source: options,
select: function (event, ui)
updateElementValueWithLabel(event, ui);
);
;
return Abstract.extend(
selectedDepartment: ko.observable(''),
selectedCity: ko.observable(''),
postCode: ko.observable(''),
getDepartments: function( request, response )
$.ajax(
url: url.build('list/check/departments/'),
data: JSON.stringify(
q: request.term
),
contentType: "application/json",
type: "POST",
dataType: 'json',
error : function ()
alert("An error have occurred.");
,
success : function (data)
//Data is a string of the form: '["label": label, "value": value]'
var items = JSON.parse(data);
response( items );
);
,
getCities: function( request, response )
var departmentValue = $('[name="region"]').val();
$.ajax(
url: url.build('list/check/cities/'),
data: JSON.stringify(
q: request.term,
filter: departmentValue
),
contentType: "application/json",
type: "POST",
dataType: 'json',
error : function ()
alert("An error have occurred.");
,
success : function (data)
//Data is a string of the form: '["label": label, "value": value]'
var items = JSON.parse(data);
response( items );
);
);
);
The inputs are as follows:
In Vendor/Module/view/frontend/web/template/autocomplete/shippingAddress/regionInput.html
<input class="admin__control-text" type="text"
data-bind="
shippingAutoComplete:
selected: selectedDepartment,
options: getDepartments
,
event: change: userChanges,
value: value,
hasFocus: focused,
valueUpdate: valueUpdate,
attr:
name: inputName,
placeholder: placeholder,
'aria-describedby': noticeId,
id: uid,
disabled: disabled
"/>
In Vendor/Module/view/frontend/web/template/autocomplete/shippingAddress/cityInput.html
<input class="admin__control-text" type="text"
data-bind="
shippingAutoComplete:
selected: selectedCity,
options: getCities
,
event: change: userChanges,
value: value,
hasFocus: focused,
valueUpdate: valueUpdate,
attr:
name: inputName,
placeholder: placeholder,
'aria-describedby': noticeId,
id: uid,
disabled: disabled
"/>
In Vendor/Module/view/frontend/web/template/autocomplete/regionIdInput.html
<input class="admin__control-text"
data-bind="
value: value,
hasFocus: focused,
attr:
name: inputName,
placeholder: placeholder,
'aria-describedby': noticeId,
id: uid
"/>
In Vendor/Module/view/frontend/web/template/autocomplete/cityIdInput.html
<input class="admin__control-text"
data-bind="
value: value,
hasFocus: focused,
attr:
name: inputName,
placeholder: placeholder,
'aria-describedby': noticeId,
id: uid
"/>
In Vendor/Module/view/frontend/web/template/autocomplete/postcodeInput.html
<input class="admin__control-text" type="text"
data-bind="
event: change: userChanges,
value: value,
hasFocus: focused,
valueUpdate: valueUpdate,
attr:
name: inputName,
placeholder: placeholder,
'aria-describedby': noticeId,
id: uid,
disabled: disabled
"/>
magento2 checkout jquery knockoutjs autocomplete
bumped to the homepage by Community♦ 5 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
The Problem:
I've implemented jQuery-ui Autocomplete with Knockout Js in Checkout Page but some input fields that are binded to my Component throw a validation error ("Required Field") when i update the input's value programmatically.
The Context:
I've asked already a question for the way of implementing the Autocomplete functionality but taking into account that the new problems are more specific i've posted this question. This is the link for the other one: Jquery-Ui Autocomplete with KnockoutJs Magento 2
The result 'till now is this:
What I've Done:
In Vendor/Module/Plugin/Checkout/Block/Checkout/LayoutProcessorPlugin.php
public function afterProcess(
MagentoCheckoutBlockCheckoutLayoutProcessor $subject,
array $jsLayout
)
$departmentField = [
'component' => 'Vendor_Module/js/shippingAutocomplete',
'config' => [
// customScope is used to group elements within a single form (e.g. they can be validated separately)
'customScope' => 'shippingAddress',
'template' => 'ui/form/field',
'elementTmpl' => 'Vendor_Module/autocomplete/shippingAddress/regionInput',
'id' => 'region',
'tooltip' => [
'description' => 'Select a Department',
],
],
'dataScope' => 'shippingAddress.region',
'label' => 'Department',
'provider' => 'checkoutProvider',
'sortOrder' => 51,
'validation' => [
'required-entry' => true
],
'options' => [],
'visible' => true,
];
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['shipping-address-fieldset']['children']['region'] = $departmentField;
$departmentIdField = [
'component' => 'Vendor_Module/js/shippingAutocomplete',
'config' => [
// customScope is used to group elements within a single form (e.g. they can be validated separately)
'customScope' => 'shippingAddress',
'template' => 'ui/form/field',
'elementTmpl' => 'Vendor_Module/autocomplete/regionIdInput',
'id' => 'region_id',
],
'dataScope' => 'shippingAddress.region_id',
'provider' => 'checkoutProvider',
'sortOrder' => 50,
'validation' => [
'required-entry' => true
],
'options' => [],
'visible' => true,
];
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['shipping-address-fieldset']['children']['region_id'] = $departmentIdField;
$cityField = [
'component' => 'Vendor_Module/js/shippingAutocomplete',
'config' => [
// customScope is used to group elements within a single form (e.g. they can be validated separately)
'customScope' => 'shippingAddress',
'template' => 'ui/form/field',
'elementTmpl' => 'Vendor_Module/autocomplete/shippingAddress/cityInput',
'id' => 'city',
'tooltip' => [
'description' => 'Select a City.',
],
],
'dataScope' => 'shippingAddress.city',
'label' => 'City',
'provider' => 'checkoutProvider',
'sortOrder' => 53,
'validation' => [
'required-entry' => true
],
'options' => [],
'visible' => true,
];
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['shipping-address-fieldset']['children']['city'] = $cityField;
$cityIdField = [
'component' => 'Vendor_Module/js/shippingAutocomplete',
'config' => [
// customScope is used to group elements within a single form (e.g. they can be validated separately)
'customScope' => 'shippingAddress',
'template' => 'ui/form/field',
'elementTmpl' => 'Vendor_Module/autocomplete/cityIdInput',
'id' => 'city_id',
],
'dataScope' => 'shippingAddress.city_id',
'provider' => 'checkoutProvider',
'sortOrder' => 52,
'validation' => [
'required-entry' => true
],
'options' => [],
'visible' => true,
];
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['shipping-address-fieldset']['children']['city_id'] = $cityIdField;
$postcodeField = [
'component' => 'Vendor_Module/js/shippingAutocomplete',
'config' => [
// customScope is used to group elements within a single form (e.g. they can be validated separately)
'customScope' => 'shippingAddress',
'template' => 'ui/form/field',
'elementTmpl' => 'Vendor_Module/autocomplete/postcodeInput',
'id' => 'postcode',
],
'dataScope' => 'shippingAddress.postcode',
'label' => 'Postal Code',
'provider' => 'checkoutProvider',
'sortOrder' => 54,
'validation' => [
'required-entry' => true
],
'options' => [],
'visible' => true,
];
return $jsLayout;
My Vendor/Module/view/frontend/web/js/shippingAutocomplete.js looks like this:
define([
'Magento_Ui/js/form/element/abstract',
'mage/url',
'ko',
'jquery',
'jquery/ui'
], function (Abstract, url, ko, $)
'use strict';
ko.bindingHandlers.shippingAutoComplete =
init: function (element, valueAccessor)
console.log("Init - Custom Binding Shipping");
console.log(element);
console.log(valueAccessor);
// valueAccessor = selected: mySelectedOptionObservable, options: myArrayOfLabelValuePairs
var settings = valueAccessor();
var selectedOption = settings.selected;
var options = settings.options;
var updateElementValueWithLabel = function (event, ui)
// Stop the default behavior
event.preventDefault();
// Update the value of the html element with the label
// of the activated option in the list (ui.item)
$(element).val(ui.item.label);
if (ui.item.type == 'city')
var cityId = $('[name="city_id"]');
cityId.val(ui.item.value);
var postcode = $('[name="region_id"]').val()+cityId.val();
$('[name="postcode"]').val(postcode);
else if (ui.item.type == 'department')
$('[name="region_id"]').val(ui.item.value);
// Update our SelectedOption observable
if(typeof ui.item !== "undefined")
// ui.item - id
;
$(element).autocomplete(
source: options,
select: function (event, ui)
updateElementValueWithLabel(event, ui);
);
;
return Abstract.extend(
selectedDepartment: ko.observable(''),
selectedCity: ko.observable(''),
postCode: ko.observable(''),
getDepartments: function( request, response )
$.ajax(
url: url.build('list/check/departments/'),
data: JSON.stringify(
q: request.term
),
contentType: "application/json",
type: "POST",
dataType: 'json',
error : function ()
alert("An error have occurred.");
,
success : function (data)
//Data is a string of the form: '["label": label, "value": value]'
var items = JSON.parse(data);
response( items );
);
,
getCities: function( request, response )
var departmentValue = $('[name="region"]').val();
$.ajax(
url: url.build('list/check/cities/'),
data: JSON.stringify(
q: request.term,
filter: departmentValue
),
contentType: "application/json",
type: "POST",
dataType: 'json',
error : function ()
alert("An error have occurred.");
,
success : function (data)
//Data is a string of the form: '["label": label, "value": value]'
var items = JSON.parse(data);
response( items );
);
);
);
The inputs are as follows:
In Vendor/Module/view/frontend/web/template/autocomplete/shippingAddress/regionInput.html
<input class="admin__control-text" type="text"
data-bind="
shippingAutoComplete:
selected: selectedDepartment,
options: getDepartments
,
event: change: userChanges,
value: value,
hasFocus: focused,
valueUpdate: valueUpdate,
attr:
name: inputName,
placeholder: placeholder,
'aria-describedby': noticeId,
id: uid,
disabled: disabled
"/>
In Vendor/Module/view/frontend/web/template/autocomplete/shippingAddress/cityInput.html
<input class="admin__control-text" type="text"
data-bind="
shippingAutoComplete:
selected: selectedCity,
options: getCities
,
event: change: userChanges,
value: value,
hasFocus: focused,
valueUpdate: valueUpdate,
attr:
name: inputName,
placeholder: placeholder,
'aria-describedby': noticeId,
id: uid,
disabled: disabled
"/>
In Vendor/Module/view/frontend/web/template/autocomplete/regionIdInput.html
<input class="admin__control-text"
data-bind="
value: value,
hasFocus: focused,
attr:
name: inputName,
placeholder: placeholder,
'aria-describedby': noticeId,
id: uid
"/>
In Vendor/Module/view/frontend/web/template/autocomplete/cityIdInput.html
<input class="admin__control-text"
data-bind="
value: value,
hasFocus: focused,
attr:
name: inputName,
placeholder: placeholder,
'aria-describedby': noticeId,
id: uid
"/>
In Vendor/Module/view/frontend/web/template/autocomplete/postcodeInput.html
<input class="admin__control-text" type="text"
data-bind="
event: change: userChanges,
value: value,
hasFocus: focused,
valueUpdate: valueUpdate,
attr:
name: inputName,
placeholder: placeholder,
'aria-describedby': noticeId,
id: uid,
disabled: disabled
"/>
magento2 checkout jquery knockoutjs autocomplete
bumped to the homepage by Community♦ 5 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
The Problem:
I've implemented jQuery-ui Autocomplete with Knockout Js in Checkout Page but some input fields that are binded to my Component throw a validation error ("Required Field") when i update the input's value programmatically.
The Context:
I've asked already a question for the way of implementing the Autocomplete functionality but taking into account that the new problems are more specific i've posted this question. This is the link for the other one: Jquery-Ui Autocomplete with KnockoutJs Magento 2
The result 'till now is this:
What I've Done:
In Vendor/Module/Plugin/Checkout/Block/Checkout/LayoutProcessorPlugin.php
public function afterProcess(
MagentoCheckoutBlockCheckoutLayoutProcessor $subject,
array $jsLayout
)
$departmentField = [
'component' => 'Vendor_Module/js/shippingAutocomplete',
'config' => [
// customScope is used to group elements within a single form (e.g. they can be validated separately)
'customScope' => 'shippingAddress',
'template' => 'ui/form/field',
'elementTmpl' => 'Vendor_Module/autocomplete/shippingAddress/regionInput',
'id' => 'region',
'tooltip' => [
'description' => 'Select a Department',
],
],
'dataScope' => 'shippingAddress.region',
'label' => 'Department',
'provider' => 'checkoutProvider',
'sortOrder' => 51,
'validation' => [
'required-entry' => true
],
'options' => [],
'visible' => true,
];
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['shipping-address-fieldset']['children']['region'] = $departmentField;
$departmentIdField = [
'component' => 'Vendor_Module/js/shippingAutocomplete',
'config' => [
// customScope is used to group elements within a single form (e.g. they can be validated separately)
'customScope' => 'shippingAddress',
'template' => 'ui/form/field',
'elementTmpl' => 'Vendor_Module/autocomplete/regionIdInput',
'id' => 'region_id',
],
'dataScope' => 'shippingAddress.region_id',
'provider' => 'checkoutProvider',
'sortOrder' => 50,
'validation' => [
'required-entry' => true
],
'options' => [],
'visible' => true,
];
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['shipping-address-fieldset']['children']['region_id'] = $departmentIdField;
$cityField = [
'component' => 'Vendor_Module/js/shippingAutocomplete',
'config' => [
// customScope is used to group elements within a single form (e.g. they can be validated separately)
'customScope' => 'shippingAddress',
'template' => 'ui/form/field',
'elementTmpl' => 'Vendor_Module/autocomplete/shippingAddress/cityInput',
'id' => 'city',
'tooltip' => [
'description' => 'Select a City.',
],
],
'dataScope' => 'shippingAddress.city',
'label' => 'City',
'provider' => 'checkoutProvider',
'sortOrder' => 53,
'validation' => [
'required-entry' => true
],
'options' => [],
'visible' => true,
];
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['shipping-address-fieldset']['children']['city'] = $cityField;
$cityIdField = [
'component' => 'Vendor_Module/js/shippingAutocomplete',
'config' => [
// customScope is used to group elements within a single form (e.g. they can be validated separately)
'customScope' => 'shippingAddress',
'template' => 'ui/form/field',
'elementTmpl' => 'Vendor_Module/autocomplete/cityIdInput',
'id' => 'city_id',
],
'dataScope' => 'shippingAddress.city_id',
'provider' => 'checkoutProvider',
'sortOrder' => 52,
'validation' => [
'required-entry' => true
],
'options' => [],
'visible' => true,
];
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['shipping-address-fieldset']['children']['city_id'] = $cityIdField;
$postcodeField = [
'component' => 'Vendor_Module/js/shippingAutocomplete',
'config' => [
// customScope is used to group elements within a single form (e.g. they can be validated separately)
'customScope' => 'shippingAddress',
'template' => 'ui/form/field',
'elementTmpl' => 'Vendor_Module/autocomplete/postcodeInput',
'id' => 'postcode',
],
'dataScope' => 'shippingAddress.postcode',
'label' => 'Postal Code',
'provider' => 'checkoutProvider',
'sortOrder' => 54,
'validation' => [
'required-entry' => true
],
'options' => [],
'visible' => true,
];
return $jsLayout;
My Vendor/Module/view/frontend/web/js/shippingAutocomplete.js looks like this:
define([
'Magento_Ui/js/form/element/abstract',
'mage/url',
'ko',
'jquery',
'jquery/ui'
], function (Abstract, url, ko, $)
'use strict';
ko.bindingHandlers.shippingAutoComplete =
init: function (element, valueAccessor)
console.log("Init - Custom Binding Shipping");
console.log(element);
console.log(valueAccessor);
// valueAccessor = selected: mySelectedOptionObservable, options: myArrayOfLabelValuePairs
var settings = valueAccessor();
var selectedOption = settings.selected;
var options = settings.options;
var updateElementValueWithLabel = function (event, ui)
// Stop the default behavior
event.preventDefault();
// Update the value of the html element with the label
// of the activated option in the list (ui.item)
$(element).val(ui.item.label);
if (ui.item.type == 'city')
var cityId = $('[name="city_id"]');
cityId.val(ui.item.value);
var postcode = $('[name="region_id"]').val()+cityId.val();
$('[name="postcode"]').val(postcode);
else if (ui.item.type == 'department')
$('[name="region_id"]').val(ui.item.value);
// Update our SelectedOption observable
if(typeof ui.item !== "undefined")
// ui.item - id
;
$(element).autocomplete(
source: options,
select: function (event, ui)
updateElementValueWithLabel(event, ui);
);
;
return Abstract.extend(
selectedDepartment: ko.observable(''),
selectedCity: ko.observable(''),
postCode: ko.observable(''),
getDepartments: function( request, response )
$.ajax(
url: url.build('list/check/departments/'),
data: JSON.stringify(
q: request.term
),
contentType: "application/json",
type: "POST",
dataType: 'json',
error : function ()
alert("An error have occurred.");
,
success : function (data)
//Data is a string of the form: '["label": label, "value": value]'
var items = JSON.parse(data);
response( items );
);
,
getCities: function( request, response )
var departmentValue = $('[name="region"]').val();
$.ajax(
url: url.build('list/check/cities/'),
data: JSON.stringify(
q: request.term,
filter: departmentValue
),
contentType: "application/json",
type: "POST",
dataType: 'json',
error : function ()
alert("An error have occurred.");
,
success : function (data)
//Data is a string of the form: '["label": label, "value": value]'
var items = JSON.parse(data);
response( items );
);
);
);
The inputs are as follows:
In Vendor/Module/view/frontend/web/template/autocomplete/shippingAddress/regionInput.html
<input class="admin__control-text" type="text"
data-bind="
shippingAutoComplete:
selected: selectedDepartment,
options: getDepartments
,
event: change: userChanges,
value: value,
hasFocus: focused,
valueUpdate: valueUpdate,
attr:
name: inputName,
placeholder: placeholder,
'aria-describedby': noticeId,
id: uid,
disabled: disabled
"/>
In Vendor/Module/view/frontend/web/template/autocomplete/shippingAddress/cityInput.html
<input class="admin__control-text" type="text"
data-bind="
shippingAutoComplete:
selected: selectedCity,
options: getCities
,
event: change: userChanges,
value: value,
hasFocus: focused,
valueUpdate: valueUpdate,
attr:
name: inputName,
placeholder: placeholder,
'aria-describedby': noticeId,
id: uid,
disabled: disabled
"/>
In Vendor/Module/view/frontend/web/template/autocomplete/regionIdInput.html
<input class="admin__control-text"
data-bind="
value: value,
hasFocus: focused,
attr:
name: inputName,
placeholder: placeholder,
'aria-describedby': noticeId,
id: uid
"/>
In Vendor/Module/view/frontend/web/template/autocomplete/cityIdInput.html
<input class="admin__control-text"
data-bind="
value: value,
hasFocus: focused,
attr:
name: inputName,
placeholder: placeholder,
'aria-describedby': noticeId,
id: uid
"/>
In Vendor/Module/view/frontend/web/template/autocomplete/postcodeInput.html
<input class="admin__control-text" type="text"
data-bind="
event: change: userChanges,
value: value,
hasFocus: focused,
valueUpdate: valueUpdate,
attr:
name: inputName,
placeholder: placeholder,
'aria-describedby': noticeId,
id: uid,
disabled: disabled
"/>
magento2 checkout jquery knockoutjs autocomplete
The Problem:
I've implemented jQuery-ui Autocomplete with Knockout Js in Checkout Page but some input fields that are binded to my Component throw a validation error ("Required Field") when i update the input's value programmatically.
The Context:
I've asked already a question for the way of implementing the Autocomplete functionality but taking into account that the new problems are more specific i've posted this question. This is the link for the other one: Jquery-Ui Autocomplete with KnockoutJs Magento 2
The result 'till now is this:
What I've Done:
In Vendor/Module/Plugin/Checkout/Block/Checkout/LayoutProcessorPlugin.php
public function afterProcess(
MagentoCheckoutBlockCheckoutLayoutProcessor $subject,
array $jsLayout
)
$departmentField = [
'component' => 'Vendor_Module/js/shippingAutocomplete',
'config' => [
// customScope is used to group elements within a single form (e.g. they can be validated separately)
'customScope' => 'shippingAddress',
'template' => 'ui/form/field',
'elementTmpl' => 'Vendor_Module/autocomplete/shippingAddress/regionInput',
'id' => 'region',
'tooltip' => [
'description' => 'Select a Department',
],
],
'dataScope' => 'shippingAddress.region',
'label' => 'Department',
'provider' => 'checkoutProvider',
'sortOrder' => 51,
'validation' => [
'required-entry' => true
],
'options' => [],
'visible' => true,
];
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['shipping-address-fieldset']['children']['region'] = $departmentField;
$departmentIdField = [
'component' => 'Vendor_Module/js/shippingAutocomplete',
'config' => [
// customScope is used to group elements within a single form (e.g. they can be validated separately)
'customScope' => 'shippingAddress',
'template' => 'ui/form/field',
'elementTmpl' => 'Vendor_Module/autocomplete/regionIdInput',
'id' => 'region_id',
],
'dataScope' => 'shippingAddress.region_id',
'provider' => 'checkoutProvider',
'sortOrder' => 50,
'validation' => [
'required-entry' => true
],
'options' => [],
'visible' => true,
];
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['shipping-address-fieldset']['children']['region_id'] = $departmentIdField;
$cityField = [
'component' => 'Vendor_Module/js/shippingAutocomplete',
'config' => [
// customScope is used to group elements within a single form (e.g. they can be validated separately)
'customScope' => 'shippingAddress',
'template' => 'ui/form/field',
'elementTmpl' => 'Vendor_Module/autocomplete/shippingAddress/cityInput',
'id' => 'city',
'tooltip' => [
'description' => 'Select a City.',
],
],
'dataScope' => 'shippingAddress.city',
'label' => 'City',
'provider' => 'checkoutProvider',
'sortOrder' => 53,
'validation' => [
'required-entry' => true
],
'options' => [],
'visible' => true,
];
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['shipping-address-fieldset']['children']['city'] = $cityField;
$cityIdField = [
'component' => 'Vendor_Module/js/shippingAutocomplete',
'config' => [
// customScope is used to group elements within a single form (e.g. they can be validated separately)
'customScope' => 'shippingAddress',
'template' => 'ui/form/field',
'elementTmpl' => 'Vendor_Module/autocomplete/cityIdInput',
'id' => 'city_id',
],
'dataScope' => 'shippingAddress.city_id',
'provider' => 'checkoutProvider',
'sortOrder' => 52,
'validation' => [
'required-entry' => true
],
'options' => [],
'visible' => true,
];
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['shipping-address-fieldset']['children']['city_id'] = $cityIdField;
$postcodeField = [
'component' => 'Vendor_Module/js/shippingAutocomplete',
'config' => [
// customScope is used to group elements within a single form (e.g. they can be validated separately)
'customScope' => 'shippingAddress',
'template' => 'ui/form/field',
'elementTmpl' => 'Vendor_Module/autocomplete/postcodeInput',
'id' => 'postcode',
],
'dataScope' => 'shippingAddress.postcode',
'label' => 'Postal Code',
'provider' => 'checkoutProvider',
'sortOrder' => 54,
'validation' => [
'required-entry' => true
],
'options' => [],
'visible' => true,
];
return $jsLayout;
My Vendor/Module/view/frontend/web/js/shippingAutocomplete.js looks like this:
define([
'Magento_Ui/js/form/element/abstract',
'mage/url',
'ko',
'jquery',
'jquery/ui'
], function (Abstract, url, ko, $)
'use strict';
ko.bindingHandlers.shippingAutoComplete =
init: function (element, valueAccessor)
console.log("Init - Custom Binding Shipping");
console.log(element);
console.log(valueAccessor);
// valueAccessor = selected: mySelectedOptionObservable, options: myArrayOfLabelValuePairs
var settings = valueAccessor();
var selectedOption = settings.selected;
var options = settings.options;
var updateElementValueWithLabel = function (event, ui)
// Stop the default behavior
event.preventDefault();
// Update the value of the html element with the label
// of the activated option in the list (ui.item)
$(element).val(ui.item.label);
if (ui.item.type == 'city')
var cityId = $('[name="city_id"]');
cityId.val(ui.item.value);
var postcode = $('[name="region_id"]').val()+cityId.val();
$('[name="postcode"]').val(postcode);
else if (ui.item.type == 'department')
$('[name="region_id"]').val(ui.item.value);
// Update our SelectedOption observable
if(typeof ui.item !== "undefined")
// ui.item - id
;
$(element).autocomplete(
source: options,
select: function (event, ui)
updateElementValueWithLabel(event, ui);
);
;
return Abstract.extend(
selectedDepartment: ko.observable(''),
selectedCity: ko.observable(''),
postCode: ko.observable(''),
getDepartments: function( request, response )
$.ajax(
url: url.build('list/check/departments/'),
data: JSON.stringify(
q: request.term
),
contentType: "application/json",
type: "POST",
dataType: 'json',
error : function ()
alert("An error have occurred.");
,
success : function (data)
//Data is a string of the form: '["label": label, "value": value]'
var items = JSON.parse(data);
response( items );
);
,
getCities: function( request, response )
var departmentValue = $('[name="region"]').val();
$.ajax(
url: url.build('list/check/cities/'),
data: JSON.stringify(
q: request.term,
filter: departmentValue
),
contentType: "application/json",
type: "POST",
dataType: 'json',
error : function ()
alert("An error have occurred.");
,
success : function (data)
//Data is a string of the form: '["label": label, "value": value]'
var items = JSON.parse(data);
response( items );
);
);
);
The inputs are as follows:
In Vendor/Module/view/frontend/web/template/autocomplete/shippingAddress/regionInput.html
<input class="admin__control-text" type="text"
data-bind="
shippingAutoComplete:
selected: selectedDepartment,
options: getDepartments
,
event: change: userChanges,
value: value,
hasFocus: focused,
valueUpdate: valueUpdate,
attr:
name: inputName,
placeholder: placeholder,
'aria-describedby': noticeId,
id: uid,
disabled: disabled
"/>
In Vendor/Module/view/frontend/web/template/autocomplete/shippingAddress/cityInput.html
<input class="admin__control-text" type="text"
data-bind="
shippingAutoComplete:
selected: selectedCity,
options: getCities
,
event: change: userChanges,
value: value,
hasFocus: focused,
valueUpdate: valueUpdate,
attr:
name: inputName,
placeholder: placeholder,
'aria-describedby': noticeId,
id: uid,
disabled: disabled
"/>
In Vendor/Module/view/frontend/web/template/autocomplete/regionIdInput.html
<input class="admin__control-text"
data-bind="
value: value,
hasFocus: focused,
attr:
name: inputName,
placeholder: placeholder,
'aria-describedby': noticeId,
id: uid
"/>
In Vendor/Module/view/frontend/web/template/autocomplete/cityIdInput.html
<input class="admin__control-text"
data-bind="
value: value,
hasFocus: focused,
attr:
name: inputName,
placeholder: placeholder,
'aria-describedby': noticeId,
id: uid
"/>
In Vendor/Module/view/frontend/web/template/autocomplete/postcodeInput.html
<input class="admin__control-text" type="text"
data-bind="
event: change: userChanges,
value: value,
hasFocus: focused,
valueUpdate: valueUpdate,
attr:
name: inputName,
placeholder: placeholder,
'aria-describedby': noticeId,
id: uid,
disabled: disabled
"/>
magento2 checkout jquery knockoutjs autocomplete
magento2 checkout jquery knockoutjs autocomplete
edited Apr 13 '17 at 12:55
Community♦
1
1
asked Feb 14 '17 at 15:15
A. MartzA. Martz
85415
85415
bumped to the homepage by Community♦ 5 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 5 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Change directy the value of the knockout observable object from your custom input field.
Pass the observale in your data-bind attribute:
shippingAutoComplete:
selected: selectedCity,
options: getCities
value: value // this is your obersable you wanna change
,
Access it via
var settings = valueAccessor();
var inputValue= settings.value;
Change value
inputValue("yourValue");
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f159862%2fvalidation-error-with-jquery-ui-autocomplete-and-knockoutjs-magento-2%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Change directy the value of the knockout observable object from your custom input field.
Pass the observale in your data-bind attribute:
shippingAutoComplete:
selected: selectedCity,
options: getCities
value: value // this is your obersable you wanna change
,
Access it via
var settings = valueAccessor();
var inputValue= settings.value;
Change value
inputValue("yourValue");
add a comment |
Change directy the value of the knockout observable object from your custom input field.
Pass the observale in your data-bind attribute:
shippingAutoComplete:
selected: selectedCity,
options: getCities
value: value // this is your obersable you wanna change
,
Access it via
var settings = valueAccessor();
var inputValue= settings.value;
Change value
inputValue("yourValue");
add a comment |
Change directy the value of the knockout observable object from your custom input field.
Pass the observale in your data-bind attribute:
shippingAutoComplete:
selected: selectedCity,
options: getCities
value: value // this is your obersable you wanna change
,
Access it via
var settings = valueAccessor();
var inputValue= settings.value;
Change value
inputValue("yourValue");
Change directy the value of the knockout observable object from your custom input field.
Pass the observale in your data-bind attribute:
shippingAutoComplete:
selected: selectedCity,
options: getCities
value: value // this is your obersable you wanna change
,
Access it via
var settings = valueAccessor();
var inputValue= settings.value;
Change value
inputValue("yourValue");
answered Feb 27 at 15:10
Lukas KomarekLukas Komarek
613
613
add a comment |
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f159862%2fvalidation-error-with-jquery-ui-autocomplete-and-knockoutjs-magento-2%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown