Skip to content

A free open source IT asset/license management system

Removed form request on ajax, cleaned up some other things

snipeff97b359ad5e
diff --git a/app/Http/Controllers/Api/SettingsController.php b/app/Http/Controllers/Api/SettingsController.php
index 0980b297c6..5f2d57ffd4 100644
--- a/app/Http/Controllers/Api/SettingsController.php
+++ b/app/Http/Controllers/Api/SettingsController.php
@@ -163,16 +163,26 @@ class SettingsController extends Controller
}
- public function slacktest(SlackSettingsRequest $request)
+ public function slacktest(Request $request)
{
\Log::error(strncmp($request->input('slack_endpoint'), 'https://hooks.slack.com/', 24));
\Log::error('Endpoint: '.$request->input('slack_endpoint'));
\Log::error('Request: '.print_r($request->all(), true));
- \Log::error('Rules: '.print_r(Setting::rules(), true));
+ // \Log::error('Rules: '.print_r(Setting::rules(), true));
+ $validator = Validator::make($request->all(), [
+ 'slack_endpoint' => 'url|required_with:slack_channel|starts_with:https://hooks.slack.com|nullable',
+ 'slack_channel' => 'required_with:slack_endpoint|starts_with:#|nullable',
+ ]);
+
+ if ($validator->fails()) {
+ return response()->json(['message' => 'Validation failed', 'errors' => $validator->errors()], 422);
+ }
+
+
// if (($request->filled('slack_endpoint')) && ((strncmp($request->input('slack_endpoint'), "https://hooks.slack.com/", 24) !== 0))) {
// return response()->json(['message' => 'Oops! Please check the endpoint URL. Slack endpoints should start with https://hooks.slack.com. Your current endpoint is '.$request->input('slack_endpoint'), ['errors' => 'Invalid slack endpoint.']], 422);
@@ -221,7 +231,7 @@ class SettingsController extends Controller
return response()->json(['message' => 'Success'], 200);
} catch (\Exception $e) {
- return response()->json(['message' => 'Oops! Please check the channel name and webhook endpoint URL. Slack responded with: '.$e->getMessage()], 400);
+ return response()->json(['message' => 'FARTS! Please check the channel name and webhook endpoint URL ('.$request->input('slack_endpoint').'). Slack responded with: '.$e->getMessage()], 400);
}
//}
diff --git a/app/Http/Requests/SlackSettingsRequest.php b/app/Http/Requests/SlackSettingsRequest.php
index d838fde462..1f44215198 100644
--- a/app/Http/Requests/SlackSettingsRequest.php
+++ b/app/Http/Requests/SlackSettingsRequest.php
@@ -22,15 +22,12 @@ class SlackSettingsRequest extends Request
public function rules()
{
return [
- 'slack_endpoint' => 'url|required_with:slack_channel|starts_with:https://hooks.slack.com|nullable',
+ 'slack_endpoint' => 'url|required_with:slack_channel|starts_with:"https://hooks.slack.com"|nullable',
'slack_channel' => 'required_with:slack_endpoint|starts_with:#|nullable',
'slack_botname' => 'string|nullable',
];
}
- public function response(array $errors)
- {
- return $this->redirector->back()->withInput()->withErrors($errors, $this->errorBag);
- }
+
}
diff --git a/app/Models/Setting.php b/app/Models/Setting.php
index 13d7b38bc1..447071e2a9 100755
--- a/app/Models/Setting.php
+++ b/app/Models/Setting.php
@@ -52,10 +52,7 @@ class Setting extends Model
'admin_cc_email' => 'email|nullable',
'default_currency' => 'required',
'locale' => 'required',
- 'slack_endpoint' => 'url|required_with:slack_channel|nullable',
'labels_per_page' => 'numeric',
- 'slack_channel' => 'regex:/^[\#\@]?\w+/|required_with:slack_endpoint|nullable',
- 'slack_botname' => 'string|nullable',
'labels_width' => 'numeric',
'labels_height' => 'numeric',
'labels_pmargin_left' => 'numeric|nullable',
diff --git a/resources/lang/en/validation.php b/resources/lang/en/validation.php
index 4ece7d41c7..801cfe4931 100644
--- a/resources/lang/en/validation.php
+++ b/resources/lang/en/validation.php
@@ -64,6 +64,7 @@ return array(
'string' => 'The :attribute must be at least :min characters.',
'array' => 'The :attribute must have at least :min items.',
],
+ 'starts_with' => 'The :attribute must start with one of the following: :values.',
'not_in' => 'The selected :attribute is invalid.',
'numeric' => 'The :attribute must be a number.',
'present' => 'The :attribute field must be present.',
diff --git a/resources/views/settings/slack.blade.php b/resources/views/settings/slack.blade.php
index 22da6f1de5..f7bd8b84b6 100644
--- a/resources/views/settings/slack.blade.php
+++ b/resources/views/settings/slack.blade.php
@@ -165,14 +165,23 @@
$("#slackteststatus").removeClass('text-danger');
$("#slackteststatus").html('');
$("#slacktesticon").html('<i class="fa fa-spinner spin"></i> Sending Slack test message...');
- $.ajax({
+ $.ajax(
+ {
+ // If I comment this back in, I always get a success (200) message
+ // Without it, I get
+ // beforeSend: function (xhr) {
+ // xhr.setRequestHeader("Content-Type","application/json");
+ // xhr.setRequestHeader("Accept","text/json");
+ // },
+
url: '{{ route('api.settings.slacktest') }}',
type: 'POST',
headers: {
"X-Requested-With": 'XMLHttpRequest',
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr('content'),
- "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr('content')
+ // 'Accept': 'application/json',
+ // 'Content-Type': 'application/json',
},
data: {
'slack_endpoint': $('#slack_endpoint').val(),