Matrix
Matrix

Reputation: 107

Form validation using js - Laravel

I'm using Laravel

I have this form items I get validation from database and I'm using jquery for validation all inputs feild have name="element[{{$element->id}}]" (get id from database) I'm trying different method to make validation but it doesn't work

 @if($element->type =='text' && $element->is_depend_on=='0')
            <div class="row">
            <div class="col-sm-12">
            <div class="form-group">
                  <label for="userName">{{$element->label}}</label>
                   <input id="userName" name="element[{{$element->id}}]" type="text"
                       class="form-control @if($element->validation=='required')required @endif" placeholder={{$element->placeholder}}>
                @if($errors->has('element.'.$element->id))
                    <div class="error">{{ $errors->first('element.'.$element->id) }}</div>
                @endif
                    </div>
                </div>
            </div>
            @endif
     @if($element->type =='textarea' &&$element->is_depend_on=='0')
                <div class="row">
                <div class="col-sm-12">
                <div class="form-group">
                     <label for="userName">{{$element->label}}</label>
                     <textarea class="form-control  @if($element->validation=='required')required @endif" name="element[{{$element->id}}]"
                         placeholder={{$element->placeholder}} *="" style="margin: 0px; width: 601px; height: 108px;" spellcheck="false" data-gramm="false"></textarea></div>
                    @if($errors->has('element.'.$element->id))
                        <div class="error">{{ $errors->first('element.'.$element->id) }}</div>
                    @endif
                </div>
                 </div>
               @endif

and here is my js code

var form = $("#order");
   form.validate({
       errorPlacement: function (error, element) {
           error.insertBefore(element);
       },
       rules: {}
   });

also I tried this code

 $("#order").validate({
                rules: {

                },
                messages: {
                }
            });

can anyone help me , or Do you have any suggestions ?

Upvotes: 0

Views: 1150

Answers (1)

Deepesh Thapa
Deepesh Thapa

Reputation: 1789

just use https://parsleyjs.org/ for form validation on your blade template. Very effective and has a lot of useful API and plugins. Rather than repeating same validation again and again you can make use of this little open source project.

Upvotes: 1

Related Questions