Octobercms successful mail send returns empty alert - laravel

I am a newbie to Laravel and OctoberCMS.
I have successfully set up an info request form that sends me an email via Mailgun when submitted. Submission is handled by an AJAX handler.
The problem I have is that the process opens an alert box with an empty array on the form page.
Here is the page: http://imageweaversmarketing.com/
Click on 'CONTACT' in the top-right to move to the form.
Here is the partial with the form:
<div class="row">
<div class="col-lg-12">
<form data-request="onInfoRequest" name="sentMessage" id="contactForm" novalidate="">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="text" class="form-control" placeholder="Your Name *" id="name" required="" data-validation-required-message="Please enter your name.">
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="email" class="form-control" placeholder="Your Email *" id="email" required="" data-validation-required-message="Please enter your email address.">
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="tel" class="form-control" placeholder="Your Phone *" id="phone" required="" data-validation-required-message="Please enter your phone number.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<textarea class="form-control" placeholder="Your Message *" id="message" required="" data-validation-required-message="Please enter a message."></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<div class="clearfix"></div>
<div class="col-lg-12 text-center">
<div id="success"></div>
<button type="submit" class="btn btn-xl">
Send Message
</button>
</div>
</div>
</form>
</div>
Here is the PHP AJAX:
use Illuminate\Support\Facades\Mail;
public function onInfoRequest() {
$data = ['name' => 'test from function'];
Mail::send('mail.inforequest', $data, function ($message) {
$message->to('fake#hidden.com', 'Roger Creasy');
});
}
I appreciate any help.

I see you haven't used the data-request-success handler. That's necessary to show the output when mail is successful. Here's how I do it
function onSend()
{
// Collect input
$name = post('name');
$email = post('email');
// Form Validation
$validator = Validator::make(
[
'name' => $name,
'email' => $email
],
[
'name' => 'required',
'email' => 'required|email'
]
);
if ($validator->fails())
{
$messages = $validator->messages();
throw new ApplicationException($messages->first());
}
// All is well -- Submit form
$to = System\Models\MailSettings::get('sender_email');
$params = compact('name','email');
Mail::sendTo($to, 'temp.website::mail.newrequest', $params);
return true;
}
And on the form side I used
<form class="form-horizontal" data-request="onSend" data-request-success="alert('We have recieved your email.. we\'ll get back to you shortly')">
<div class="form-group">
<label class="col-md-4 control-label" for="name">Name</label>
<div class="col-md-5">
<input id="name" name="name" type="text" placeholder="Name" class="form-control input-md" required="">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="email">Email</label>
<div class="col-md-5">
<input id="email" name="email" type="text" placeholder="Email ID" class="form-control input-md" required="">
</div>
</div>
</form>
So basically all you need is to add a data-request-success for it to work. Enjoy!

I had to get this completed. So, I gave up on a solution within October.
I am posting what I did as a workaround, in case it is helpful to someone else.
What I did is create a route resource, and direct it to a controller to handle the form. basically I went to the Laravel base structure and handled the form through its MVC.

Related

Once the password has been reset, I cannot log into my application

I'm following the documentation on this link about resetting the password:
Reset Password
So first I create the view containing a form just to request the email and once the email has been received I click on the button to reset the password.
So far everything ok! Once I reset the password I try to log into my app with the new password but I cannot log in with this new password.
Can anyone kindly tell me where the problem lies? Thank you all
Route:
Route::get('/forgot-password', [Controller::class,'passwordRequest'])->middleware('guest')->name('password.request');
Route::post('/forgot-password', [Controller::class,'passwordEmail'])->middleware('guest')->name('password.email');
Route::get('/reset-password/{token}', [Controller::class,'passwordReset'])->middleware('guest')->name('password.reset');
Route::post('/reset-password', [Controller::class,'passwordUpdate'])->middleware('guest')->name('password.update');
Controller:
public function passwordRequest() {
return view('auth.forgot-password');
}
public function passwordEmail(Request $request) {
$request->validate(['email' => 'required|email']);
$status = Password::sendResetLink(
$request->only('email')
);
return $status === Password::RESET_LINK_SENT
? back()->with(['status' => __($status)])
: back()->withErrors(['email' => __($status)]);
}
public function passwordReset($token) {
return view('auth.reset-password', ['token' => $token]);
}
public function passwordUpdate(Request $request) {
$request->validate([
'token' => 'required',
'email' => 'required|email',
'password' => 'required|min:8|confirmed',
]);
$status = Password::reset(
$request->only('email', 'password', 'password_confirmation', 'token'),
function ($user, $password) {
$user->forceFill([
'password' => Hash::make($password)
])->setRememberToken(Str::random(60));
$user->save();
event(new PasswordReset($user));
}
);
return $status === Password::PASSWORD_RESET
? redirect()->route('login')->with('status', __($status))
: back()->withErrors(['email' => [__($status)]]);
}
View:
ForgotPassword
<div class="row mt-3 mx-5">
<div class="col-12 col-md-6 namelabel">
<form action="{{route('password.email')}}" method="post">
#csrf
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" name="email">
</div>
<button type="submit" class="btn btn-primary mb-5">Invia</button>
</form>
</div>
</div>
ResetPassword
<div class="row mt-3 mx-5">
<div class="col-12 col-md-6 namelabel">
<form action="{{route('password.email')}}" method="post">
#csrf
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" name="email">
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" name="password">
</div>
<div class="mb-3">
<label for="password_confirmation" class="form-label">Conferma password</label>
<input type="password" class="form-control" name="password_confirmation">
</div>
<div class="mb-3">
<input type="hidden" class="form-control" name="token" value="{{$token}}" >
</div>
<button type="submit" class="btn btn-primary mb-5">Invia</button>
</form>
</div>
</div>
You're almost done! In your auth.reset-password view, you must send the request to the password.update route, not the password.email route.
The password.update route will run the passwordUpdate method to update the User's password.
https://laravel.com/docs/9.x/passwords#password-reset-handling-the-form-submission
<div class="row mt-3 mx-5">
<div class="col-12 col-md-6 namelabel">
<form action="{{ route('password.update') }}" method="post">
#csrf
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" name="email">
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" name="password">
</div>
<div class="mb-3">
<label for="password_confirmation" class="form-label">Conferma password</label>
<input type="password" class="form-control" name="password_confirmation">
</div>
<div class="mb-3">
<input type="hidden" class="form-control" name="token" value="{{$token}}" >
</div>
<button type="submit" class="btn btn-primary mb-5">Invia</button>
</form>
</div>
</div>

Laravel 8 "not required" validation rule

how can I "tell" laravel that if the email field with validation rule 'email' and URL field (for example field where I expect the user to put his Facebook account link but that field IS NOT required) with validation rule 'URL' is not required and it is fine if it is empty. I have put nullable() on all database table columns but my validation rule still applies like it is required. Here are the parts of the code.
This is from migration:
$table->string('site_facebook')->nullable();
$table->string('site_instagram')->nullable();
$table->string('site_twitter')->nullable();
$table->string('site_linkedin')->nullable();
$table->string('site_behance')->nullable();
$table->string('site_dribbble')->nullable();
$table->string('site_email')->nullable();
This part is from validation:
'site_facebook' => 'url',
'site_facebook' => 'url',
'site_instagram' => 'url',
'site_twitter' => 'url',
'site_linkedin' => 'url',
'site_behance' => 'url',
'site_dribbble' => 'url',
'site_email' => 'email',
So, to wrap up my question. I want my URL and email fields to be optional not required :)
EDIT
Since there are some answers and no one work for me I just want to mention (if it does do anything with it), all of my validation rules are in a separate request...
class ManageSiteSettingsRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'site_name' => 'required|max:50',
'site_facebook' => 'nullable|url',
'site_facebook' => 'url',
'site_instagram' => 'url',
'site_twitter' => 'url',
'site_linkedin' => 'url',
'site_behance' => 'url',
'site_dribbble' => 'url',
'site_email' => 'email',
'site_creator_link' => 'url',
];
}
}
This is the form:
#extends('layouts.admin')
#section('content')
<h2>Site settings</h2>
#include('includes._errors')
#include('includes._sessions')
<form action="{{ route('settings.update') }}" method="post">
#csrf
<div class="form-group">
<label for="site_name">Site name</label>
<input type="text" name="site_name" class="form-control" required placeholder="This field is required!" value="{{ $settings->site_name }}">
</div>
<div class="form-group">
<label for="site_description">Site description</label>
<textarea name="site_description" class="form-control mytinytext" id="site_description" cols="30" rows="10">{!! $settings->site_description !!}</textarea>
</div>
<div class="form-group">
<label for="site_facebook">Site facebook page</label>
<input type="text" name="site_facebook" class="form-control" placeholder="https://www.facebook.com/" value="{{ $settings->site_facebook }}">
</div>
<div class="form-group">
<label for="site_instagram">Site instagram page</label>
<input type="text" name="site_instagram" class="form-control" placeholder="https://www.instagram.com/" value="{{ $settings->instagram }}">
</div>
<div class="form-group">
<label for="site_twitter">Site twitter page</label>
<input type="text" name="site_twitter" class="form-control" placeholder="https://twitter.com/" value="{{ $settings->site_twitter }}">
</div>
<div class="form-group">
<label for="site_linkedin">Site linkedin page</label>
<input type="text" name="site_linkedin" class="form-control" placeholder="https://www.linkedin.com/" value="{{ $settings->site_linkedin }}">
</div>
<div class="form-group">
<label for="site_behance">Site behance page</label>
<input type="text" name="site_behance" class="form-control" placeholder="https://www.behance.net/" value="{{ $settings->site_behance }}">
</div>
<div class="form-group">
<label for="site_dribbble">Site dribbble page</label>
<input type="text" name="site_dribbble" class="form-control" placeholder="https://dribbble.com/" value="{{ $settings->site_dribbble }}">
</div>
<div class="form-group">
<label for="site_email">Site email</label>
<input type="text" name="site_email" class="form-control" placeholder="...#outlook... / ...#hotmail... / ...#gmail..." value="{{ $settings->site_email }}">
</div>
<hr class="divider">
<div class="form-group">
<label for="site_field_one">First footer field title</label>
<input type="text" name="site_field_one" class="form-control" placeholder="PHONE" value="{{ $settings->site_field_one }}">
</div>
<div class="form-group">
<label for="site_field_one_value">First footer field value</label>
<textarea name="site_field_one_value" id="site_field_one_value" class="mytinytext form-control" placeholder="+80 (0)5 22 55 66 77">{{ $settings->site_field_one_value }}</textarea>
</div>
<hr class="divider">
<div class="form-group">
<label for="site_field_two">Second footer field title</label>
<input type="text" name="site_field_two" class="form-control" placeholder="ADDRESS" value="{{ $settings->site_field_two }}">
</div>
<div class="form-group">
<label for="site_field_two_value">Second footer field value</label>
<textarea name="site_field_two_value" id="site_field_two_value" class="mytinytext form-control" placeholder="33 rue Burdeau 69089, Paris France">{{ $settings->site_field_two_value }}</textarea>
</div>
<hr class="divider">
<div class="form-group">
<label for="site_field_three">Third footer field title</label>
<input type="text" name="site_field_three" class="form-control" placeholder="ENQUIRUES" value="{{ $settings->site_field_three }}">
</div>
<div class="form-group">
<label for="site_field_three_value">Third footer field value</label>
<textarea name="site_field_three_value" id="site_field_three_value" class="mytinytext form-control" placeholder="email#gmail.com">{{ $settings->site_field_three_value }}</textarea>
</div>
<hr class="divider">
<div class="form-group">
<label for="site_field_four">Fourth footer field title</label>
<input type="text" name="site_field_four" class="form-control" placeholder="WORK HOURS" value="{{ $settings->site_field_four }}">
</div>
<div class="form-group">
<label for="site_field_four_value">Fourth footer field value</label>
<textarea name="site_field_four_value" id="site_field_four_value" class="mytinytext form-control" placeholder="Weekdays: 09:00 - 18:00 Weekends: 11:00 - 17:00">{{ $settings->site_field_four_value }}</textarea>
</div>
<hr class="divider">
<div class="form-group">
<label for="site_copyright">Site copyright</label>
<input type="text" name="site_copyright" class="form-control" placeholder="Created with love by _______ (fill 'Site creator name' field)" value="{{ $settings->site_copyright }}">
</div>
<hr class="divider">
<div class="form-group">
<label for="site_creator_name">Site creator name</label>
<input type="text" name="site_creator_name" class="form-control" placeholder="John Doe" value="{{ $settings->site_creator_name }}">
</div>
<div class="form-group">
<label for="site_creator_link">Site creator page</label>
<input type="text" name="site_creator_link" class="form-control" placeholder="https://site-creator-page.com" value="{{ $settings->site_creator_link }}">
</div>
<div class="form-group">
<button type="submit" name="btn_update_site_settings" class="btn btn-block btn-success">Update site settings</button>
</div>
</form>
#include('includes._errors')
#endsection
You can use nullable rule
Like the example below:
'site_facebook' => 'url|nullable',

Message sending through session isn't showing

In my laravel-7 application I am sending custom message for invalid login in blade through controller. but message is not showing in blade. also tail me if I am doing anything wrong.
in blade
<div class="card-body">
#if(session()->has('message'))
<div class="alert alert-{{session('type')}}">
<li>{{session('message')}}</li>
</div>
#endif
<form action="{{route('login')}}" method="post" class="form">
#csrf
<div class="form-group">
<label for="email">Email address</label>
<input type="email" name="email" class="form-control" id="email" placeholder="Enter email" value="{{old('email')}}">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" name="password" class="form-control" id="password" placeholder="Password">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-block">Login</button>
</div>
</form>
in auth controller
public function processlogin(Request $request)
{
$this->validate($request,[
'email'=>'required|email',
'password'=>'required|min:6',
]);
$credentials=$request->except(['_token']);
if (Auth::attempt($credentials)){
return redirect()->route('index');
}
$this->setErrorMessage('Invalid Credentials.');
return redirect()->back();
}
in controller
public function setErrorMessage($message):void
{
session()->flash($message);
session()->flash('type','danger');
}
You are not naming the flashed variable message. You are passing the value of $message as the first argument to flash which is the name of the key.
session()->flash('message', $message);

Contact us page receive information but not getting message

See the contact us page here.
This is view file of contact page.
<div class="section-title">
<h2>Contact</h2>
<h3><span>Contact Us</span></h3>
</div>
<div class="row" data-aos="fade-up" data-aos-delay="100">
</div>
<div class="row justify-content-center" data-aos="fade-up" data-aos-delay="100">
<div class="col-lg-9">
<form action="{{route('contact.store')}}" method="post" role="form" class="php-email-form">
#csrf
<div class="form-row">
<div class="col form-group">
<input type="text" name="name" class="form-control" id="name" placeholder="Your Name" data-rule="minlen:4" data-msg="Please enter at least 4 chars" />
<div class="validate"></div>
</div>
<div class="col form-group">
<input type="email" class="form-control" name="email" id="email" placeholder="Your Email" data-rule="email" data-msg="Please enter a valid email" />
<div class="validate"></div>
</div>
</div>
<div class="form-group">
<input type="text" class="form-control" name="subject" id="subject" placeholder="Subject" data-rule="minlen:4" data-msg="Please enter at least 8 chars of subject" />
<div class="validate"></div>
</div>
<div class="form-group">
<textarea class="form-control" name="message" rows="5" data-rule="required" data-msg="Please write something for us" placeholder="Message"></textarea>
<div class="validate"></div>
</div>
<div class="mb-3">
<div class="loading">Loading</div>
<div class="sent-message">Your message has been sent. Thank you!</div>
</div>
<div class="text-center"><button type="submit">Send Message</button></div>
</form>
</div>
</div>
</div>
</section><!-- End Contact Section -->
This is Controller code of contact us page.
public function store(Request $request)
{
Contact::create([
'name' => $request->name,
'email' => $request->email,
'subject' => $request->subject,
'message' => $request->message,
]);
\Session::flash('flash_message','successfully saved.');
return redirect()->back();
}
Problem is i have a one page theme so i redirect same page after contact us details fill. But contact details here stay , not blank....what is it solution?
don't use return back try by using return view
return view('viewName');
Because your browser will do "back" when you use your code:
return redirect()->back();
Change to use this:
return redirect()->route('your_back_route_name');
I think that will work as reload page.
When it's not work, i think it is about your browser auto fill.
So, set you input value is empty when load.
<input value="" />

What are all the steps I have to do to get my bootstrap email form working?

What should I do to get my form working? My website and everything is set up. I just can't get my form working. Can I do it from rdp? or firezila server?
<form>
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="form-group">
<input type="text" class="form-control" required="required" placeholder="Name">
</div>
</div>
<div class="col-md-6 col-sm-6">
<div class="form-group">
<input type="text" class="form-control" required="required" placeholder="Email address">
</div>
</div>
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="form-group">
<textarea name="message" id="message" required="required" class="form-control" rows="3" placeholder="Message"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default">Submit Request</button>
</div>
</div>
</div>
</div>
</form>
Submit your form to php and send the mail, ulter the code as below and consider adding a subject field too:
<form name="nameforform" action="thephpfilename.php" method="POST">
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="form-group">
<input type="text" class="form-control" required="required" placeholder="Name" name="name">
</div>
</div>
<div class="col-md-6 col-sm-6">
<div class="form-group">
<input type="text" class="form-control" required="required" placeholder="Email address" name="email">
</div>
</div>
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="form-group">
<textarea name="message" id="message" required="required" class="form-control" rows="3" placeholder="Message" name="message"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default">Submit Request</button>
</div>
</div>
</div>
</div>
</form>
Then create a php file (eg: thefilename.php) [besure to change the action attribute of the form element to that of the created php file's name.
The php file should have the following code:
<?php
$to = $_POST['email'];
$subject = '';
$message = $_POST['message'];
$headers = 'From:'. $_POST['name'] . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
It would work, be sure to tweak your server's settings and also specify more info in the question next time.
Lastly how it works,
On submitting the form the browser sends a get/post request to the file specified on the action attribute of the form element. The php
file handles the request and does the needy. The data is stored with
reference to the name attribute of the input and textarea elements.
The data can be stored as arrays too. Here we use the php's mail()
function to send the mail using appropriate headers. HTML or bootstrap
cannot send mail independently without server intervention.
Be sure to comment for more info and other help, enjoy out there.

Resources