doulan9419 2014-05-10 07:10
浏览 57
已采纳

laravel ajax验证未显示所有字段

I am using jquery ajax validation. The problem is that only for some fields of the form is displayed validation, for others not.

jQuery(document).ready(function()
{
    jQuery('form#inline-validate').submit(function()
    {       
        jQuery.ajax({
            url: "http://localhost:8080/insur_docs/store",
            type: "post",
            data: jQuery('form#inline-validate').serialize(),
            datatype: "json",
            beforeSend: function()
            {
                jQuery('#ajax-loading').show();
                jQuery(".validation-error-inline").hide();
            }
            })
            .done(function(data)
            {
                if (data.validation_failed === 1)
                {
                    var arr = data.errors;
                    jQuery.each(arr, function(index, value)
                    {
                        if (value.length !== 0)
                        {
                            jQuery("#" + index).after('<span class="text-error validation-error-inline">' + value + '</span>');
                        }
                    });
                    jQuery('#ajax-loading').hide();
                }
            })
            .fail(function(jqXHR, ajaxOptions, thrownError)
            {
                  alert('No response from server');
            });
            return false;
    });
});

insur_docs_controller.blade.php

<div id="div-1" class="body">
            {{ Form::open(array('url' => 'insur_docs/store', 'class'=>'form-horizontal','id'=>'inline-validate')) }} 
            <div class="form-group">
                {{ Form::label('ownership_cert', 'Ownership Certificate*', array('class'=>'control-label col-lg-4')) }}
                <div class="col-lg-8">
                    {{ Form::select('ownership_cert', array('' => '', '1' => 'Yes', '0' => 'No'),Input::old('ownership_cert'),  array(
                                'class' => 'form-control'))
                    }}

                </div>
            </div>
            <div class="form-group">
                {{ Form::label('authoriz', 'Authorization*', array('class'=>'control-label col-lg-4')) }}            
                <div class="col-lg-8">
                    {{ Helpers\Helper::date('authoriz', Input::old('authoriz') , array(
                                'class' => 'form-control')) 
                    }}

                </div>
            </div>
            <div class="form-group">
                {{ Form::label('drive_permis', 'Drive Permission*', array('class'=>'control-label col-lg-4')) }}
                <div class="col-lg-8">
                    {{ Form::select('drive_permis', array('' => '', '1' => 'Active', '0' => 'Not active'),  Input::old('drive_permis'),  array(
                                'class' => 'form-control'))
                    }}

                </div>
            </div>
            <div class="form-group">
                {{ Form::label('sgs', 'SGS*', array('class'=>'control-label col-lg-4')) }}
                <div class="col-lg-8">
                    {{ Helpers\Helper::date('sgs',  Input::old('sgs') , array(
                                'class' => 'form-control')) 
                    }}

                </div>
            </div>  
            <div class="form-group">
                {{ Form::label('tpl', 'TPL*', array('class'=>'control-label col-lg-4')) }}
                <div class="col-lg-8">
                    {{ Helpers\Helper::date('tpl', Input::old('tpl') , array(
                                'class' => 'form-control')) 
                    }}

                </div>
            </div>
            <div class="form-group">
                {{ Form::label('kasko', 'Kasko*', array('class'=>'control-label col-lg-4')) }}
                <div class="col-lg-8">
                    {{ Helpers\Helper::date('kasko', Input::old('kasko') , array(
                                'class' => 'form-control')) 
                    }}

                </div>
            </div>
            <div class="form-group">
                {{ Form::label('inter_permis', 'International Permission*', array('class'=>'control-label col-lg-4')) }}
                <div class="col-lg-8">
                    {{ Helpers\Helper::date('inter_permis', Input::old('inter_permis') , array(
                                'class' => 'form-control')) 
                    }}

                </div>
            </div>
            <div class="form-group">
                {{ Form::label('car', 'Car*', array('class'=>'control-label col-lg-4')) }}
                <div class="col-lg-8">
                    {{ Form::select('car', $cars, Input::old('car'), array( 
                                'data-validation-error-msg' => 'You did not enter a valid car',
                                'class' => 'form-control')) 
                    }}<br/>
                    @if($errors->count() > 0)
                    <div class="alert alert-danger">
                        <p>The following errors have occurred:</p><br/>
                        @foreach($errors->all() as $message)
                        {{ Helpers\Helper::macro($message) }}<br/>
                        @endforeach
                    </div>
                    @endif                
                </div>
            </div>  

            {{ Form::submit('Save', array('class' => 'btn btn-success btn-line')) }}
            <input type="button" value="Back" class="btn btn-danger btn-line" onClick="history.go(-1);
                    return true;">
            {{ Form::close() }}
            {{ HTML::script('assets/js/jquery.validate-form.js') }}
        </div>

So validation is showing only for ownership_cert and drive permis. For other fields not. I have created a function for date which is :

  public static function date($name, $value = null, $options = array()) {
        $input = '<input type="date" name="' . $name . '" value="' . $value . '"';

        foreach ($options as $key => $value) {
            $input .= ' ' . $key . '="' . $value . '"';
        }

        $input .= '>';

        return $input;
    }

May the problem be with this?

  • 写回答

2条回答 默认 最新

  • doulan1073 2014-05-18 22:41
    关注

    Try this:

    jQuery(document).ready(function()
    {
        jQuery('form').submit(function()
        {
            var url = $(this).attr("action");
            jQuery.ajax({
                url: url,
                type: "post",
                data: jQuery('form').serialize(),
                datatype: "json",
                beforeSend: function()
                {
                    jQuery('#ajax-loading').show();
                    jQuery(".validation-error-inline").hide();
                }
            })
                    .done(function(data)
                    {
                        $('#validation-div').empty()
                        if (data.validation_failed === 1)
                        {
                            var arr = data.errors;
                            jQuery.each(arr, function(index, value)
                            {
                                if (value.length !== 0)
                                {
                                    $("#validation-div").addClass('alert alert-danger');
                                    document.getElementById("validation-div").innerHTML += '<span class="glyphicon glyphicon-warning-sign"></span>' + value + '<br/>';
                                }
                            });
                            jQuery('#ajax-loading').hide();
                        }
                        else {
                            window.location = data.redirect_to;
                        }
                    })
                    .fail(function(jqXHR, ajaxOptions, thrownError)
                    {
                        alert('No response from server');
                    });
            return false;
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题