Validation Error with jQuery-UI Autocomplete and KnockoutJs - Magento 2 The 2019 Stack Overflow Developer Survey Results Are InJquery-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?
Why do UK politicians seemingly ignore opinion polls on Brexit?
Does light intensity oscillate really fast since it is a wave?
What do the Banks children have against barley water?
"To split hairs" vs "To be pedantic"
Evaluating number of iteration with a certain map with While
Geography at the pixel level
What is the steepest angle that a canal can be traversable without locks?
aging parents with no investments
What does "sndry explns" mean in one of the Hitchhiker's guide books?
Inline version of a function returns different value then non-inline version
Lethal sonic weapons
How long do I have to send payment?
Does duplicating a spell with wish count as casting that spell?
Families of ordered set partitions with disjoint blocks
How to deal with fear of taking dependencies
In microwave frequencies, do you use a circulator when you need a (near) perfect diode?
What is the meaning of Triage in Cybersec world?
Is three citations per paragraph excessive for undergraduate research paper?
Is it possible for the two major parties in the UK to form a coalition with each other instead of a much smaller party?
Should I write numbers in words or as symbols in this case?
Extreme, unacceptable situation and I can't attend work tomorrow morning
Is this food a bread or a loaf?
What do hard-Brexiteers want with respect to the Irish border?
Why isn't airport relocation done gradually?
Validation Error with jQuery-UI Autocomplete and KnockoutJs - Magento 2
The 2019 Stack Overflow Developer Survey Results Are InJquery-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") label
;
$(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
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") label
;
$(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
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") label
;
$(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") label
;
$(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
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