jQuery表单验证规则已添加到所有元素,而不是某些元素

时间:2019-06-28 07:24:13

标签: jquery validation

我正在构建这样的Laravel表单:-

{{ Form::open(array(
                  'method' => 'POST',
                  'class' => 'form-horizontal form-label-left',
                  'route' =>  ['edit_settings'],
                  'id' => 'editSettingsForm',
                  'files' => true,
                  'novalidate' => true)) }}

<div class="col-md-12 col-sm-12 col-xs-12">
  @foreach($configList as $key => $cl)
  <div class="item form-group">
    @php
    $caption = $cl['config_key'];
    $caption = str_replace('_', ' ', $caption);
    @endphp
    <label class="control-label col-md-3 col-sm-3 col-xs-12" for="config_key">@if($caption == 'emails'){{ 'Admin ' . ucfirst($caption) }}@else{{ ucfirst($caption) }}@endif <span class="required">*</span>
    </label>
    <div class="col-md-9 col-sm-9 col-xs-12">
      {{ Form::hidden('config_key[]', $cl['config_key'], array(
                              'id' => 'config_key_' . $key,
                              'class' => 'form-control col-md-7 col-xs-12',
                              'data-validate-length-range' => '3,50',
                              'placeholder' => 'Config Key',
                              'required' => 'required' )) }}
      @if($cl['value_unit'] == 'date')
      {{ Form::select('config_value[]', 
                      array(
                        'd-m-Y' => 'd-m-Y',
                        'd/m/Y' => 'd/m/Y',
                        'd.m.Y' => 'd.m.Y',
                        'Y-m-d' => 'Y-m-d',
                        'Y/m/d' => 'Y/m/d',
                        'Y.m.d' => 'Y.m.d',
                        'm-d-Y' => 'm-d-Y',
                        'm/d/Y' => 'm/d/Y',
                        'm-d-Y' => 'm.d.Y'
                      ), 
                      $cl['config_value'], 
                      array(
                        'class' => 'form-control col-md-7 col-xs-12', 
                        'id'    => 'config_value_' . $key
                      )) }}
      @elseif($cl['value_unit'] == 'time')  
      {{ Form::select('config_value[]', 
                      array(
                        'H:i:s'   => 'H:i:s',
                        'H.i.s'   => 'H.i.s',

                        'H:i'     => 'H:i',
                        'H.i'     => 'H.i',

                        'h:i:s A' => 'h:i:s A',
                        'h.i.s A' => 'h.i.s A',
                        'h:i:s a' => 'h:i:s a',
                        'h.i.s a' => 'h.i.s a',

                        'h:i A'   => 'h:i A',
                        'h.i A'   => 'h.i A',
                        'h:i a'   => 'h:i a',
                        'h.i a'   => 'h.i a'
                      ), 
                      $cl['config_value'], 
                      array(
                        'class' => 'form-control col-md-7 col-xs-12', 
                        'id'    => 'config_value_' . $key
                      )) }}
      @elseif($cl['value_unit'] == 'url')  
      {{ Form::url('config_value[]', $cl['config_value'], array(
                              'id' => 'config_value_' . $key,
                              'class' => 'form-control col-md-7 col-xs-12',
                              'data-validate-length-range' => '3,50',
                              'placeholder' => 'Config value',
                              'required' => 'required' )) }}
      @else                      
      {{ Form::text('config_value[]', $cl['config_value'], array(
                              'id' => 'config_value_' . $key,
                              'class' => 'form-control col-md-7 col-xs-12',
                              'data-validate-length-range' => '3,50',
                              'placeholder' => 'Config value',
                              'required' => 'required' )) }}
      @endif
    </div>
  </div>
  @endforeach
  <div class="ln_solid"></div>
  <div class="form-group">
    <div class="col-md-6 col-md-offset-3">
      <a class="btn btn-primary back_new" href="{{url('/cpanel/dashboard/')}}">Back</a>
      <button id="send" type="submit" class="btn btn-success submit_new">Submit</button>
    </div>
  </div>
</div>

<div style="clear:both;"></div>
{{ Form::close() }}

请注意:-

  1. 多个输入元素具有相同的名称,即。 config_value []。
  2. 名称为config_value []的元素的类型可能为“文本”,而 名称为config_value []的另一个元素的类型可能为“ url”。
  3. type ='url'的用户应具有url验证规则,而 类型为'text'的网址,应该没有网址验证规则

所有输入类型将具有一些必需的通用基本规则。类型为url的将具有url验证规则。

这是jquery表单验证代码:-

$(document).ready(function(){

    $("#editSettingsForm").validate({
        ignore: [],
        rules: {
            'config_value[]': {
                required: true
            }
        },
        messages: {
            'config_value[]': {
                required: "Please enter value"
            }
        }
    });

    $("input[type=url]").each(function(){
        $(this).rules( "add", {
            url: true,
            messages: {
                url: "Please, provide valid url link"
            }
        });
    });
}); 

但是,所有输入都将添加url验证规则,而不是type = url的输入。我在做什么错了?

0 个答案:

没有答案