From 407914ae12552f3fc2cca813a6603f7bdc36ef76 Mon Sep 17 00:00:00 2001 From: butschster Date: Fri, 27 May 2016 16:47:54 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B2=D1=8B=D0=BD=D0=BE=D1=81=20=D0=BB=D0=BE?= =?UTF-8?q?=D0=B3=D0=B8=D0=BA=D0=B8=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=B8=20=D1=80=D0=B5=D0=B4=D0=B0=D0=BA=D1=82?= =?UTF-8?q?=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F=20=D0=B2=D0=B8?= =?UTF-8?q?=D0=B4=D0=B6=D0=B5=D1=82=D0=BE=D0=B2=20=D0=B2=20Forms?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/Controllers/WidgetController.php | 18 +++---- src/Http/Forms/CreateWidgetForm.php | 59 +++++++++++++++++++++++ src/Http/Forms/UpdateWidgetForm.php | 32 ++++++++++++ src/Repository/WidgetRepository.php | 22 --------- 4 files changed, 100 insertions(+), 31 deletions(-) create mode 100644 src/Http/Forms/CreateWidgetForm.php create mode 100644 src/Http/Forms/UpdateWidgetForm.php diff --git a/src/Http/Controllers/WidgetController.php b/src/Http/Controllers/WidgetController.php index 7ad7763..9c4a41f 100644 --- a/src/Http/Controllers/WidgetController.php +++ b/src/Http/Controllers/WidgetController.php @@ -3,6 +3,8 @@ namespace KodiCMS\Widgets\Http\Controllers; use KodiCMS\Widgets\Contracts\WidgetManager; +use KodiCMS\Widgets\Http\Forms\CreateWidgetForm; +use KodiCMS\Widgets\Http\Forms\UpdateWidgetForm; use Meta; use WYSIWYG; use Illuminate\View\View; @@ -85,14 +87,13 @@ public function getCreate(WidgetManager $widgetManager, $type = 'html') } /** - * @param WidgetRepository $repository + * @param CreateWidgetForm $widgetForm * * @return \Illuminate\Http\RedirectResponse */ - public function postCreate(WidgetRepository $repository) + public function postCreate(CreateWidgetForm $widgetForm) { - $repository->validateOnCreate($this->request); - $widget = $repository->create($this->request->all()); + $widget = $widgetForm->save(); return $this->smartRedirect([$widget]) ->with('success', trans($this->wrapNamespace('core.messages.created'), [ @@ -118,15 +119,14 @@ public function getEdit(WidgetRepository $repository, $id) } /** - * @param WidgetRepository $repository - * @param int $id + * @param UpdateWidgetForm $widgetForm + * @param int $id * * @return \Illuminate\Http\RedirectResponse */ - public function postEdit(WidgetRepository $repository, $id) + public function postEdit(UpdateWidgetForm $widgetForm, $id) { - $repository->validateOnUpdate($id, $this->request); - $widget = $repository->update($id, $this->request->all()); + $widget = $widgetForm->save(); return $this->smartRedirect([$widget]) ->with('success', trans($this->wrapNamespace('core.messages.updated'), [ diff --git a/src/Http/Forms/CreateWidgetForm.php b/src/Http/Forms/CreateWidgetForm.php new file mode 100644 index 0000000..a95fb0c --- /dev/null +++ b/src/Http/Forms/CreateWidgetForm.php @@ -0,0 +1,59 @@ +repository = $repository; + } + + /** + * @return array + */ + public function rules() + { + return [ + 'name' => 'required|max:255', + 'type' => 'required', + ]; + } + + /** + * @return array + */ + public function labels() + { + return trans('widgets::core.field'); + } + + /** + * Persist the form. + * + * @return Widget + */ + public function persist() + { + return $this->repository->create($this->request->all()); + } +} \ No newline at end of file diff --git a/src/Http/Forms/UpdateWidgetForm.php b/src/Http/Forms/UpdateWidgetForm.php new file mode 100644 index 0000000..f4ddd6a --- /dev/null +++ b/src/Http/Forms/UpdateWidgetForm.php @@ -0,0 +1,32 @@ + 'required|max:255', + ]; + } + + /** + * Persist the form. + * + * @return Widget + */ + public function persist() + { + return $this->repository->update( + $this->request->route('id'), + $this->request->all() + ); + } +} \ No newline at end of file diff --git a/src/Repository/WidgetRepository.php b/src/Repository/WidgetRepository.php index e4e837c..d0431de 100644 --- a/src/Repository/WidgetRepository.php +++ b/src/Repository/WidgetRepository.php @@ -17,28 +17,6 @@ public function __construct(Widget $model) parent::__construct($model); } - /** - * @param Request $request - */ - public function validateOnCreate(Request $request) - { - $this->validate($request, [ - 'name' => 'required|max:255', - 'type' => 'required', - ]); - } - - /** - * @param int $id - * @param Request $request - */ - public function validateOnUpdate($id, Request $request) - { - $this->validate($request, [ - 'name' => 'required|max:255', - ]); - } - /** * @param int $id * @param array $data