【发布时间】:2019-04-25 04:19:31
【问题描述】:
所以我正在使用 laravel 5.4,但我陷入了这个错误,无法弄清楚。我研究了这个错误,我已经看到这发生过,但解决方案不起作用我的项目。
我创建了一个表单来在我的页面中添加 cmets,如果我输入一些内容,它将它保存在数据库中并且验证正在工作,因为它不允许我添加空注释但它没有显示页面中的错误。
This is the comment form in views
<form method="post" action="{{ route('comments.store') }}">
{{ csrf_field() }}
<input type="hidden" name="commentable_type" value="App\Company">
<input type="hidden" name="commentable_id" value="{{ $company->id }}">
<h2>Add a comment</h2>
<div class="form-group @if($errors->has('url')) has-error @endif">
<label for="comment-content">Work done (url/title)</label>
<textarea placeholder="Enter url/title"
style="resize: vertical;"
id="comment-content"
name="url"
rows="2"
spellcheck="false"
class="form-control autosize-target text-left">
</textarea>
</div>
<div class="form-group @if($errors->has('body')) has-error @endif">
<label for="comment-content">Comment</label>
<textarea placeholder="Enter comment"
style="resize: vertical;"
id="comment-content"
name="body"
rows="3"
spellcheck="false"
class="form-control autosize-target text-left">
</textarea>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Submit"/>
</div>
</form>
这是CommentsControlles.php
public function store(CommentSubmitFormRequest $request)
{
$comment = Comment::create([
'body' => $request->input('body'),
'url' => $request->input('url'),
'commentable_type' => $request->input('commentable_type'),
'commentable_id' => $request->input('commentable_id'),
'user_id' => Auth::user()->id
]);
if ($comment)
{
return back()->with('success', 'Comment added successfully');
}
}
这是CommentSubmitFormRequest.php的请求
class CommentSubmitFormRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'body' => 'required',
'url' => 'required',
];
}
}
当我提交空评论表单时,$errors 返回 null 而不是错误
【问题讨论】:
标签: php laravel laravel-5.4