我正在研究laravel 5.2。我的评论框的textarea总是被检测为空。我已经指定了我的post_id。还完成了其他任何手续。
控制器:
public function postReply(Request $request, $post_id) {
$data = $request::all();
// Applying validation rules.
$rules = array("reply-{ $post_id }" => 'required|max:1000');
$val = Validator::make($data, $rules);
if ($val->passes()) {
dd('all ok');
} else {
$message = 'There is an error';
}
return redirect()->route('home')->with(['message' => $message]);
}
形式:
<form action="{{ route('post.reply',['post_id' => $post->id]) }}"
method="post">
<div class="form-group">
<textarea class="form-control" name=" reply-{{ $post->id }} " rows="2"
placeholder = "Reply to this Post"></textarea>
</div>
<button type="submit" class="btn btn-default btn-sm"> Reply </button>
<input type="hidden" value="{{ Session::token() }}" name="_token">
</form>
答案 0 :(得分:0)
你有一个问题:
$data = $request::all();
应该是
$data = $request->all();
或者像这样:
public function postReply(Request $request)
{
// Here you are passing class so all is member of Request Class.
$data=$request->all()
// Applying validation rules.
$rules = [
'post_id' => 'required|max:1000'
];
$val = Validator::make($data, $rules);
if($val->passes())
{
dd('all ok');
}
else
{
$message= 'There is an error';
}
return redirect()->route('home')->with(['message' => $message]);
}