Thứ Bảy, 3 tháng 12, 2016

Laravel 5 & JQuery Tutorial : Bootstrap-validator Validation Example in Laravel 5.3


Laravel 5.3 tutorial for beginners - How to create validation using JQuery Bootstrap-validator in Laravel 5.3? this tutorial will show you how to build simple validation with Bootstrap-validator plugins the best plugin for Form validation.

At the previews lessons we have learn how to create simple validation in laravel, please read How to use Validation in Laravel 5.3.

Video tutorial Using Bootstrap-validator in laravel 5.3


Full Source Code create Form Validation in Laravel 5.3

New Route (routes\web.php)

  // new route for simple validation page
  Route::get('/validation',function(){
    return view('validation');
  });

New View (resources\view\validation.blade.php)

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Simple Validation Form</title>

    <!-- Bootstrap -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.2/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    <div class="container">
      <div class="row">
        {!! Form::open(array('method'=>'POST','files'=>true,'id'=>'validateForm')) !!}
        <div class="col-sm-12">
          <h2>Simple Validation Form</h2>
        </div>
        <div class="col-xs-8">
          <div class="form-group">
            <strong>User Name :</strong>
            <input type="text" name="username" class="form-control">
          </div>
        </div>

        <div class="col-xs-8">
          <div class="form-group">
            <strong>Email :</strong>
            {!! Form::text('email', null, array('placeholder' => 'Email','class' => 'form-control')) !!}
            {!! $errors->first('email', '<p class="alert alert-danger">:message</p>') !!}
          </div>
        </div>

        <div class="col-xs-8">
          <div class="form-group">
            <strong>Password :</strong>
            <input type="password" name="password" class="form-control">
          </div>
        </div>

        <div class="col-xs-8">
          <div class="form-group">
            <strong>Phone Number :</strong>
            {!! Form::text('phone', null, array('placeholder' => 'Mobila Number','class' => 'form-control')) !!}
            {!! $errors->first('phone', '<p class="alert alert-danger">:message</p>') !!}
          </div>
        </div>

        <div class="col-xs-8">
          <div class="form-group">
            <strong>Gender</strong>
            <div class="radio">
              <label>
                <input type="radio" name="gender" value="male"> Male
              </label>
            </div>
            <div class="radio">
              <label>
                <input type="radio" name="gender" value="female"> Female
              </label>
            </div>
            <div class="radio">
              <label>
                <input type="radio" name="gender" value="other"> Other
              </label>
            </div>
          </div>
        </div>
        <div class="col-xs-8">
          <button type="submit" class="btn btn-success">Submit</button>
          <button type="submit" class="btn btn-warning">Clear</button>
          <button type="submit" class="btn btn-primary">Back</button>
        </div>
      </div>
    </div>

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

<!-- java scrip to validate the form. -->
  <script type="text/javascript">
    $(document).ready(function(){
      $('#validateForm').bootstrapValidator({
        message:'This value is not valid',
        feedbackIcons: {
          valid: 'glyphicon glyphicon-ok',
          invalid: 'glyphicon glyphicon-remove',
          validating: 'glyphicon glyphicon-refresh'
      },
      fields: {
        username: {
          validators: {
            notEmpty: {
              message: 'The username is required'
            },
            stringLength: {
              min: 6,
              max: 20,
              message:'The username must be more than 6 and less than 30 characters long'
            },
            regexp: {
              regexp: /^[a-zA-Z0-9_\.]+$/,
              message: 'The username can only consist of alphabetical, number, dot and underscore'
            },
          }
        },

        password: {
          validators: {
              notEmpty: {
                  message: 'The password is required'
              },
              different: {
                  field: 'username',
                  message: 'The password cannot be the same as username'
              }
          }
      },

      gender: {
        validators: {
            notEmpty: {
                message: 'The gender is required'
            }
        }
    },

      email: {
        validators: {
            notEmpty: {
                message: 'Please enter your email'
            }, emailAddress: {
                message: 'The value is not a valid email address'
            }
        }
    },

      phone: {
        validators: {
        notEmpty: {
                message: 'Please enter your phone no.'
            },
        digits: {
                message: 'The mobile phone number is not valid'
            },
            stringLength: {
                min: 10,
                max: 12,
                message: 'The phone no. must be 10 digits'
            }
        }
    },
  }
});
});
</script>
    <!-- add library to validate form  -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-validator/0.5.3/js/bootstrapValidator.min.js"></script>
  </body>
</html>

Watch more Laravel Video Tutorial's



See you next lessons

Không có nhận xét nào:

Đăng nhận xét