0

我正在做一个项目,我需要将客户注册表单验证为客户类型。我想使用事件 keyup 进行输入。我也想更改默认消息。现在我正在做如下验证

$("#firstname").keyup(function(){
    $('input[name="firstname"]').validation();
    if(!$('input[name="firstname"]').validation('isValid')){
        $("#firstname-error").remove();
        $("#firstname").after('<div for="firstname" generated="true" class="mage-error" id="firstname-error">Please enter your firstname</div>');
    }else{
        $("#firstname-error").remove();
    }
});

我认为这不是一个好方法。但我需要对所有字段执行此操作。然后我在第 1735 行附近查看此文件 vendor\magento\magento2-base\lib\web\mage\validation.js 我看到了下面的代码

$.widget('mage.validation', {
        options: {
            meta: 'validate',
            onfocusout: false,
            onkeyup: false,
            onclick: false,
            ignoreTitle: true,
            errorClass: 'mage-error',
            errorElement: 'div',
    ...

看到这个我想也许有更好的方法来做到这一点。因此,如果有任何简单的方法,请告诉我。

4

1 回答 1

2

经过一番挖掘,我想出了这个解决方案。对于解决方案,您需要做的就是使用requirejs-config.js. 但我创建了一个新模块。模块文件如下。

app\code\Vky\Core\registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vky_Core',
    __DIR__
);

app\code\Vky\Core\etc\module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vky_Core" setup_version="0.0.1"/>
</config>

app\code\Vky\Core\view\frontend\requirejs-config.js

var config = {
    map: {
        '*': {
            vky_customjs:      'Vky_Core/js/vky_custom'
        }
    }
};

app\code\Vky\Core\view\frontend\web\js\vky_custom.js

define([
    "jquery",
    "jquery/ui",
    'mage/validation'
], function($) {
    "use strict";
    console.log('vky_custom.js is loaded!!');
        //creating jquery widget
        $.widget('vky_custom.js', {
            _create: function() {
                this._bind();
            },

            /**
             * Event binding, will monitor change, keyup and paste events.
             * @private
             */
            _bind: function () {
                this._on(this.element, {
                    'change': this.validateField,
                    'keyup': this.validateField,
                    'paste': this.validateField,
                    'click': this.validateField,
                    'focusout': this.validateField,
                    'focusin': this.validateField,
                });
            },

            validateField: function () {
                $.validator.validateSingleElement(this.element);
            },

        });

    return $.vky_custom.js;
});

现在,无论您的register.phtml文件在哪里打开它。如下添加一些东西。在文件末尾添加这个

<script type="text/x-magento-init">
    { ".v-validate": { "Vky_Core/js/vky_custom": {} } }
</script>

然后,例如,您要验证电子邮件。查找电子邮件的输入标签并添加类v-validate。像这样

<input type="email" name="email" autocomplete="email" id="email_address" value="<?= $block->escapeHtmlAttr($block->getFormData()->getEmail()) ?>" title="<?= $block->escapeHtmlAttr(__('Email')) ?>" class="input-text v-validate" data-validate="{required:true, 'validate-email':true}">

因此,任何带有类的输入v-validate都将在 keyup、change、click、focusout 等事件上得到验证。我为所有输入标签添加了一个类。

在这条线的上方,firstnamelastname添加了register.phtmlvar dataForm = $('#form-validate');

$('#firstname').addClass('v-validate');
$('#lastname').addClass('v-validate');

这就是我为解决我的问题所做的一切。它有效。这就是为什么发布我的答案。可能这可以帮助某人。

于 2018-05-02T16:21:02.050 回答