Backend for songs.zachdecook.com
form validation: Validate key for songs
| -rw-r--r-- | laravel/app/Http/Controllers/SongController.php | 6 | ||||
| -rw-r--r-- | laravel/app/Song.php | 11 | ||||
| -rw-r--r-- | laravel/resources/views/editsong.blade.php | 10 |
3 files changed, 25 insertions, 2 deletions
diff --git a/laravel/app/Http/Controllers/SongController.php b/laravel/app/Http/Controllers/SongController.php index d4369ae..1a0ec18 100644 --- a/laravel/app/Http/Controllers/SongController.php +++ b/laravel/app/Http/Controllers/SongController.php @@ -257,7 +257,11 @@ class SongController extends Controller { $song->title = $request->title; $song->author = $request->author; - $song->key = $request->key; + try { + $song->key = $request->key; + } catch (\Exception $e){ + return redirect("/s/{$song->id}/edit")->withErrors(['key' => $e->getMessage()])->withInput(); + } $song->text = $_POST['text'];//request->text strips whitespace. $song->verse = $request->verse; $song->save(); diff --git a/laravel/app/Song.php b/laravel/app/Song.php index c723e3a..a6cb0c0 100644 --- a/laravel/app/Song.php +++ b/laravel/app/Song.php @@ -31,6 +31,17 @@ class Song extends Model } return $sc->transposeBlock($this->text, $transp); } + + public function setKeyAttribute($value) + { + if (!$value) return; + if ($value[0]>="A"&&$value[0]<="G"){ + $this->attributes['key'] = $value; + } else { + throw new \Exception("Invalid key: $value"); + } + } + public function getNameAttribute() { return $this->title diff --git a/laravel/resources/views/editsong.blade.php b/laravel/resources/views/editsong.blade.php index 267f04a..5d477aa 100644 --- a/laravel/resources/views/editsong.blade.php +++ b/laravel/resources/views/editsong.blade.php @@ -2,7 +2,15 @@ @section('title', "Editing $song[title]") @section('content') - +@if ($errors->any()) +<div class="alert alert-danger"> + <ul> + @foreach ($errors->all() as $error) + <li>{{ $error }}</li> + @endforeach + </ul> +</div> +@endif <form method="POST" action="{{ route('song.show',['song'=>$song]) }}"> <input name='playlistReturn' hidden value='{{$_GET['playlist'] ?? ''}}'/> <input name='keyReturn' hidden value='{{$_GET['key'] ?? ''}}'/> |