我在电子邮件字段上有一个简单的验证程序。通过JQuery,我运行Laravel控制器来检查输入。但是它总是失败。.
为了使内容更清楚,我将else留空了。
我也收到此错误。找到一些与此有关的stackoverflow主题,并尝试使用代码中看到的解决方案,但这没有帮助。
422 (Unprocessable Entity)
我的控制器功能:
public function send_mail(Request $request, VendorProfile $profile_id){
$profile= $profile_id;
$email = $request->input('contact_e_mail');
$contact_first_name = $request->input('contact_first_name');
$email_profile = $profile->email;
$name_profile = $profile->name;
$rules = [
'contact_e_mail' => 'required|email',
];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
if($request->ajax())
{
return response()->json(array(
'success' => false,
'message' => 'There are incorect values in the form!',
'errors' => $validator->getMessageBag()->toArray(),
), 422);
}
$this->throwValidationException(
$request, $validator
);
} else {
}
}
我的观点:
<div class="events single-event">
<div class="o-grid">
<div class="o-grid__col u-6/12">
<div class="o-grid__col u-12/12@sm">
<h4>@lang('profile.contactTitle')</h4>
</div>
<div class="o-grid__col u-12/12@sm">
{!! Form::label('', __('profile.contactSalutation').'*') !!}
@if( Session::get('urlLang') == "en" )
{!! Form::select(__('contact_contactSalutation'), array('Miss' => 'Miss', 'Sir' => 'Sir'),array('class' => 'c-dropdown c-dropdown__simple u-mb-x6'),['required' => 'required']) !!}
@else
{!! Form::select(__('contact_contactSalutation'), array('Frau' => 'Frau', 'Herr' => 'Herr'),array('class' => 'c-dropdown c-dropdown__simple u-mb-x6'),['required' => 'required']) !!}
@endif
</div>
<br>
</div>
<div class="o-grid__col u-6/12">
<div class="o-grid__col u-12/12@sm">
<p style="color: #696978; font-size: 14px; text-align: right">@lang('profile.mandatoryField')</p>
</div>
</div>
</div>
<div class="o-grid">
<div class="o-grid__col u-6/12">
<div class="o-grid__col u-12/12@sm">
{!! Form::label('contact_first_name', __('profile.contactFirstName').'*') !!}
{!! Form::text('contact_first_name', null, ['placeholder' => __('profile.contactFirstName'),'class' => 'c-input required','id' => 'contact_first_name','required']) !!}
</div>
<div class="o-grid__col u-12/12@sm">
{!! Form::label('contact_e_mail', __('profile.contactEmail').'*') !!}
{!! Form::text('contact_e_mail', null, ['placeholder' => __('profile.contactEmail'),'class' => 'c-input required email','id' => 'contact_e_mail','required']) !!}
<input type="hidden" id="profile_id" value="{{ $profile->id }}">
</div>
</div>
<div class="o-grid__col u-6/12">
<div class="o-grid__col u-12/12@sm">
{!! Form::label('contact_last_name', __('profile.contactLastName').'*') !!}
{!! Form::text('contact_last_name', null, ['placeholder' => __('profile.contactLastName'),'class' => 'c-input required','id' => 'contact_last_name','required']) !!}
</div>
<div class="o-grid__col u-12/12@sm">
{!! Form::label('contact_phone', __('profile.contactPhone')) !!}
{!! Form::text('contact_phone', null, ['placeholder' => __('profile.contactPhone'),'class' => 'c-input','id' => 'contact_phone']) !!}
</div>
</div>
<div class="o-grid__col u-12/12">
<div class="o-grid__col">
{!! Form::label('text', __('profile.contactMessageInfo')) !!}
{!! Form::textarea('contact_text',null,['class' => 'c-input c-input__text required','placeholder' => __('profile.contactMessageInfo'),'id' => 'contact_text','required']) !!}
</div>
</div>
{{--<div class="o-grid__col u-12/12">
<label for="captcha">@lang('mail.contactCaptcha')*</label>
</div>
<div class="o-grid__col u-6/12">
<input id="captcha" class="c-input" type="text" name="captcha" placeholder="@lang('mail.contactCaptchaInsert')" required>
</div>
<div class="o-grid__col u-6/12">
<p id="captcha-image-frame">{!! captcha_img('ah_contact') !!}</p>
</div>--}}
<div class="o-grid__col u-12/12">
<input class="c-input__checkbox required" id="toc" type="checkbox" name="agree_to_toc" value="1" required>
<label class="c-input__checkboxLabel p2" for="toc">
{!! @trans('global.formTacInfo', [
'class' => 'c-link c-link__primary',
'link_datenschutz' => route('static.show', ['folder_id' => 28, 'slug' => "datenschutz"])
]) !!}</label>
</div>
<div class="o-grid__col u-6/12">
<span class="c-form--haserror" id="alert_message" style="display: none"></span>
</div>
<div class="o-grid__col u-text-right">
<button id="submit_contact_form" class="c-btn c-btn--small c-btn--red" type="submit" style="display: none" onclick="submitForm()">
<span>@lang('profile.contactSendMessage')</span></button>
</div>
</div>
</div>
<script>
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('#toc').change(function() {
if($(this).is(":checked")) {
$('#submit_contact_form').show();
}
else{
$('#submit_contact_form').hide();
}
})
function submitForm() {
var profile_id = $("#profile_id").val();
var contact_e_mail = $("#contact_e_mail").val();
$.ajax({
method: 'POST',
url: '{{route("profile.sendmail")}}',
data: {profile_id: profile_id,contact_e_mail:contact_e_mail},
success: function(data){
$('#alert_message').hide()
},
error: function(jqXhr, json, errorThrown){// this are default for ajax errors
var errors = jqXhr.responseJSON;
var errorsHtml = '';
$.each(errors['errors'], function (index, value) {
errorsHtml += value;
});
$('#alert_message').text(errorsHtml);
$('#alert_message').show();
}
});
}
</script>