0

I have used the below code for form submission and validation, but form is not getting submitted when it gets validated.

  <form action = "/edit/{{$users[0]->id}}" id="myform" method = "post">
  <input type = "hidden" name = "_token" value = "{{csrf_token()}}">

  <div class="form-group">
     <label for="Name">Name</label>
     <input id="my-input" name="name" class="form-control left" value="{{$users[0]->name}}" type="text">
  </div>

  <div class="form-group">
    <label for="url">Name of the Link</label>
    <input type="text" name="url" id="url" class="form-control" value = "{{$users[0]->url}}" aria-describedby="helpId">
  </div>

  <div class="form-group">
    <label for="Category">Category</label>
    <select name="category" class="form-control">
    <option value="<?php echo $users[0]->category; ?>" selected><?php echo $users[0]->category; ?></option>
    <option value="Human Resource">Human Resource</option>
    <option value="Decksys">Decksys</option>
    <option value="Pentaho">Pentaho</option>
    <option value="Makto">Makto</option>
    <option value="Carton">Carton</option>
    </select>


  </div>


  <input type="submit" class="btn btn-primary" value="Submit">
  </form>

Find below the script which I have used :

   <script type="text/javascript">
   // just for the demos, avoids form submit
   jQuery.validator.setDefaults({
    debug: true,
    success: "valid"
    });
    $( "#myform" ).validate({
     rules: {
     url: {
       required: true,
          url: true
        }

       }
      });
     </script>

Find the controller code below

 public function edit(Request $request,$id) {
    $name = $request->input('name');
    $url = $request->input('url');
    $category = $request->input('category');

    DB::update('update links set name = ?,url=?,category=? where id = ?',[$name,$url,$category,$id]);
    \Session::flash('message', 'Successfully updated!');
    return redirect()->route('home');

    }

Kindly suggest a solution to submit the form along with form validation.

4
  • How do you want to submit form using AJAX or normal form submission on submit click Commented Jan 3, 2019 at 11:30
  • I need the Normal form submission Commented Jan 3, 2019 at 11:31
  • If you want normal form submission then you can use controller for validating input fields which is more easier and if you want to validate input fields on client side you can use library call parsley. Which is better solution of client side validation Commented Jan 3, 2019 at 11:33
  • Kindly check my controller code.I was updated the question Commented Jan 3, 2019 at 11:38

5 Answers 5

1

The submit method needs to be called once the validation is performed. Take a look in the documentation.
https://jqueryvalidation.org/validate/

Remeber to define proper route though. like

 Route::post('/doadduser','usercontroller@createuser');


You need to call a submit handler to submit the form.
Here is an working example. 

    <form action="/doadduser" name="registration" method="POST">

        <label for="firstname">First Name</label>
        <input type="text" name="firstname" id="firstname" placeholder="John">

        <label for="lastname">Last Name</label>
        <input type="text" name="lastname" id="lastname" placeholder="Doe">

        <label for="email">Email</label>
        <input type="email" name="email" id="email" placeholder="[email protected]">

        <label for="password">Password</label>
        <input type="password" name="password" id="password" placeholder="&#9679;&#9679;&#9679;&#9679;&#9679;">

        <button type="submit">Register</button>
      </form>

   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script  src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.js"></script>
<script type="text/javascript">
    // Wait for the /books/doAddbookDOM to be ready
$(function() {
  // Initialize form validation on the registration form.
  // It has the name attribute "registration"
  $("form[name='registration']").validate({
    // Specify validation rules
    rules: {
      // The key name on the left side is the name attribute
      // of an input field. Validation rules are defined
      // on the right side
      firstname: "required",
      lastname: "required",
      email: {
        required: true,
        // Specify that email should be validated
        // by the built-in "email" rule
        email: true
      },
      password: {
        required: true,
        minlength: 5
      }
    },
    // Specify validation error messages
    messages: {
      firstname: "Please enter your firstname",
      lastname: "Please enter your lastname",
      password: {
        required: "Please provide a password",
        minlength: "Your password must be at least 5 characters long"
      },
      email: "Please enter a valid email address"
    },
    // Make sure the form is submitted to the destination defined
    // in the "action" attribute of the form when valid
    submitHandler: function(form) {
      form.submit();
    }
  });
});
</script>

That is working fine in my local. Hope that help you too.
Sign up to request clarification or add additional context in comments.

Comments

0

I think you should validation in controller side and return json response. Then you check response with jquery.

Or maybe you should add this function after validate;

  submitHandler: function(form) {
      form.submit();
  }

4 Comments

I need the client-side validation.Now this code is working on client-side validation but it is not submit after validation
Client-side validation is a good UX while server-side validation is also required because it's the only way to ensure security and consistency of data (since the client-side is easy to tamper with).
But validate a form in the client and server both side is a good thing I guess. As a invalid form is prevented form submitting in the client end it saves some resource on the server end.
It is working but it could not perform form validation part
0

you need to define route as shown below:

Route::PATCH('/category/update/{id}','Controller@update');

also define method PATCH after your <form>:

<input name="_method" type="hidden" value="PATCH">

Comments

0

You have to do something like this in your controller.

public function edit(Request $request,$id) {
 $this->validate($request, [
    'name' => 'required',
    'url' => 'required',
    'category' => 'required',
 ]);

 $name = $request->input('name');
 $url = $request->input('url');
 $category = $request->input('category');

 DB::update('update links set name = ?,url=?,category=? where id =  ?',[$name,$url,$category,$id]);
\Session::flash('message', 'Successfully updated!');
return redirect()->route('home');

 }

If validation fails laravel will automatically throws you back to your view. Add this code to your view if validation fails it will show all the errors.

@if (count($errors) > 0)
<div class="alert alert-danger">
    <ul>
        @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
    </ul>
</div>
 @endif

8 Comments

This is worked for me while insert the data into database. But it is not working while update the data using this method
@sharmila what error you are getting while updating ?
if the url field is not a url format it return enter the valid url.If it is a correct url format it is getting updated.But now the field validation part is ok after validation form is not submitting.
@sharmila Can you post your error or screen shot of your error. So it will be better to understand.
I dont have an error but after validation submit button is not working
|
0

You have to add submit handler code to your script. You can check this tutorial to check more understanding how to validation script is working. https://www.sitepoint.com/basic-jquery-form-validation-tutorial/

   $("#myform").validate({
      // Specify validation rules
      rules: {
        ....
      },
      // Specify validation error messages
      messages: {
        ...
      },
      // Make sure the form is submitted to the destination defined
      // in the "action" attribute of the form when valid
      submitHandler: function(form) {
        form.submit();
      }
    });
  });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.