Email Sending and Validation Failing in Laravel 5.4 - validation

I am creating a contact form using bootstrap3 build on Laravel 5.4. When I click the submit button, I expect an email to be sent to my inbox or if there are errors they should be validated on the back end and errors displayed at the top of the form. I am using Laravel collective to build the form, when I fill the form and click the submit button, the page only reloads and no validation happens or in case of correct input no email is sent. Please assist?
Form Section
<div class="col-sm-4 wow animated fadeInLeft">
<div id="success" class="col-sm-12">
#if(Session::has('success'))
<span class="alert alert-success" role="alert">
<strong> Success: </strong> {{ Session::get('success') }}
</span>
#endif
#if(count($errors) > 0)
<span class="alert alert-danger" role="alert">
<strong> Errors: </strong>
<ul>
#foreach($errors->all() as $error)
<li> {{ $error }} </li>
#endforeach
</ul>
</span>
#endif
</div>
{!! Form::open(array('route' => 'index.post', 'method' => 'POST','class' => 'contact-form')) !!}
{{ Form::text('name', null, array( 'placeholder' => 'Name...', 'class' => 'input', 'required' => ''))}}
{{ Form::email('email', null, array('placeholder' => 'Email Address...','class' => 'input', 'required' => ''))}}
{{ Form::textarea('message', null, array('placeholder' => 'Message...', 'class' => '', 'required' => 'input')) }}
{{ Form::submit('Submit') }}
{!! Form::close() !!}
</div>
Routes File
Route::post('/', 'PagesController#postIndex') ->name('index.post');
Route::get('/', 'PagesController#getIndex') ->name('pages.index');
PagesController
public function postIndex(Request $request){
$this->validate($request, array(
'name' => 'required|min:10',
'email' => 'required|email',
'message' => 'required|min:100'
));
$name = $request->name;
$data = array(
'name' => $request->name,
'email' => $request->email,
'bodymessage' => $request->message
);
Mail::send('emails.contact', $data, function($message) use ($data) {
$message->from($data['email']);
$message->to('info#pwebk.com');
});
Session::flash('success', 'Hello '.$name.', Your Form was successfully sent');
return redirect()->route('pages.index');
}
public function getIndex(){
return view('pages.welcome');
}

Related

Laravel 8.0 Error "Action App\Http\Controllers\SkladController not defined."

This is my problem in SkladController. I want to fetch data from the DB but it does show me anything because I have that error in the picture.
SkladController
class SkladController extends Controller
{
public function index()
{
$sklads = Sklad::all();
return view('sklads.index')->with('sklads', $sklads);
}
public function create()
{
return view('sklads.index');
}
public function store(Request $request)
{
$this->validate($request, [
'datle' => 'required',
'mandle' => 'required',
'marcipan' => 'required',
'orechy' => 'required',
]);
//vytvorit v sklade
$sklads = new Sklad;
$sklads->datle = $request->input('datle');
$sklads->mandle = $request->input('mandle');
$sklads->marcipan = $request->input('marcipan');
$sklads->orechy = $request->input('orechy');
$sklads->save();
return redirect('/sklad')->with('success', 'Uložené');
}
}
index.blade.php
<h3> Príjem v sklade</h3>
<br>
{!! Form::open(['action' => 'App\Http\Controllers\SkladController', 'method' => 'POST']) !!}
<div class="row">
<div class="form-group col-md-2">
{{ Form::number('datle', '', ['class' => 'form-control', 'placeholder' => 'Ďatle']) }}
</div>
<div class="form-group col-md-2">
{{ Form::number('mandle', '', ['class' => 'form-control', 'placeholder' => 'Mandle']) }}
</div>
<div class="form-group col-md-2">
{{ Form::number('marcipan', '', ['class' => 'form-control', 'placeholder' => 'Marcipán']) }}
</div>
<div class="form-group col-md-2">
{{ Form::number('orechy', '', ['class' => 'form-control', 'placeholder' => 'Orechy']) }}
</div>
<div class="form-group col-md-2">
{{ Form::submit('Submit', ['class' => 'btn btn-primary']) }}
</div>
</div>
{!! Form::close() !!}
#if(count($sklads) > 0)
#foreach($sklads as $sklad)
<br>
{{$sklad->datle}} {{$sklad->mandle}} {{$sklad->marcipan}} {{$sklad->orechy}}
#endforeach
#else
<p>nenasli sa zaznamy</p>
#endif
web.php
Route::resource('/sklad', App\Http\Controllers\SkladController::class)
->except(['create', 'store', 'update', 'destroy']);
The issue is the action in
{!! Form::open(['action' => 'App\Http\Controllers\SkladController', 'method' => 'POST']) !!}
It should be
'action' => 'App\Http\Controllers\SkladController#index'
Usually you'd use the store method but you only don't have it according to your route declaration.
or you could use route instead.
{!! Form::open(['route' => 'sklads.index', 'method' => 'POST']) !!}

Alert Message not showing in Laravel 5.4

I have got a contact form that is build using Laravel 5.4, parsley.js and Bootstrap 3, it works fine but it does not display a success message at the top of the form on successful delivery of a message or display any errors in case there is an error. Please assist?
Contoller
public function postIndex(Request $request){
$this->validate($request, array(
'name' => 'required|min:10',
'email' => 'required|email',
'message' => 'required|min:100'
));
$name = $request->name;
$data = array(
'name' => $request->name,
'email' => $request->email,
'bodymessage' => $request->message
);
Mail::send('emails.contact', $data, function($message) use ($data) {
$message->from($data['email']);
$message->to('info#kapsol.com');
});
Session::flash('success', 'Hello $name, Your Form was successfully sent');
return redirect()->route('pages.index');
}
Index.blade.php
<div class="col-sm-4">
{!! Form::open(array('route' => 'index.post', 'class' => 'contact-form', 'data-parsley-validate' => '')) !!}
<div id="success">
<div class="col-sm-12">
#if(Session::has('success'))
<div class="alert alert-success" role="alert">
<strong> Success: </strong> {{ Session::get('success') }}
</div>
#endif
#if(count($errors) > 0)
<div class="alert alert-danger" role="alert">
<strong> Errors: </strong>
<ul>
#foreach($errors->all() as $error)
<li> {{ $error }} </li>
#endforeach
</ul>
</div>
#endif
</div>
</div>
{{ Form::text('name', null, array( 'placeholder' => 'Name...', 'class' => 'input', 'required' => '', 'minlength' => '10'))}}
{{ Form::email('email', null, array('placeholder' => 'Email Address...','class' => 'input', 'required' => '', 'type' => 'email'))}}
{{ Form::textarea('message', null, array('placeholder' => 'Message...', 'class' => '', 'required' => 'input', 'minlength' => '100')) }}
{{ Form::submit('Submit') }}
{!! Form::close() !!}
</div>
Route
Route::get('/', 'PagesController#getIndex') ->name('pages.index');
Route::post('/', 'PagesController#postIndex') ->name('index.post');
You are redirecting to the same route your form is posting. I assume that the route that render the view is a GET route and have another name, you should redirect to that route or the route that renders index.blade.php
This is why you have the with() method to chain the route() method on. So rather than use Session:flash(), you can simply add a with() method that flashes that message to the session for the next request:
return redirect()->route('pages.index')->with('success', 'Hello'. $name.', Your Form was successfully sent');
Or rather if your form was originally from the index page, then you simply don't have to remember the name of the route, simply use back() helper method, i.e:
return back()->with('success', 'Hello'. $name.', Your Form was successfully sent');

htmlentities error when passing from a modal

New to Laravel, please bare with.
Error:
htmlentities() expects parameter 1 to be string, object given (View: /var/www/html/willow/resources/views/emails/valuation.blade.php)
The modal from which it is being sent:
{!! Form::open(['action' => ['EnquiryController#valuationRequest']]) !!}
<div class="form-group">
{!! Form::text('name', null, ['class' => 'form-control has-feedback', 'placeholder' => 'Name']) !!}
</div>
<div class="form-group">
{!! Form::text('email', null, ['class' => 'form-control has-feedback', 'placeholder' => 'Email Address']) !!}
</div>
<div class="form-group">
{!! Form::text('telephone', null, ['class' => 'form-control has-feedback', 'placeholder' => 'Telephone Number']) !!}
</div>
<div class="form-group">
{!! Form::text('house_number', null, ['class' => 'form-control has-feedback', 'placeholder' => 'House name / number']) !!}
</div>
<div class="form-group">
{!! Form::text('postcode', null, ['class' => 'form-control has-feedback', 'placeholder' => 'Postcode']) !!}
</div>
<div class="form-group">
{!! Form::textarea('message', null, ['class' => 'form-control has-feedback', 'placeholder' => 'Message', 'rows' => '5']) !!}
</div>
<div class="form-group">
<input type="submit" class="button black" value="Register">
</div>
{!! Form::close() !!}
and the function:
public function valuationRequest(ValuationRequest $request)
{
// dd($request->all());
Mail::send('emails.valuation',
['name' => $request['name'],
'email' => $request['email'],
'telephone' => $request['telephone'],
'house_number' => $request['house_number'],
'postcode' => $request['postcode'],
'message' => $request['message'],
],
function ($message) use ($request) {
$message->to('paolo#bigg.co.uk', 'Paolo Resteghini')->subject('Valuation Request - Willow Lettings');
});
Session::flash('flash_message', 'Your request has been sent.');
return redirect(URL::previous());
}
The contents of the DD are perfect. All of the requests are populated as expected, but when trying to go through the rest of the function it fails with the error above.
emails.valuation:
Hello, <br><br>
You have received a new valuation request via the Willow Lettings website. Here they are: <br><br>
<b>Name:</b> {{ $name }}<br>
<b>Email:</b> {{ $email }}<br>
<b>Phone:</b> {{ $telephone }}<br>
<b>House number:</b> {{ $house_number }}<br><br>
<b>Postcode:</b> {{ $postcode }}<br><br>
{{ $message }}
Most likely, this is a problem with your message variable. As you can see from the docs:
Note: A $message variable is always passed to e-mail views, and allows the inline embedding of attachments. So, you should avoid passing a message variable in your view payload.
In other words, you should change message into something else like msg.
'msg' => $request['message'],
Then, in your blade file, reflect that change:
{{ $msg }}

Laravel validation not showing the default error messages

I am working on a laravel project and I am having some issues with the validation.
The default error messages dont appear instead I get to see the validation requirements like this: http://gyazo.com/681e9d8e2e176a29d90db041354f7177
this is my code:
routes.php (I put all the code in here for now)
Route::filter('checkLogin', function()
{
if(Input::GET('email') != ""){ //register
$rules =
array(
'username' => 'required|max:64|min:3|unique:users',
'password' => 'required|max:64|min:6',
'fname' => 'required|max:255|alpha',
'lname' => 'required|max:255|alpha',
'email' => 'required|max:255|email',
'phone' => 'max:24|min:9',
'zip' => 'required',
'street' => 'required|max:255|alpha',
'housenumber' => 'required|max:6|numeric',
'country' => 'required',
'avatar' => 'max:32'
);
$validator = Validator::make(Input::all(), $rules);
if($validator->fails()) {
return Redirect::to('/')->withInput()->withErrors($rules);
}
}
});
this is how the code from the view:
<div class="fields">
<div class="field">
<i class="fa fa-user"></i>
{{ Form::text('username', null, ['placeholder' => 'Username', 'tabindex' => 1]) }}
{{ $errors->first('username') }}
</div>
<div class="field">
<i class="fa fa-lock"></i>
{{ Form::password('password', ['placeholder' => 'Password', 'tabindex' => 2]) }}
{{ $errors->first('password') }}
</div>
</div>
When validation fails, you are returning your $rules as the errors. Change this line:
return Redirect::to('/')->withInput()->withErrors($rules);
to this:
return Redirect::to('/')->withInput()->withErrors($validator);

MethodNotAllowedHttpException Laravel

I'm having trouble with an error that I'm getting in laravel. When I run my code on localhost I don't have any issues, but when I placed laravel in demo live server which is server ('https') I get the MethodNotAllowedHttpException error.
Here's my code for my route
Route::post('post_reminder', function(){
$creds = array(
'email' => Input::get('email')
);
$rules = array(
'email' => 'required|email'
);
$messages = array(
'email' => 'The :attribute needs to be an real email'
);
$validator = Validator::make($creds, $rules,$messages);
if($validator->fails())
{
return Redirect::route('getReminder')->withErrors($validator);
}
else
{
return Password::remind($creds);
}
});
And here's the form code
<div id="reset_container">
{{ Form::open(array('url' => 'post_reminder')) }}
<h1 id="pass_recovery_text">Password Recovery Form</h1>
<p>
#if (Session::has('error'))
<li id="error">{{ trans(Session::get('reason')) }}</li>
#elseif (Session::has('success'))
<li id="error">An e-mail with the password reset has beensent.</li>
#endif
#foreach($errors->all() as $error)
<li id="error">{{ $error }}</li>
#endforeach
{{ Form::label('email', 'Please enter you email: ') }}
{{ Form::text('email','',array('id' => 'forgot')) }}
{{ Form::submit('Reset') }}<br /><br /><br />
{{ HTML::link('/', 'Have a account Sign-In', array('id' => 'sign-in')) }}
</p>
{{ Form::close() }}
</div>
<div id="reset_container">
{{ Form::open(array('url' => 'post_reminder','method' => 'post')) }}
<h1 id="pass_recovery_text">Password Recovery Form</h1>
<p>
#if (Session::has('error'))
<li id="error">{{ trans(Session::get('reason')) }}</li>
#elseif (Session::has('success'))
<li id="error">An e-mail with the password reset has beensent.</li>
#endif
#foreach($errors->all() as $error)
<li id="error">{{ $error }}</li> #endforeach
{{ Form::label('email', 'Please enter you email: ') }}
{{ Form::text('email','',array('id' => 'forgot')) }}
{{ Form::submit('Reset') }}
{{ HTML::link('/', 'Have a account Sign-In', array('id' => 'sign-in')) }} </p>
{{ Form::close() }}
Have you tried force the route to be served over https?
Route::post('post_reminder', array('https', function(){
$creds = array(
'email' => Input::get('email')
);
$rules = array(
'email' => 'required|email'
);
$messages = array(
'email' => 'The :attribute needs to be an real email'
);
$validator = Validator::make($creds, $rules,$messages);
if($validator->fails())
{
return Redirect::route('getReminder')->withErrors($validator);
}
else
{
return Password::remind($creds);
}
}));
Referencing to http://laravel.com/docs/4.2/routing here.

Resources