Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to load resource: the server responded with a status of 406 (Not Acceptable) with throw new ValidationException #3655

Closed
davidatMB opened this issue Jul 28, 2018 · 2 comments

Comments

@davidatMB
Copy link

Expected behavior

It should work without an error message in console of Webdevelper.

Actual behavior

When I use the line the code

use ValidationException;
...
throw new ValidationException($validator);
...

an error message is shown in console.

Reproduce steps

Create a component like shown

<?php
namespace David\Contact\Components;

use Cms\Classes\ComponentBase;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Validator;
use ValidationException;
use System\Classes\CombineAssets;

class ContactForm extends ComponentBase {

	public function onRun() {
		$js = ['assets/js/contactform.js'];
		$css = ['assets/css/contactform.css'];
		$this->addJs(CombineAssets::combine($js, plugins_path('david/contact')));
		$this->addCss(CombineAssets::combine($css, plugins_path('david/contact')));
	}

	public function defineProperties() {
		return [
			'to' => [
				'title' => 'david.contact::lang.plugin.components.contactform.to',
				'description' => 'david.contact::lang.plugin.components.contactform.description',
				'default' => 'david.bruenner@kk-software.de',
				'validationPattern' => '^.+@.+[com|de|org]$',
				'validationMessage' => 'david.contact::lang.plugin.components.contactform.validationMessage'
			]
		];
	}

	public function componentDetails() {
		return [
			'name' => 'david.contact::lang.plugin.components.contactform.name',
			'description' => 'david.contact::lang.plugin.components.contactform.description'
		];
	}

	public function onSend() {
		$data = post();

		$name = Input::get('name');
		$email = Input::get('email  ');

		$validationMessages = [
			'name.required' => trans('david.contact::lang.plugin.components.contactform.validationMessages.name.required'),
			'name.min' => trans('david.contact::lang.plugin.components.contactform.validationMessages.name.min'),
			'email.required' => trans('david.contact::lang.plugin.components.contactform.validationMessages.email.required'),
			'email.email' => trans('david.contact::lang.plugin.components.contactform.validationMessages.email.email'),
            'message.required' => trans('david.contact::lang.plugin.components.contactform.validationMessages.message.required'),
		];

		$rules = [
			'name' => 'required|min:8',
			'email' => 'required|email',
            'message' => 'required'
		];

		$validator = Validator::make($data, $rules, $validationMessages);

		if ($validator->passes()) {
			$vars = [
				'name' => $name,
				'email' => $email,
				'message' => Input::get('message')
			];

			Mail::send('david.contact::mail.message', $vars, function ($message) {
				$subject = trans('david.contact::lang.plugin.components.contactform.subject');

				$message->to('david.bruenner@t-online.de', 'David');
				$message->subject($subject);
			});
		} else {
			throw new ValidationException($validator);
		}
	}
}

and the form

<h2>{{ 'contact.contactform'|_ }}

<form data-request="contactform::onSend" data-request-validate>

	<div class="form-group">
		<label>{{ 'contactform.yourname'|_ }}</label>
		<input type="text" name="name">
		<span data-validate-for="name" class="errormsg"></span>
	</div>

	<div class="form-group">
		<label>{{ 'contactform.from'|_ }}</label>
		<input type="email" name="email">
		<span data-validate-for="email" class="errormsg"></span>
	</div>

	<div class="form-group">
		<label>{{ 'contactform.message'|_ }}</label>
		<textarea name="message"></textarea>
		<span data-validate-for="message" class="errormsg textarea"></span>
	</div>

	<button type="submit" data-attach-loading>{{ 'contactform.send'|_ }}</button>
</form>

Submit the form without entering anything in name, lastname or message. Then in the AJAX-Request the error appears.
When you comment the line 'throw new ValidationException()' no error is thrown.

October build

437

@LukeTowers
Copy link
Contributor

@davidatMB I'm not sure what you're expecting to have happen here, you explicitly are telling the application that you are throwing an exception, this halts execution and informs the user that the request was unacceptable. What are you wanting to have happen here instead?

@davidatMB
Copy link
Author

As I wrote in

Expected behavior
It should work without an error message in console of Webdevelper.

But when you say its ok, than ok.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants