Skip to content

Commit

Permalink
WIP: length validation
Browse files Browse the repository at this point in the history
  • Loading branch information
schmengler committed Jul 4, 2017
1 parent 570f412 commit 2541364
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 8 deletions.
16 changes: 16 additions & 0 deletions Model/OrderCommentConfigProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Bold\OrderComment\Model;

use Magento\Checkout\Model\ConfigProviderInterface;

class OrderCommentConfigProvider implements ConfigProviderInterface
{
public function getConfig()
{
return [
'max_length' => 360,
];
}

}
13 changes: 13 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<section id="sales">
<group id="ordercomments" translate="label" type="text" sortOrder="110" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Order Comment</label>
<field id="max_length" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">
<label>Maximum length in characters</label>
</field>
</group>
</section>
</system>
</config>
10 changes: 10 additions & 0 deletions etc/frontend/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Checkout\Model\CompositeConfigProvider">
<arguments>
<argument name="configProviders" xsi:type="array">
<item name="ordercomments_config_provider" xsi:type="object">Bold\OrderComment\Model\OrderCommentConfigProvider</item>
</argument>
</arguments>
</type>
</config>
5 changes: 5 additions & 0 deletions view/frontend/web/css/source/_module.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
._error {
.order-comment-form__characters {
color: @checkout-field-validation__border-color;
}
}
19 changes: 15 additions & 4 deletions view/frontend/web/js/model/checkout/order-comment-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ define(
'Magento_Checkout/js/model/quote',
'Magento_Checkout/js/model/url-builder',
'mage/url',
'Magento_Checkout/js/model/error-processor'
'Magento_Checkout/js/model/error-processor',
'Magento_Ui/js/model/messageList',
'mage/translate'
],
function ($, customer, quote, urlBuilder, urlFormatter, errorProcessor) {
function ($, customer, quote, urlBuilder, urlFormatter, errorProcessor, messageContainer, __) {
'use strict';

return {
Expand All @@ -21,9 +23,15 @@ define(
var isCustomer = customer.isLoggedIn();
var form = $('.payment-method input[name="payment[method]"]:checked').parents('.payment-method').find('form.order-comment-form');

var comment = form.find('.input-text.order-comment').val();
if (comment.length > this.getMaxLength()) {
messageContainer.addErrorMessage({ message: __("Comment is too long") });
return false;
}

var quoteId = quote.getQuoteId();
var url;

var url;
if (isCustomer) {
url = urlBuilder.createUrl('/carts/mine/set-order-comment', {})
} else {
Expand All @@ -33,7 +41,7 @@ define(
var payload = {
cartId: quoteId,
orderComment: {
comment: form.find('.input-text.order-comment').val()
comment: comment
}
};

Expand Down Expand Up @@ -62,6 +70,9 @@ define(
);

return result;
},
getMaxLength: function () {
return window.checkoutConfig.max_length;
}
};
}
Expand Down
16 changes: 14 additions & 2 deletions view/frontend/web/js/view/checkout/order-comment-block.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
define(
[
'jquery',
'uiComponent'
'uiComponent',
'knockout'
],
function ($, Component) {
function ($, Component, ko) {
'use strict';

return Component.extend({
defaults: {
template: 'Bold_OrderComment/checkout/order-comment-block'
},
initialize: function() {
this._super();
var self = this;
this.comment = ko.observable("");
this.remainingCharacters = ko.computed(function(){
return self.getMaxLength() - self.comment().length;
});
},
getMaxLength: function () {
return window.checkoutConfig.max_length;
}
});
}
Expand Down
5 changes: 3 additions & 2 deletions view/frontend/web/template/checkout/order-comment-block.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
<div class="payment-option-content" data-role="content">
<form class="form form-discount order-comment-form">
<div class="payment-option-inner">
<div class="field">
<div class="field" data-bind="css: { _error : remainingCharacters() < 0}">
<label class="label">
<span data-bind="i18n: 'Enter comment'"></span>
</label>
<div class="control">
<textarea class="input-text order-comment" name="comment-code" rows="4" data-bind="attr:{placeholder: $t('Enter your comment...')} " ></textarea>
<textarea class="input-text order-comment" name="comment-code" rows="4" data-bind="value:comment,valueUpdate: 'afterkeydown',attr:{placeholder: $t('Enter your comment...')} " ></textarea>
<p>Remaining characters: <span class="order-comment-form__characters" data-bind="text: remainingCharacters"></span></p>
</div>
</div>
</div>
Expand Down

0 comments on commit 2541364

Please sign in to comment.