【问题标题】:Custom validation rules required when select box选择框时需要自定义验证规则
【发布时间】:2019-12-01 00:41:00
【问题描述】:

我有一个可以选择公司、商店、部门的范围,选择公司时,会出现公司字段供用户输入。同样在选择店铺时,会出现公司和店铺字段供用户输入。选择部门时,会出现所有 3 个字段。 My problem is that I have to set the rule so that when selecting company, only company will be requried, when the store is selected, the company and store fields are requried.这是规则,但所有 3 个字段都是必需的。点击范围,显示对应的字段,我写在脚本里

这是规则函数:

public function rules()
    {
        return [
            'assigned_company' => 'required',
            'assigned_store' =>'required',
            'assigned_department' =>'required',
        ];
    }

    public function messages()
    {
        return [
            'assigned_company.required' => 'The company field is required.',
            'assigned_store.required' => 'The store field is required.',
            'assigned_department.required' => 'The department field is required.'
        ];
    }

还有脚本:

$('input[name=checkout_to_type_contract]').on("change",function () {
        var object_type = $('input[name=checkout_to_type_contract]:checked').val();
        var object_id = $('#assigned_company option:selected').val();
        if (object_type == 'company') {
            $('#current_assets_box').fadeOut();
            $('#assigned_company').show();
            $('#assigned_store').hide();
            $('#assigned_department').hide();
            $('.notification-callout').fadeOut();
        } else if (object_type == 'store') {
            $('#current_assets_box').fadeOut();
            $('#assigned_company').show();
            $('#assigned_store').show();
            $('#assigned_department').hide();
            $('.notification-callout').fadeOut();
        } else  {
            $('#assigned_company').show();
            $('#assigned_store').show();
            $('#assigned_department').show();
            if (object_id) {
                $('#current_assets_box').fadeIn();
            }
            $('.notification-callout').fadeIn();
        }
    });

这是html。因为篇幅太长,所以我把相关的部分拿走

<!--Scope-->
<div class="form-group" id="assignto_selector"{!!  (isset($style)) ? ' style="'.e($style).'"' : ''  !!}>
    {{ Form::label('name', trans('general.scope'), array('class' => 'col-md-3 control-label')) }}
    <div class="col-md-8">
        <div class="btn-group" data-toggle="buttons">
            @if ((isset($scope_company_contract)) && ($scope_company_contract!='false'))
            <label class="btn btn-default active">
                <input name="checkout_to_type_contract" value="company" type="radio" checked="checked"><i class="fa fa-user"></i> {{ trans('general.company') }}
            </label>
            @endif
            @if ((isset($scope_store_contract)) && ($scope_store_contract!='false'))
            <label class="btn btn-default">
                <input name="checkout_to_type_contract" value="store" type="radio"><i class="fa fa-barcode"></i> {{ trans('general.store') }}
            </label>
            @endif
            @if ((isset($scope_department_contract)) && ($scope_department_contract!='false'))
            <label class="btn btn-default">
                <input name="checkout_to_type_contract" value="department" class="active" type="radio"><i class="fa fa-map-marker"></i> {{ trans('general.department') }}
            </label>
            @endif

            {!! $errors->first('checkout_to_type_contract', '<span class="alert-msg"><i class="fa fa-times"></i> :message</span>') !!}
        </div>
    </div>
</div>

<!-- Company -->
<div id="assigned_company" class="form-group{{ $errors->has($fieldname) ? ' has-error' : '' }}"{!!  (isset($style)) ? ' style="'.e($style).'"' : ''  !!}>
    {{ Form::label($fieldname, $translated_name, array('class' => 'col-md-3 control-label')) }}
    <div class="col-md-7{{  ((isset($required) && ($required =='true'))) ?  ' required' : '' }}">
        <select class="js-data-ajax" data-endpoint="companies" data-placeholder="{{ trans('general.select_company') }}" name="{{ $fieldname }}" style="width: 100%" id="company_select">
            @if ($company_id = Input::old($fieldname, (isset($item)) ? $item->{$fieldname} : ''))
                <option value="{{ $company_id }}" selected="selected">
                    {{ (\App\Models\Company::find($company_id)) ? \App\Models\Company::find($company_id)->name : '' }}
                </option>
            @else
                <option value="">{{ trans('general.select_company') }}</option>
            @endif
        </select>
    </div>


    {!! $errors->first($fieldname, '<div class="col-md-8 col-md-offset-3"><span class="alert-msg"><i class="fa fa-times"></i> :message</span></div>') !!}

</div>

<!-- Store -->
<div id="assigned_store" class="form-group{{ $errors->has($fieldname) ? ' has-error' : '' }}"{!!  (isset($style)) ? ' style="'.e($style).'"' : ''  !!}>
        {{ Form::label($fieldname, $translated_name, array('class' => 'col-md-3 control-label')) }}
        <div class="col-md-7{{  ((isset($required) && ($required =='true'))) ?  ' required' : '' }}">
            <select class="store_select" data-endpoint="" data-placeholder="Select Store" name="{{ $fieldname }}" style="width: 100%" id="store_select">
                @if ($storeSelect = Input::old($fieldname, (isset($item)) ? $item->{$fieldname} : ''))
                    <option value="{{ $storeSelect }}" selected="selected">
                        {{ (\App\Models\Store::find($storeSelect)) ? \App\Models\Store::find($storeSelect)->name : '' }}
                    </option>
                @else
                    <option value="">{{ trans('admin.store.table.select_store') }}</option>
                @endif
            </select>
        </div>


        {!! $errors->first($fieldname, '<div class="col-md-8 col-md-offset-3"><span class="alert-msg"><i class="fa fa-times"></i> :message</span></div>') !!}

    </div>
<!-- Department -->
<div id="assigned_department" class="form-group{{ $errors->has($fieldname) ? ' has-error' : '' }}"{!!  (isset($style)) ? ' style="'.e($style).'"' : ''  !!}>

    {{ Form::label($fieldname, $translated_name, array('class' => 'col-md-3 control-label')) }}

    <div class="col-md-7{{  ((isset($required) && ($required =='true'))) ?  ' required' : '' }}">
        <select class="department_select" data-endpoint="departments" data-placeholder="{{ trans('general.select_department') }}" name="{{ $fieldname }}" style="width: 100%" id="department_select">
            @if ($department_id = Input::old($fieldname, (isset($item)) ? $item->{$fieldname} : ''))
                <option value="{{ $department_id }}" selected="selected">
                    {{ (\App\Models\Department::find($department_id)) ? \App\Models\Department::find($department_id)->name : '' }}
                </option>
            @else
                <option value="">{{ trans('general.select_department') }}</option>
            @endif
        </select>
    </div>


    {!! $errors->first($fieldname, '<div class="col-md-8 col-md-offset-3"><span class="alert-msg"><i class="fa fa-times"></i> :message</span></div>') !!}

</div>

【问题讨论】:

  • 我们能不能也看看你的html表单
  • 是的,我编辑了它

标签: laravel validation rules


【解决方案1】:

您可以使用条件规则来验证您的结帐类型。

public function rules()
{
    return [
        'checkout_to_type_contract' => 'required',
        'assigned_company' => 'required',
        'assigned_store' => Rule::requiredIf(in_array(request('checkout_to_type_contract'),['department','store'])),
        'assigned_department' => 'required_if:checkout_to_type_contract,department',
    ];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 2017-09-12
    • 2011-09-28
    • 2017-04-11
    • 2018-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多