我已使用以下代码进行表单提交和验证,但是在验证表单时并未提交表单。
<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>
在我使用的脚本下面找到:
<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>
在下面找到控制器代码
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');
}
请提出一种提交表单以及表单验证的解决方案。
答案 0 :(得分:1)
执行验证后,需要调用Submit方法。看一下文档。
https://jqueryvalidation.org/validate/
请务必定义正确的路线。像
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="john@doe.com">
<label for="password">Password</label>
<input type="password" name="password" id="password" placeholder="●●●●●">
<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.
答案 1 :(得分:0)
我认为您应该在控制器端进行验证并返回json响应。然后用jquery检查响应。
或者也许您应该在验证后添加此功能;
submitHandler: function(form) {
form.submit();
}
答案 2 :(得分:0)
您需要如下所示定义路线:
Reading package lists...
Building dependency tree...
Reading state information...
E: Unable to locate package java-1.8.0-openjdk-headless
E: Couldn't find any package by glob 'java-1.8.0-openjdk-headless'
E: Couldn't find any package by regex 'java-1.8.0-openjdk-headless'
E: Unable to locate package java-1.8.0-openjdk-devel
E: Couldn't find any package by glob 'java-1.8.0-openjdk-devel'
E: Couldn't find any package by regex 'java-1.8.0-openjdk-devel'
The command '/bin/sh -c apt-get update && apt-get install curl
unzip java-1.8.0-openjdk-headless java-1.8.0-openjdk-devel -y &&
apt-get clean all' returned a non-zero code: 100
还要在Route::PATCH('/category/update/{id}','Controller@update');
之后定义方法PATCH:
<form>
答案 3 :(得分:0)
您必须在控制器中执行类似的操作。
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');
}
如果验证失败,laravel将自动将您带回到视图。如果验证失败,则将此代码添加到您的视图中,它将显示所有错误。
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
答案 4 :(得分:0)
您必须将提交处理程序代码添加到脚本中。您可以查看本教程,以进一步了解验证脚本的工作原理。 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();
}
});
});