0

最近、静的コンテンツの代わりに部分ビューを使用したかったので、JQuery ステップをカスタマイズする方法について質問しました。jquery-steps でサポートされている次のコードを使用して、この問題を部分的に解決しました。

   <h3>Step 1</h3>
      <section data-mode="async" data-url="/Formation/RenderStep1"></section>

   <h3>Step 2</h3>
      <section data-mode="async" data-url="/Formation/RenderStep2"></section>

今、私が直面している大きな問題は、目立たない検証をどのように使用するかです。私は JQuery カスタム検証を使用したくありません。Obtrusive を使用する何らかの方法が必要です。

レンダリングされる各部分ビューには、独自のフォームがあります。jquery-stepsのonStepChanging関数でフォームをバリデーションしたいのですが、

$("#my-steps").steps({
        headerTag: "h3",
        bodyTag: "section",
        contentMode: "async",
        transitionEffect: "fade",
        stepsOrientation: "vertical",

        onStepChanging: function (event, currentIndex, newIndex) {             
            return true;
        }
    });

$.validator.unobtrusvie.parse('#myform'); を呼び出してみました。onStepChanging 関数では ('#myform') は定義されていませんが、これが目立たない検証を手動で呼び出す正しい方法であるかどうかはまだわかりません。親切に私を導き、これを達成するための方向性を示してください。どんな助けでも大歓迎です。

4

2 に答える 2

2

プラグインを試してみたところ、formvalidationフォームタグなしでバリデーションを検索したり、フォームを送信せずにバリデーションを検索したりして、解決した問題を解決できます。

無料ではないことはわかっていますが、ここから試すことができます。個人的には気に入っています

検証後の最初の高さの更新

<style type="text/css">
    /* Adjust the height of section */
    #profileForm .content {
        min-height: 100px;
    }
    #profileForm .content > .body {
        width: 100%;
        height: auto;
        padding: 15px;
        position: relative;
    }

次に、data-stepsセクションにインデックスを追加します*

<form id="profileForm" method="post" class="form-horizontal">
   <h2>Account</h2>
   <section data-step="0">
    <div class="form-group">
        <label class="col-xs-3 control-label">Username</label>
        <div class="col-xs-5">
            <input type="text" class="form-control" name="username" />
        </div>
    </div>

    <div class="form-group">
        <label class="col-xs-3 control-label">Email</label>
        <div class="col-xs-5">
            <input type="text" class="form-control" name="email" />
        </div>
    </div>

    <div class="form-group">
        <label class="col-xs-3 control-label">Password</label>
        <div class="col-xs-5">
            <input type="password" class="form-control" name="password" />
        </div>
    </div>

    <div class="form-group">
        <label class="col-xs-3 control-label">Retype password</label>
        <div class="col-xs-5">
            <input type="password" class="form-control" name="confirmPassword" />
        </div>
    </div>
</section>

第三に、JavaScriptコード

<script>
## // to adjust step height to fit frame after showing validation messages##
$(document).ready(function() {
  function adjustIframeHeight() {
    var $body   = $('body'),
        $iframe = $body.data('iframe.fv');
    if ($iframe) {
        // Adjust the height of iframe
        $iframe.height($body.height());
    }
}

// IMPORTANT: You must call .steps() before calling .formValidation()
$('#profileForm')
  // setps setup
    .steps({
        headerTag: 'h2',
        bodyTag: 'section',
        onStepChanged: function(e, currentIndex, priorIndex) {
            // You don't need to care about it
            // It is for the specific demo
            adjustIframeHeight();
        },


        // Triggered when clicking the Previous/Next buttons
        // to apply validation to your section 

        onStepChanging: function(e, currentIndex, newIndex) {
            var fv         = $('#profileForm').data('formValidation'), // FormValidation instance
                // The current step container
                $container = $('#profileForm').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;
            }

            return true;
        },
        // Triggered when clicking the Finish button
        onFinishing: function(e, currentIndex) {
            var fv         = $('#profileForm').data('formValidation'),
                $container = $('#profileForm').find('section[data-step="' + currentIndex +'"]');

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

            var isValidStep = fv.isValidContainer($container);
            if (isValidStep === false || isValidStep === null) {
                return false;
            }

            return true;
        },
        onFinished: function(e, currentIndex) {
            // Uncomment the following line to submit the form using the defaultSubmit() method
            // $('#profileForm').formValidation('defaultSubmit');

            // For testing purpose
            $('#welcomeModal').modal();
        }
    })
    .formValidation({
        framework: 'bootstrap',
        icon: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        // This option will not ignore invisible fields which belong to inactive panels
        excluded: ':disabled',
        fields: {
            username: {
                validators: {
                    notEmpty: {

                     // for asp.net i used element attribute to integerated with unobtrusive validation 
                     // message :$('username').attr('data-val-required')

                        message: 'The username is required'
                    },
                    stringLength: {
                        min: 6,
                        max: 30,
                        message: 'The username must be more than 6 and less than 30 characters long'
                    },
                    regexp: {
                        regexp: /^[a-zA-Z0-9_\.]+$/,
                        message: 'The username can only consist of alphabetical, number, dot and underscore'
                    }
                }
            },
            email: {
                validators: {
                    notEmpty: {
                        message: 'The email address is required'
                    },
                    emailAddress: {
                        message: 'The input is not a valid email address'
                    }
                }
            },
            password: {
                validators: {
                    notEmpty: {
                        message: 'The password is required'
                    },
                    different: {
                        field: 'username',
                        message: 'The password cannot be the same as username'
                    }
                }
            },
            confirmPassword: {
                validators: {
                    notEmpty: {
                        message: 'The confirm password is required'
                    },
                    identical: {
                        field: 'password',
                        message: 'The confirm password must be the same as original one'
                    }
                }
            }
          }
    });
于 2016-01-04T20:39:05.537 に答える
1

JQuery Steps ライブラリ内で複数のフォームを管理しようとしているように思えますが、それが意図したものではないと思います。

JQuery ステップを構成するときは、ビュー内のフォームに対して設定します。

Unobtrusive JQuery Validation は、ビュー内のモデルを見て、エラー処理に関連するデータ属性を使用して HTML を自動的に構成します。

この検証は、クライアント側で自動的に起動する必要があります。

同じフォーム要素内にカプセル化されている限り、部分ビューを使用しても問題はありません。

各部分ビューを独自の形式でラップするための要件は何ですか? JQuery ステップ フォーム ウィザード全体で複数の投稿を作成しようとすると、オブジェクトが無効になります。

JQuery ステップ フォームの各ステップで、次のような 1 つのフォームのみを検証します。

onStepChanging: function (event, currentIndex, newIndex) {

                    //Allways allow user to move backwards.

                    if (currentIndex > newIndex) {
                        return true;
                    }

                    // Remove the validation errors from the next step, incase user has previously visited it.

                    var form = $(this);

                    if (currentIndex < newIndex) {

                        // remove error styles
                        $(".body:eq(" + newIndex + ") label.error", form).remove();
                        $(".body:eq(" + newIndex + ") .error", form).removeClass("error");
                    }

                    //disable validation on fields that are disabled or hidden.

                    form.validate().settings.ignore = ":disabled,:hidden";

                    return form.valid();
                }

ユーザーがデータの入力を終了し、クライアント側の検証が満たされると、onFinished メソッドにフックしてフォームを送信します:-

onFinished: function (event, currentIndex) {

                    var form = $(this);
                    form.submit();
                }

JQuery Steps の目的は、ユーザーがフォームにスムーズに入力できるようにし、多数の質問に圧倒されないようにすることです。

開発者の観点からは、画面間の進行状況を保存したり、フォーム データの状態を失ったりすることを心配することなく、フォームを適切なサイズのチャンクに分割でき、必要なデータをすべて取得するだけで済みます。すべての検証基準が満たされたら、その 1 つの投稿を作成します。

于 2015-06-12T15:39:00.430 に答える