Have you referred to the docs, its well explained and will answer your questions...
Validation problem: Array to string conversion
I have 2 questions, both are validation related.
I am using Laracasts' Flash Message package, it works in some places but in other places it don't get a notification message after a fail.
For example, in this chunk of code, I am validating if the comment input button is empty so that I can redirect home with either a success message or an error. And it works on both instances, I see both messages.
My question for this one is how can I display the actual error message instead of displaying a static message like there was an error?
At the moment, I get a message with the following info
{"comment-text":["The comment-text field is required."]}
That's not a nice message, I would like only to display that the comment field is required without the extra characters.
Doing $validator->errors()->toArray(); gives an error of Array to string conversion
if(Input::has('post_comment')) {
$rules = [
'comment-text' => 'required|string'
];
$validator = Validator::make($request->all(), $rules);
if(!$validator->fails()) {
$status = Input::get('post_comment');
$commentBox = Input::get('comment-text');
$selectedStatus = Status::find($status);
$selectedStatus->comments()->create([
'comment_text' => $commentBox,
'user_id' => Auth::user()->id,
'status_id' => $status
]);
Flash::message('Your comment has been posted', 'success');
return redirect(route('home'));
} else {
$errors = $validator->errors();
Flash::message($errors, 'warning');
return redirect(route('home'));
}
}
The second chunk of code has the same structure, I am checking if the status field is empty then I display an error or success message if everything was filled. The success message shows but not the error message. Why does it work in the first bit but not on this? Even though it's basically the same code.
if(Input::has('status-text')) {
$text = e(Input::get('status-text'));
$rules = [
'status-text' => 'required|string'
];
$validator = Validator::make($request->all(), $rules);
if(Input::hasFile('status_image_upload')) {
$rules['status_image_upload'] = 'image';
$validator = Validator::make($request->all(), $rules);
if(!$validator->fails()) {
$image = $request->file('status_image_upload');
$imageName = str_random(8) . '_' . $image->getClientOriginalName();
//$imageFull = str_random(8) . '_' . $image->getClientOriginalName();
$image->move('uploads/status_images', $imageName);
$userStatus = new Status;
$userStatus->status_text = $text;
$userStatus->image_url = $imageName;
$userStatus->type = 1;
$userStatus->user_id = Auth::user()->id;
$userStatus->save();
Flash::success('Your status has been posted');
return redirect(route('home'));
} else {
Flash::message('There was an error', 'warning');
return redirect(route('home'));
//return redirect()->back()->with('error', 'Validation failed: ' . $validator->errors);
}
} else {
if(!$validator->fails()) {
$userStatus = new Status;
$userStatus->status_text = $text;
$userStatus->user_id = Auth::user()->id;
$userStatus->save();
Flash::success('Your status has been posted');
return redirect(route('home'));
} else {
Flash::message('There was an error', 'warning');
return redirect(route('home'));
//return redirect()->back()->with('error', 'Validation failed: ' . $validator->errors);
}
}
}
Please or to participate in this conversation.