This is my edit() method. NOTE: I've stripped out some code that isn't relevant to my issue. It is just setting up my theme.
public function edit($id)
{
$voice = Voice::with('demos')->find($id);
if(empty($voice))
{
return Redirect::route('admin::voices.index')->withError('The voice talent you are trying to modify does not exist.');
}
// display the page
return $this->theme->of('admin.voices.edit', compact('voice'))->render();
}
This is my update() method. As with the edit() method, I've removed some code that isn't relevant to my issue.
public function update(SaveVoiceRequest $request, FlysystemManager $flysystem, $id)
{
$voice = Voice::find($id);
if(empty($voice))
{
return Redirect::route('admin::voices.index')->withError('The voice talent you are trying to modify does not exist.');
}
// are demos being uploaded?
if($request->has('demo_names'))
{
$errors = [];
$demos = [];
$max_filesize = Config::get('voices.max_filesize');
$allowed_filetypes = Config::get('voices.allowed_filetypes');
foreach($form_values['demo_names'] as $key => $name)
{
$count = $key+1;
$file = $form_values['demo_files'][$key];
$filename = (!empty($file)) ? $file->getClientOriginalName() : null;
$extension = (!empty($file)) ? $file->getClientOriginalExtension() : null;
$size = (!empty($file)) ? $file->getClientSize() : null;
// VALIDATION:
// - name required
if(empty($name))
{
$errors[] = '<li>You must enter a name for demo #'.$count.'.</li>';
}
// - file required
if(empty($file))
{
$errors[] = '<li>You must upload a file for demo #'.$count.'.</li>';
}
// - file type allowed
else if(!in_array($extension, $allowed_filetypes))
{
$errors[] = '<li>You uploaded an invalid file for demo #'.$count.'. It must be one of the following types: '.implode(', ', $allowed_filetypes).'</li>';
}
// - file size less than maximum size
else if($size > $max_filesize)
{
$errors[] = '<li>You must upload a file smaller than the maximum allowed ('.NumberHelper::convertBytesToString($max_filesize).') for demo #'.$count.'.</li>';
}
// - file name unique
else if(VoiceDemo::where('filename', $filename)->exists())
{
$errors[] = '<li>A file with the same name as the file you uploaded for demo #'.$count.' already exists.</li>';
}
$demos[] = ['name' => $name, 'filename' => $filename, 'file' => $file];
}
// were there any validation issues with the uploaded files?
if(!empty($errors))
{
return Redirect::route('admin::voices.edit', ['id' => $id])->withInput()->with('demos', $demos)->withError('The voice talent could not be updated. Please address the following issues and try again.<ul>'.implode('', $errors).'</ul>');
}
}
if($voice->update($form_values) === true)
return Redirect::route('admin::voices.edit', ['id' => $id])->withSuccess('The voice talent has been successfully updated.');
else
return Redirect::route('admin::voices.edit', ['id' => $id])->withInput()->withError('An error was encountered while attempting to update the voice talent.');
}
As for my Form Request, I'm just setting up the validation rules and messages in that.
In my view, I have the following code that displays the validation error message.
@if(Session::has('error'))
<div class="alert-box alert"><i class="fa fa-lg fa-exclamation-circle"></i> {!! Session::get('error') !!}</div>
@endif