Skip to content

Commit afd9ed0

Browse files
authored
Merge pull request #11 from webfactor/1.2
[EH] use translation for notification and format errors
2 parents 57cece7 + bbf0ced commit afd9ed0

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
2121
### Security
2222
- Nothing
2323

24+
## 1.2.0 - 2018-05-24
25+
26+
### Changed
27+
- uses translation files for notifications
28+
- formated validation errors in notifications
29+
2430
## 1.1.0 - 2018-05-23
2531

2632
### Added

resources/views/modal/create.blade.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,17 @@
3434
success: function (data) {
3535
new PNotify({
3636
type: "success",
37-
title: "Erfolg",
38-
text: "gespeichert"
37+
title: "{{ trans('backpack::base.success') }}",
38+
text: "{{ trans('backpack::crud.insert_success') }}"
3939
});
4040
4141
$("#{{ $entity }}_modal").modal('toggle');
4242
},
4343
error: function (data) {
4444
new PNotify({
4545
type: "error",
46-
title: "Fehler",
47-
text: "Nicht gespeichert: " + data.responseText
46+
title: "{{ trans('backpack::base.error') }}",
47+
text: "{{ trans('backpack::base.error') }}: " + data.responseJSON
4848
});
4949
}
5050
});
@@ -53,4 +53,4 @@
5353
});
5454
</script>
5555

56-
@endpush
56+
@endpush

src/Traits/InstantFields.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Foundation\Http\FormRequest;
66
use Illuminate\Http\Request;
7+
use Illuminate\Support\MessageBag;
78

89
trait InstantFields
910
{
@@ -122,7 +123,7 @@ public function ajaxStore(Request $request)
122123

123124
if ($storeRequest = $this->getAjaxStoreRequest()) {
124125
if ($errors = $this->ajaxValidationFails($request, $storeRequest->rules())) {
125-
return response()->json($errors, 422);
126+
return response()->json($this->ajaxFormatMessage($errors), 422);
126127
}
127128
}
128129

@@ -156,7 +157,7 @@ public function ajaxValidationFails(Request $request, array $rules)
156157
*/
157158
private function ajaxRespondNoPermission()
158159
{
159-
return response()->json(['errors' => 'No permission'], 403);
160+
return response()->json($this->ajaxFormatMessage(trans('backpack::base.unauthorized')), 403);
160161
}
161162

162163
/**
@@ -176,6 +177,28 @@ private function ajaxRespondCreated()
176177
*/
177178
private function ajaxRespondError()
178179
{
179-
return response()->json(['errors' => 'Could not save'], 422);
180+
return response()->json($this->ajaxFormatMessage(trans('backpack::base.error_saving')), 422);
181+
}
182+
183+
/**
184+
* Formats the message for the notification
185+
*
186+
* @return string
187+
*/
188+
private function ajaxFormatMessage($message)
189+
{
190+
if ($message instanceof MessageBag) {
191+
$validationErrors = '<ul>';
192+
193+
foreach ($message->all() as $validationError) {
194+
$validationErrors .= '<li>' . $validationError . '</li>';
195+
}
196+
197+
$validationErrors .= '</ul>';
198+
199+
return $validationErrors;
200+
}
201+
202+
return $message;
180203
}
181204
}

0 commit comments

Comments
 (0)