I am sending an email through a form and it works fine.
I have the problem in the validation, that when it is not fulfilled, either because I do not fill in a field, etc; it redirects me to the page and I have to scroll down to the form to see the error fields displayed.
How could I put the block before, so that if it does not comply with the validation that I put, it does not execute the sending yet, it does not try to send the mail and it refreshes the page with the errors, but not the whole one?
Controller :
public function store(FormInfoImaginarq $request)
{
if($validated = $request->validated()){
Mail::to('[email protected]')->send(new InfoImaginarq($request));
return redirect('/#contacto');
} else {
return redirect('/#contacto');
}
}
FormRequest :
public function rules()
{
return [
'name' => 'required',
'email' => 'required|email',
'message' => 'required',
];
}
Form in the View :
{{ Form::open(['method' => 'post' , 'route' => 'infocontacto']) }}
<div class="row">
<div class="input-field col s12">
<i class="material-icons prefix">account_circle</i>
{{ Form::label('name', 'Introduce tu nombre', ['class' => 'icon_prefix', 'required' => 'required']) }}
{{ Form::text('name') }}
@if ($errors->has('name'))
<small class="form-text text-danger">{{ $errors->first('name') }} </small>
@endif
</div>
</div>
<div class="row">
<div class="input-field col s12">
<i class="material-icons prefix">email</i>
{{ Form::label('email', 'Introduzca su email', ['class' => 'icon_prefix1', 'required' => 'required']) }}
{{ Form::email('email') }}
@if ($errors->has('email'))
<small class="form-text text-danger">{{ $errors->first('email') }} </small>
@endif
</div>
</div>
<div class="row">
<div class="input-field col s12">
<i class="material-icons prefix">mode_edit</i>
{{ Form::label('message', 'Escriba su mensaje', ['class' => 'icon_prefix2', 'required' => 'required']) }}
{{ Form::text('message') }}
@if ($errors->has('message'))
<small class="form-text text-danger">{{ $errors->first('message') }} </small>
@endif
</div>
</div>
<div class="center-align">
{{ Form::button('<i class="material-icons right">send</i> Enviar', ['type' => 'submit', 'class' => 'btn waves-effect waves-light deep-purple lighten-2 yellow-text text-accent-1']) }}
</div>
{{ Form::close() }}
One thing you can do on the client side is use HTML5's built-in form validation .
Because of the rules you're putting on the server side, these are the attributes you'll be interested in for your inputs:
required
: Specifies whether a form field must be completed before the form can be submitted.type
: Specifies whether the data should be a number, an email address, or some other type of specific preset.Once added to the corresponding inputs, when an element is invalid and the user tries to submit the data, the browser crashes the form and displays an error message.
Since you're using Laravel Collective, you can specify additional HTML attributes to your inputs, passing them an array with the key values corresponding to those attributes as the third parameter. For the case of the
type
('type' => 'text', 'type' => 'email'
), it is not necessary, since Laravel Collective does it automatically when you declare the inputs withForm::text()
andForm::email()
. It only remains for you to add the attributerequired
:Continuing with the problem you mention, that when validation fails on the server , you are redirected to the page and you have to scroll down to the form to see the error fields displayed, what you can do is the following:
Assign an id attribute to the element you want to point to. This can be, for example, a
<div>
containing the form, or directly in the tag<form>
:And in the FormRequest, override the property
$redirect
, so that it points to the URI of the page where the form is, adding the reference to thisid
concrete (contacto
for the example), adding it to the end of the URI preceded by a hash. So if the form is on a page that loads by accessing/
your project's URI, you override the redirect to'/#contacto'
:References :
Form validation on the client side .
Laravel Collective Text, Text Area, Password & Hidden Fields
Laravel Form Request Validation .
Laravel redirect property on FormRequest class.
Creating hyperlinks Fragments of a document .