所以我正在使用laravel 5.4,但是我陷入了这个错误并且无法解决它。我研究了这个错误,并且我发现这是以前发生的,但是解决方案无法解决我的项目。
我创建了一个在页面中添加注释的表单,如果我键入了将其保存到数据库中并且验证工作正常的方法,则该表单可以正常工作,因为它不允许我添加空注释,但不会在页面中显示错误。
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而不是错误
答案 0 :(得分:0)
您的验证规则不完整。它只是说是必需的,在您的情况下,因为字段确实存在,所以您的body
和url
被发送。您应该设置最少的字符数或对active_url/url
字段执行url
。
public function rules()
{
return [
'body' => 'required|min:1', // minimum length of 1 character
'url' => 'required|url', // must be a valid URL
];
}