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;








1















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.



error



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:
styles



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
"/>









share|improve this question






























    1















    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.



    error



    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:
    styles



    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
    "/>









    share|improve this question


























      1












      1








      1








      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.



      error



      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:
      styles



      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
      "/>









      share|improve this question
















      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.



      error



      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:
      styles



      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 13 '17 at 12:55









      Community

      1




      1










      asked Feb 14 '17 at 15:15









      A. MartzA. Martz

      85415




      85415




















          1 Answer
          1






          active

          oldest

          votes


















          0














          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");





          share|improve this answer























            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
            );



            );













            draft saved

            draft discarded


















            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









            0














            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");





            share|improve this answer



























              0














              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");





              share|improve this answer

























                0












                0








                0







                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");





                share|improve this answer













                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");






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Feb 27 at 15:10









                Lukas KomarekLukas Komarek

                613




                613



























                    draft saved

                    draft discarded
















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Bulk add to cart function issuecart vs. mini cart issue … rwd themeRedirect Add to cart button to cart pageAdd to cart issue - Magento 2.1The requested Payment Method is not available When creating an orderM2: reason add-to-cart might not function in production modeAdd to cart issue in some android devicesMagento 2 - custom price can not add to subtotal and grand total after add to cartAdd to cart codeIssue with my cart module on pdp and cart pages, just keeps spinningBulk price and quantity update using rest api

                    БиармияSxpst500bh2ntaf! 3h2r