Removed form request on ajax, cleaned up some other things

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2021-12-08 17:58:46 -08:00
parent 81b66d0039
commit ff97b359ad
5 changed files with 27 additions and 13 deletions

View file

@ -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(strncmp($request->input('slack_endpoint'), 'https://hooks.slack.com/', 24));
\Log::error('Endpoint: '.$request->input('slack_endpoint')); \Log::error('Endpoint: '.$request->input('slack_endpoint'));
\Log::error('Request: '.print_r($request->all(), true)); \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))) { // 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); // 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); return response()->json(['message' => 'Success'], 200);
} catch (\Exception $e) { } 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);
} }
//} //}

View file

@ -22,15 +22,12 @@ class SlackSettingsRequest extends Request
public function rules() public function rules()
{ {
return [ 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_channel' => 'required_with:slack_endpoint|starts_with:#|nullable',
'slack_botname' => 'string|nullable', 'slack_botname' => 'string|nullable',
]; ];
} }
public function response(array $errors)
{
return $this->redirector->back()->withInput()->withErrors($errors, $this->errorBag);
}
} }

View file

@ -52,10 +52,7 @@ class Setting extends Model
'admin_cc_email' => 'email|nullable', 'admin_cc_email' => 'email|nullable',
'default_currency' => 'required', 'default_currency' => 'required',
'locale' => 'required', 'locale' => 'required',
'slack_endpoint' => 'url|required_with:slack_channel|nullable',
'labels_per_page' => 'numeric', 'labels_per_page' => 'numeric',
'slack_channel' => 'regex:/^[\#\@]?\w+/|required_with:slack_endpoint|nullable',
'slack_botname' => 'string|nullable',
'labels_width' => 'numeric', 'labels_width' => 'numeric',
'labels_height' => 'numeric', 'labels_height' => 'numeric',
'labels_pmargin_left' => 'numeric|nullable', 'labels_pmargin_left' => 'numeric|nullable',

View file

@ -64,6 +64,7 @@ return array(
'string' => 'The :attribute must be at least :min characters.', 'string' => 'The :attribute must be at least :min characters.',
'array' => 'The :attribute must have at least :min items.', '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.', 'not_in' => 'The selected :attribute is invalid.',
'numeric' => 'The :attribute must be a number.', 'numeric' => 'The :attribute must be a number.',
'present' => 'The :attribute field must be present.', 'present' => 'The :attribute field must be present.',

View file

@ -165,14 +165,23 @@
$("#slackteststatus").removeClass('text-danger'); $("#slackteststatus").removeClass('text-danger');
$("#slackteststatus").html(''); $("#slackteststatus").html('');
$("#slacktesticon").html('<i class="fa fa-spinner spin"></i> Sending Slack test message...'); $("#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') }}', url: '{{ route('api.settings.slacktest') }}',
type: 'POST', type: 'POST',
headers: { headers: {
"X-Requested-With": 'XMLHttpRequest', "X-Requested-With": 'XMLHttpRequest',
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr('content'), "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: { data: {
'slack_endpoint': $('#slack_endpoint').val(), 'slack_endpoint': $('#slack_endpoint').val(),