Reputation: 946
I am using laravel. Below is my form.
<h1>New Comment</h1>
<form method="post">
Comment: <input type="text" size="128" name="comment" value="{{old('body')}}" /></p>
@if ($errors->has('body'))
<p class="warning">Comment cannot be empty.</p>
@endif
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
{{ csrf_field() }}
</form>
My Route:
Route::post('/{id}', 'FilmsController@addComment')->name('addComment');
My controller:
public function addComment(Request $request)
{
$request->validate([
'body' => 'required',
]);
$entry = new Comment();
$entry->body = $request->body;
$entry->save();
return redirect('/');
My schema includes 'body'... $table->string('body');
I am seeing my warning message when I click submit. The same happens when trying to update as well as add a new comment.
Upvotes: 2
Views: 2433
Reputation: 4767
All you need to do is change the name="comment" to name="body" as you are validating and require the body
field which the form doesn't have.
<input type="text" size="128" name="body" value="{{old('body')}}" />
Upvotes: 1
Reputation: 163978
Because the element's name is comment
, change this:
'body' => 'required',
To:
'comment' => 'required',
And this:
$entry->body = $request->body;
To:
$entry->comment = $request->comment;
Also, add an action to the form, for example:
<form method="post" action="/add-comment">
And the route should be:
Route::post('add-comment', 'FilmsController@addComment')->name('addComment');
Upvotes: 3