I followed along this tutorial and it worked perfectly fine. But what I want is to not use submit
button to check or perform the validation, what I want is while I'm typing, it is now validating the form. Please see my code below, or visit the link that I mentioned above.
Code:
routes
<?php
Route::get('/', function () {
return view('welcome');
});
Route::get('/article/create', 'ArticleController@showArticleCreationForm');
Route::post('/article', 'ArticleController@publish');
ArticleController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class ArticleController extends Controller
{
public function showArticleCreationForm(){
return view('article.create');
}
public function publish(Request $request){
$this->validate($request, [
'title' => 'required|min:3',
'body' => 'required|min:10'
]);
if ($request->ajax()) return;
return 'publish';
}
}
create.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Create Article</title>
<link href="http://ift.tt/1jAc5cP" rel="stylesheet" integrity="sha256-7s5uDGW3AHqw6xtJmNNtr+OBRJUlgkNJEo78P4b0yRw= sha512-nNo+yCHEyn0smMxSswnf/OnX6/KwJuZTlNZBjauKhTK0c+zT+q5JOCx0UFhXQ6rJR9jg6Es8gPuD2uZcYDLqSw==" crossorigin="anonymous">
<style>
.body {
height: 200px !important;
}
.error {
color: darkred;
margin-top: 5px;
display: block;
}
</style>
</head>
<body id="app">
<form @submit.prevent="submitForm" class="col-md-4 col-md-offset-4" action="/article" method="post">
<h1>Create New Article</h1>
<hr>
{!! csrf_field() !!}
<div class="form-group">
<input class="form-control title" type="text" name="title" placeholder="Title" v-model="formInputs.title">
<span v-if="formErrors['title']" class="error">@</span>
</div>
<div class="form-group">
<textarea class="form-control body" name="body" placeholder="Content" v-model="formInputs.body"></textarea>
<span v-if="formErrors['body']" class="error">@</span>
</div>
<button class="btn btn-primary" type="submit">Publish</button>
</form>
<script src="http://ift.tt/2ePG18a"></script>
<script src="http://ift.tt/2h5avDV"></script>
<script>
new Vue({
el: '#app',
data: {
formInputs: {},
formErrors: {}
},
methods: {
submitForm: function(e){
var form = e.target;
var action = form.action;
var csrfToken = form.querySelector('input[name="_token"]').value;
this.$http.post(action, this.formInputs, {
headers: {
'X-CSRF-TOKEN': csrfToken
}
})
.then(function() {
form.submit();
})
.catch(function (data, status, request) {
var errors = data.data;
this.formErrors = errors;
});
}
}
});
</script>
</body>
</html>
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire