I have a feature where I sanitize a YouTube link before it gets saved to the database. I have the preg_match working fine, but I can't pass the sanitized version (just the YouTube ID) back to the Controller, it reverts back the unsanitised original link.
VideoRequest:
public function rules()
{
$this->sanitize();
return [
'page_id' => 'required|integer',
'visibility' => 'required',
'item_type' => 'required',
'title' => 'required|string',
'embed' => 'required',
'content' => '',
'image' => 'string',
'order' => 'required|integer'
];
}
public function sanitize()
{
$input = $this->all();
if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $input['embed'], $match)) {
$input['embed'] = $match[1];
} else {
return "Please try another YouTube URL or link";
}
$this->replace($input);
}
VideoController:
public function store(VideoRequest $request)
{
$video = array_intersect_key(Input::all(), $request->rules());
VideoItem::create($video);
flash()->success('New video created');
return redirect()->back();
}
When I dd($input)
at the bottom of sanitize() function it will return all inputs with the embed code correctly, just as an ID. When it passes to rules(); embed is now the original link?
Maybe use a custom validation rule and then a mutator to extract the YouTube id just before the model save. http://laravel.com/docs/5.0/validation#custom-validation-rules
http://laravel.com/docs/5.0/eloquent#accessors-and-mutators
Routes
Controller
Model
namespace App;