footer.blade.php 12.5 KB
<div class="card-footer" style="height: 50px!important;">

    {{ csrf_field() }}

{{--    <div class="col-md-{{$width['label']}} ">
    </div>--}}
    <div class="col-md-1">
    </div>

    <div class="col-md-10 col-md-{{$width['field']}}">

        <div class="d-flex d-sm-inline-flex align-items-center">
            @if(in_array('submit', $buttons))
                <button type="submit" class="btn btn-primary" {{--onclick="submitForm()"--}}>{{ trans('admin.submit') }} </button>
            @endif
            @if(in_array('reset', $buttons))
                <button type="reset" class="btn btn-warning mr-3">{{ trans('admin.reset') }}</button>
            @endif
        </div>
        <div class="d-flex d-sm-inline-flex align-items-center">
            @if(in_array('view', $checkboxes))
                <div class="custom-control custom-radio custom-control-inline">
                    <input type="radio" name="after-save" value="3" id="after-save3" class="custom-control-input" />
                    <label class="custom-control-label" for="after-save3">{{ trans('admin.view') }}</label>
                </div>
            @endif
            @if(in_array('continue_editing', $checkboxes))
                <div class="custom-control custom-radio custom-control-inline">
                    <input type="radio" name="after-save" value="1" id="after-save1" class="custom-control-input" />
                    <label class="custom-control-label" for="after-save1">{{ trans('admin.continue_editing') }}</label>
                </div>
            @endif
            @if(in_array('continue_creating', $checkboxes))
                <div class="custom-control custom-radio custom-control-inline">
                    <input type="radio" name="after-save" value="2" id="after-save2" class="custom-control-input" />
                    <label class="custom-control-label" for="after-save2">{{ trans('admin.continue_creating') }}</label>
                </div>
            @endif
        </div>

    </div>
</div>
<script type="text/javascript" src="/assets/admin/module/bootstrapValidator/js/bootstrapValidator.js"></script>
<script>

    $(function () {
        $('.form-inline').bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields:{
                class_name:{
                    validators:{
                        notEmpty:{
                            message: 'The class name is required and cannot be empty'
                        },
                        stringLength:{
                            min: 6,
                            max: 30,
                            message: 'The class name must be more than 6 and less than 30 characters long'
                        },
                    }
                }
            }

        })
    });
    function submitForm()
    {
        // $.map($('.form-inline').serializeArray(), function(item, index){
        //     console.log(item)
        //     return $('[name='+item['name']+']');
        // });
        // $('.form-inline').find('input[type="text"]').attr("required").each(function (i, e) {
        // 获取 text
        // $('.form-horizontal').find('input[type="text"]').each(function (i, e) {
        //     console.log(e);
        // });
        let ok = true;
        let value = '';
        let form_group = null;
        let target ='';
        let target_dom = null;
        let index = 0;
        $('.form-horizontal').find('[required="1"]').each(function (i, e) {
            let tagName = e.tagName.toLowerCase();
            value = $(e).val();
            form_group = $(e).closest('.form-group');
            let name = $(e).attr('name')
            if (i === 0) {
                target = name;
            }
            switch ($(e).attr('type')) {
                case 'radio':
                case 'checkbox':
                    let val = $('input:'+$(e).attr('type')+'[name="'+name+'"]:checked').val();
                    if (!val) {
                        form_group.addClass('has-error');
                        let label = form_group.find('label.asterisk').text();
                        let validation = "{{trans('validation.required')}}";
                        validation = validation.replace(':attribute', label)
                        form_group.find('.text-muted').show().find('span').text(validation);
                        ok = false;
                        // 找到第一个需要验证的对象
                        if (index === 0) {
                            target_dom = $(e);
                        }
                        index ++;
                    } else {
                        form_group.find('.text-muted').hide();
                        // 判断是否
                        if (val === 'other' || val === 1) {
                            if (form_group.next('.cascade-group').length > 0 ) {
                                // 获取 input
                                let dom = $('#'+name+'_supplement');
                                console.log(name+'_supplement');
                                if (dom.length > 0) {
                                    console.log(dom.val())
                                    if (!dom.val()) {
                                        form_group.next('.cascade-group').find('.form-group').addClass('has-error');
                                        form_group.next('.cascade-group').find('.text-muted').show().find('span').text("{{__('please_fill_other')}}");
                                        ok = false;
                                        // 找到第一个需要验证的对象
                                        if (index === 0) {
                                            target_dom = $(e);
                                        }
                                        index ++;
                                    }
                                }
                            } else {
                                form_group.removeClass('has-error');
                            }
                        } else {
                            form_group.removeClass('has-error');
                        }
                    }
                    break;
                // case 'checkbox':
                //     let val_c = $('input:checkbox[name="'+name+'"]:checked').val();
                //     console.log(val_c)
                //     break;
                default:
                    if (!value) {
                        form_group.addClass('has-error');
                        let label = form_group.find('label').text();
                        let validation = "{{trans('validation.required')}}";
                        validation = validation.replace(':attribute', label)
                        form_group.find('.text-muted').show().find('span').text(validation);
                        // 找到第一个需要验证的对象
                        if (index === 0) {
                            target_dom = $(e);
                        }
                        index ++;
                        ok = false;
                    } else {
                        form_group.removeClass('has-error');
                        form_group.find('.text-muted').hide();
                    }
            }


        {{--if (tagName === 'input') {--}}

            {{--    switch ($(e).attr('type')) {--}}
            {{--        case 'text': // 输入框--}}
            {{--            if (!value) {--}}
            {{--                form_group.addClass('has-error');--}}
            {{--                let name = $(e).attr('name');--}}
            {{--                --}}{{--let field = "\{{__('\"+name+\"')}}\";--}}

            {{--                // console.log($(e).attr('name'));--}}
            {{--                // console.log(field);--}}
            {{--                form_group.find('.text-muted').show().find('b').text("{{trans('validation.required')}}");--}}
            {{--                ok = false;--}}
            {{--            } else {--}}
            {{--                form_group.removeClass('has-error');--}}
            {{--                form_group.find('.text-muted').hide();--}}
            {{--            }--}}
            {{--            break;--}}
            {{--        case 'radio': // 输入框--}}
            {{--            if (!value) {--}}
            {{--                form_group.addClass('has-error');--}}
            {{--                let name = $(e).attr('name');--}}
            {{--                --}}{{--let field = "\{{__('\"+name+\"')}}\";--}}

            {{--                // console.log($(e).attr('name'));--}}
            {{--                // console.log(field);--}}
            {{--                form_group.find('.text-muted').show().find('b').text("{{trans('validation.required')}}");--}}
            {{--                ok = false;--}}
            {{--            } else {--}}
            {{--                form_group.removeClass('has-error');--}}
            {{--                form_group.find('.text-muted').hide();--}}
            {{--            }--}}
            {{--            break;--}}
            {{--    }--}}
            {{--} else if (tagName === 'select') {--}}
            {{--    if (!value) {--}}
            {{--        form_group.addClass('has-error');--}}
            {{--        let name = $(e).attr('name');--}}
            {{--        form_group.find('.text-muted').show().find('b').text("{{trans('validation.required')}}");--}}
            {{--        ok = false;--}}
            {{--    } else {--}}
            {{--        form_group.removeClass('has-error');--}}
            {{--        form_group.find('.text-muted').hide();--}}
            {{--    }--}}
            {{--}--}}
            // console.log(e.tagName);
        });
        // $('.form-inline').bootstrapValidator('validate');

        let $r;
        if (ok) {
            // TODO 特殊处理

            let c_url = "{{request()->url()}}";
            let url = c_url.replace('/create', '').replace('/edit', '');
            let id = 0;
            if (c_url.indexOf('/edit')) {
                let arr = url.split('/');
                id = arr[arr.length - 1];
                url = url.replace('/' + id, '');
            }

            let dom_mobile = $("#mobile");
            let mobile = dom_mobile.val();
            $r = verifyUnique(dom_mobile, url, id, 'mobile', mobile);
            if ($r !== true) {
                return false;
            } else {
                ok = true;
            }
            let dom_id_number = $("#ID_number");
            let ID_number = dom_id_number.val();
            $r = verifyUnique(dom_id_number, url, id, 'ID_number', ID_number);
            if ($r !== true) {
                return false;
            } else {
                ok = true;
            }
        }

        //
        if (ok) {
            document.forms[0].submit();
        } else {
            let top = target_dom.offset().top;
            $("body, html").stop().animate({
                scrollTop: top - 10
            });
        }
    }

    /**
     * 验证唯一性
     *
     * @param url
     * @param dom
     * @param id
     * @param key
     * @param value
     */
    function verifyUnique(dom, url, id, key, value)
    {
        if (!value) {
            return true;
        }
        url = url + '/verify?id=' + id + '&key=' + key +'&value='+value;
        let ok = true;
        let target_dom = null;
        $.ajax({
            url: url,
            type: "get",
            async: false,
            success: function (res) {
                let form_group = dom.closest('.form-group');
                if (res.code === 0) {
                    form_group.removeClass('has-error');
                    form_group.find('.text-muted').hide();
                } else {
                    form_group.addClass('has-error');
                    form_group.find('.text-muted').show().find('span').text(res.message);
                    ok = false;
                    target_dom = form_group;
                    let top = target_dom.offset().top;
                    $("body, html").stop().animate({
                        scrollTop: top - 10
                    });
                    // return false;
                }
            },
            error: function (res) {
            },
            complete: function (xhr, status) {
            }
        });

        return ok;
        // return target_dom;//{"ok":ok, "target_dom":target_dom};
    }
</script>