【问题标题】:Jquery Steps and FormValidation (re-validate)Jquery Steps 和 FormValidation(重新验证)
【发布时间】:2015-09-21 11:13:37
【问题描述】:

我正在使用 Jquery Steps 和 FormValidation,但是当 onStepChanged 时我无法重新验证字段。

    onStepChanging: function(e, currentIndex, newIndex) {        
        var fv = $('#form1').data('formValidation'), // FormValidation instance
        // The current step container
        $container = $('#form1').find('section[data-step="' + currentIndex +'"]');

        // Validate the container
        fv.validateContainer($container);

        var isValidStep = fv.isValidContainer($container);
        if (isValidStep === false || isValidStep === null) {
        // Do not jump to the next step
        return false;
    }

    if (currentIndex === 3) {  // form
        $.ajax({
            type: "POST",
            url: URL,
            data: $('#form1').serialize(),
            dataType: 'json',
            cache: false,
            async: false,
            success: function(data){

                    if (data.result === 'error') {

                        for (var field in data.fields) {
                            $('#'+field).val('');
                            $('#form1').formValidation('revalidateField', field)
                        }

                        $('#form1').steps("previous");
                        return;

                    } else {
                        console.log('Success');
                    }

                },

            error: function(x, status, error) {
                return false;
            },
        });         
    }
    return true;
}

输入为空但未重新验证!

我也尝试过 (updateStatus 和 validateField) 但没有成功!!怎么回事!

更新

JSFIDDLE:Demo

在第 3 步(确认)中,如果验证过程未通过,我将发布到服务器端以处理数据(产品库存、运输等),请返回第 1 步或第 2 步并重新输入数据。或者如果验证过程正常,则在第 3 步显示结果并允许用户完成(提交表单)。

php代码:

$result = "error";
$fields = array(
 'product' => 'Out of stock',
 'id' => 'ID not found',
);
$response = array('html'=>$html, 'result'=>$result, 'fields'=>$fields);
echo json_encode($response);

【问题讨论】:

  • 你能重现这个问题吗?
  • 可以添加HTML代码吗?

标签: jquery jquery-steps formvalidation-plugin


【解决方案1】:

您的代码中有一些错误:

1.

// Here, you should use `newIndex` instead of `curentIndex`
// Note that the index of the step goes from `0` to `count(steps) - 1`
if (newIndex === 2) {}

2。

// This
excluded: [':disabled', ':hidden', ':not(:visible)'],

// Should be =>
// You have to exclude only the disabled fields
// When you navigate between steps, the fields of the hidden steps
// will be hidden and not visible to Formvalidation plugin,
// thus you should include them.
excluded: [':disabled'],

# 工作示例: https://jsfiddle.net/Arkni/qtngbsy9/

【讨论】:

  • 是否可以转到步骤 1 而不是 $('#form').steps("previous") ??
  • @RednBlack 我认为文档中没有这样的选项,在源代码中挖掘了一下之后,我发现在主要时间是不可能的,请参阅this answer和@987654323 @
猜你喜欢
  • 1970-01-01
  • 2016-01-02
  • 2017-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多