I have a WYSIWYG editor. When users continue to press the spacebar in the editor, the input will be like that.
"<p> </p>"
To prevent this, I changed the method allin the Request class like this to remove spaces and tags.
public function all()
{
$input = parent::all();
$input['body'] = strip_tags(preg_replace('/\s+/', '', str_replace(' ',"", $input['body'])));
return $input;
}
This works great.
However, the problem is that if other validation rules do not work, the return value from the oldauxiliary function is modified by the method.
So, if the original input is similar to this
"""
<p> <iframe src="//www.youtube.com/embed/Mb5xcH9PcI0" width="560" height="314" allowfullscreen="allowfullscreen"></iframe></p>\r\n
<p>This is the body.</p>
"""
And if other validation rules do not work, I get this as an old input.
"Thisisthebody."
So, is there a way to get the original inputs of the query as the old input when the validation failed?
Here is my form request.
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
use Illuminate\Validation\Factory as ValidationFactory;
class ArticleRequest extends Request
{
public function all()
{
$input = parent::all();
$input['body'] = strip_tags(preg_replace('/\s+/', '', str_replace(' ',"", $input['body'])));
return $input;
}
public function authorize()
{
return true;
}
public function rules()
{
return [
'title' => 'required|min:3|max:40',
'tags.*' => 'required',
'body' => 'required|min:50',
'thumbnail'=>'image|file|max:10240'
];
}
public function messages()
{
return [
'title.required' => 'タイトルを入力してください',
'title.min' => 'タイトルは3文字以上でお願いします',
'title.max' => 'タイトルは40文字以下でお願いします',
'body.min' => '本文は50文字以上お書きください',
'body.required' => '本文を入力してください',
'tags.*.required' => 'タグを選んでください',
'thumbnail.image' => '画像の形式はjpeg, bmp, png, svgのいずれかをアップロードできます',
'thumbnail.file' => 'フォームから画像をもう一度アップロードしてください',
'thumbnail.max' => 'ファイルは10MBまでアップロードできます',
];
}
}