Laravel form validation doesn't seem to work - php

I'm making my first form in Laravel and the generation of the form is all working. It's just that the store function seems to blindly return the user to my contact page irrelevant of the result of the form validation.
So if the email address posted isn't an email but a random string I still get returned to the /contact page with the thank you message being sent to the view.
My controller looks like this:
namespace App\Http\Controllers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\DB;
use App\Http\Requests\ContactFormRequest;
class ContactController extends Controller {
public function create(){
return view('contact');
}
public function store(ContactFormRequest $request) {
return \Redirect::route('contact')
->with('message', 'Thanks for contacting us!');
}
}
And the form handler like this:
namespace App\Http\Requests;
use Illuminate\Http\Request;
class ContactFormRequest extends Request {
public function authorize() {
return true; // don't need to be registered to run form so true
}
public function rules() {
return [
'email' => 'required|email',
];
}
}
This is controlled by the following routes
if (config('app.tan_site_page_contact')===true){
Route::get('/contact', ['as' => 'contact', 'uses' => 'ContactController#create']);
Route::post('/contact', ['as' => 'contact_store', 'uses' => 'ContactController#store']);
});
And the form like this:
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
#if(Session::has('message'))
<div class="alert alert-info">
{{Session::get('message')}}
</div>
#endif
{!! Form::open(array('route' => 'contact_store', 'id' => 'contactCallMeForm')) !!}
{!! Form::label('Your E-mail Address') !!}
{!! Form::text('email', null, array('required', 'class'=>'form-control', 'placeholder'=>'Your e-mail address')) !!}
{!! Form::submit('Contact Us!', array('class'=>'btn btn-primary')) !!}
{!! Form::close() !!}
The form html seems fine,with a token and valid url
<form method="POST" action="http://localhost/contact" accept-charset="UTF-8" id="contactCallMeForm" novalidate="">
<input name="_token" type="hidden" value="VNHchLZhfsXadVZXCZWHGdAuJ4zgmO6cDJIGhR59">
<label for="Your E-mail Address">Your E-mail Address</label>
<input required="required" class="form-control" placeholder="Your e-mail address" name="email" type="text">
<input class="btn btn-primary" type="submit" value="Contact Us!">
</form>

The problem here is your ContactFormRequest class.
You extends it from invalid Request class. You extend it from \Illuminate\Http\Request class but you should extend it from \Illuminate\Foundation\Http\FormRequest (or \App\Http\Requests\Request class)

Related

Submitting a request in Laravel and sending mail

I am trying to submit a form and send an email based on data submitted through the form, but it doesn't seem to be working because I don't get back the success message.
Controller
public function contact(Request $request)
{
$request = request()->validate([
'name' => 'required',
'email' => 'required| email',
'message' => 'required | max:1000',
]);
Mail::to('support#mail.com')->send(new contact($request));
return redirect()->back()->with("success", "You email has successfully been sent");
}
Form
#if (session('error'))
<div class="alert alert-danger">
{{ session('error') }}
</div>
#endif
#if (session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
#endif
<form action="/contact" method="POST">
#csrf
<div class="col-md-12">
<input type="text" placeholder="Full Name" required name="name">
</div>
<div class="col-md-8">
<input type="text" placeholder="email" required name="email">
</div>
<div class="col-md-2">
<textarea name="message"></textarea>
</div>
<div class="col-md-12">
<button class="btn btn-black no-margin-bottom btn-small"
type="submit">Contact</button>
</div>
</form>
Route
`Route::post('/contact', 'HomeController#contact');`
If you're being redirected back to the form page without error messages or your sucess flash message then I am inclined to think the problem is in your template. Assuming you are using the laracasts/flash package your method should look like this
public function contact(Request $request)
{
$request = request()->validate([
'name' => 'required',
'email' => 'required| email',
'message' => 'required | max:1000'
]);
Mail::to('support#mail.com')->send(
new contact($request)
);
return redirect()->back()->with("success", "You email has successfully been sent");
}
redirect()->with() adds data to the session but it doesn't display a message unless you manually do so in the template like this
#if (session('sucess'))
<div class="alert alert-success">
{{ session('success') }}
</div>
#endif
Make sure you're displaying errors properly in your form, replace your session('error') block with the following:
#if($errors->any())
<div class="alert alert-danger">
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
In your controller:
use App\Mail\Contact;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
class HomeController extends Controller
{
// ...
public function contact(Request $request)
{
// Note I'm not overwriting the $request variable.
// This method returns an array of the validated data.
$request->validate([
'name' => 'required',
'email' => 'required|email',
'message' => 'required|max:1000'
]);
Mail::to('support#mail.com')->send(new Contact($request));
return back()->with('success', 'Your email has successfully been sent');
}
}
Your mail class should look something like this:
namespace App\Mail;
use Illuminate\Http\Request;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class Contact extends Mailable
{
use Queueable, SerializesModels;
protected $request;
public function __construct(Request $request)
{
$this->request = $request;
}
public function build()
{
// The request instance must be passed to the view...
return $this->markdown('emails.contact', [
'request' => $this->request
]);
}
}
You can now use the Request object in your email markdown (emails.contact). For example:
**Name**<br>
{{ $request->input('name') }}
**Email**<br>
{{ $request->input('email') }}
**Message**<br>
{{ $request->input('message') }}

Laravel 5.2 : MethodNotAllowedHttpException in RouteCollection.php line 219

I want to save one form data through my task controller. But when i go to url to access my form. it's showing the following Error:
MethodNotAllowedHttpException in RouteCollection.php line 219:
Here is my Routes.php
<?php
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/', function () {
return view('welcome');
});
Route::get('/all_item','TestController#index');
Route::post('/create_item','TestController#create');
Route::get('/home', 'HomeController#index');
});
Here is my TaskController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Test;
use App\Http\Requests;
use Redirect;
class TestController extends Controller
{
public function index()
{
$alldata=Test::all();
// return $alldata;
return view('test.itemlist',compact('alldata'));
}
public function create()
{
return view('test.create_item');
}
public function store(Request $request)
{
$input = $request->all();
Test::create($input);
return redirect('test');
}
}
Here is the create_item page( post form / view page)
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-heading">Create Item</div>
{!! Form::open(array('route' => 'Test.store','class'=>'form-horizontal','method' => 'patch')) !!}
{!! Form::token(); !!}
<?php echo csrf_field(); ?>
<div class="form-group">
<label>Item Code</label>
<input type="text" name="item_code" class="form-control" placeholder="Code">
</div>
<div class="form-group">
<label>Item Name</label>
<input type="text" name="item_name" class="form-control" placeholder="Name">
</div>
<button type="submit" class="btn btn-default">Submit</button>
{!! Form::close() !!}
</div>
</div>
</div>
</div>
#endsection
You're using PATCH method in form, but route with POST method
try
'method' => 'patch'
change to
'method' => 'post'
LaravelCollective's HTML only supports methods POST, GET, PUT DELETE
so you might want to change that to POST or PUT
'method' => 'POST'
You haven't declared a Test.store route in your Routes.php, so try adding a resource or a named route:
Route::post('/store_item', [
'as' => 'Test.store', 'uses' => 'TestController#store'
]);
As i can see TestController#create is a post method.But it behaves like a get method.Try passing Request $request parameter to the create method.Or else if you really need a get method for the create method, change the method as get in Routes.php as like this,
Route::get('/create_item','TestController#create');

Laravel 5.1 $this->validate(...) throws error

I'm trying to add validation to my resource controller using the laravel's validation (http://laravel.com/docs/5.1/validation) but I get this error:
ErrorException in ValidatesRequests.php line 30:
Argument 1 passed to App\Http\Controllers\Controller::validate() must be an
instance of Illuminate\Http\Request,
instance of Illuminate\Support\Facades\Request given,
called in
/Users/lextoc/Documents/Sites/partyrecycler/app/
Http/Controllers/MarkerController.php on line 30 and defined
This is the controller:
namespace App\Http\Controllers;
use Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Marker;
class MarkerController extends Controller
{
...
public function create()
{
return view('markers.create');
}
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|max:255',
'x' => 'required',
'y' => 'required',
]);
$marker=Request::all();
Marker::create($marker);
return redirect('markers');
}
...
}
And the view:
<h1>Create marker</h1>
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
{!! Form::open(array('route' => 'markers.store')) !!}
{!! csrf_field() !!}
<div>
Name
<input type="text" name="name">
</div>
<div>
x
<input type="text" name="x">
</div>
<div>
y
<input type="text" name="y">
</div>
<div>
<button type="submit">Create</button>
</div>
{!! Form::close() !!}
I don't know why it's using the wrong Request class, and why are there two being used in the controller?
The error is due to your include headers:
Try
use Illuminate\Http\Request;
Instead of
use Request;
Example:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Marker;

Laravel 5 MethodNotAllowedHttpException in RouteCollection.php line 201:

I have a number of php files in my project:
admin.blade.php: this files contains the admin form.
When called it show the following error:
MethodNotAllowedHttpException in RouteCollection.php line 201
<h2>Please Log In To Manage</h2>
<form id="form1" name="form1" method="post" action="<?=URL::to('/admin')?>">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
User Name:<br />
<input name="username" type="text" id="username" size="40" />
<br /><br />
Password:<br />
<input name="password" type="password" id="password" size="40" />
<br />
<br />
<br />
<input type="submit" name="button" id="button" value="Log In" />
</form>
In route.php, this call is made:
Route::get('/admin',array('uses'=>'student#admin'));
This is the function in student.php
public function admin()
{
return View::make('student.admin');
$validator = Validator::make($data = Input::all() , User::rules());
if ($validator->fails())
{
return Redirect::back()->withErrors($validator)->withInput();
}
else
{
$check = 0;
$check = DB::table('admin')->get();
$username = Input::get('username');
$password = Input::get('password');
if (Auth::attempt(['username' => $username, 'password' => $password]))
{
return Redirect::intended('/');
}
return Redirect::back()->withInput()->withErrors('That username/password combo does not exist.');
}
}
I don't know much about creating an admin area, I am just trying to create it.
This is how I do it.
Routes.php
Route::get('/admin', 'UsersController#getAdminLogin');
Route::get('/admin/dashboard', 'UsersController#dashboard');
Route::post('/admin', 'UsersController#postAdminLogin');
admin_login.blade.php
{!! Form::open(['url' => '/admin']) !!}
<div class="form-group">
{!! Form::label('email', 'Email Id:') !!}
{!! Form::text('email', null, ['class' => 'form-control input-sm']) !!}
</div>
<div class="form-group">
{!! Form::label('password', 'Password') !!}
{!! Form::password('password', ['class' => 'form-control input-sm']) !!}
</div>
<div class="form-group">
{!! Form::submit('Login', ['class' => 'btn btn-primary btn-block']) !!}
</div>
{!! Form::close() !!}
dashboard.blade.php
<h4 class="text-center">
Welcome {{ Auth::user()->full_name }}
</h4>
UsersController.php
/**
* Display the admin login form if not logged in,
* else redirect him/her to the admin dashboard.
*
*/
public function getAdminLogin()
{
if(Auth::check() && Auth::user()->role === 'admin')
{
return redirect('/admin/dashboard');
}
return view('admin_login');
}
/**
* Process the login form submitted, check for the
* admin credentials in the users table. If match found,
* redirect him/her to the admin dashboard, else, display
* the error message.
*
*/
public function postAdminLogin(Request $request)
{
$this->validate($request, [
'email' => 'required|email|exists:users,email,role,admin',
'password' => 'required'
]);
$credentials = $request->only( 'email', 'password' );
if(Auth::attempt($credentials))
{
return redirect('/admin/dashboard');
}
else
{
// Your logic of invalid credentials.
return 'Invalid Credentials';
}
}
/**
* Display the dashboard to the admin if logged in, else,
* redirect him/her to the admin login form.
*
*/
public function dashboard()
{
if(Auth::check() && Auth::user()->role === 'admin')
{
return view('admin.dashboard');
}
return redirect('/admin');
}
Your Code:
In routes.php, you have only 1 route, i.e.,
Route::get('/admin',array('uses'=>'student#admin'));
And there is no declaration of post method, hence, the MethodNotAllowedHttpException
Also, in your controller, you are returning the view first and then processing the form which is not going to work at all. You first need to process the form and then return the view.
public function admin(){
// Won't work as you are already returning the view
// before processing the admin form.
return \View::make(students.admin);
// ...
}
As #Sulthan has suggested, you should use Form Facade. You can check out this video on Laracasts about what Form Facade is and how you should use it.
You're using post method in the form but you're having get method in the routes.
So, Change the method to post in your routes
Note :
I recommend you to make use of the default form opening of Laravel like the below given which is always the best practise
{!! Form::open(array('url' => 'foo/bar')) !!}
{!! Form::close() !!}
Tips :
Read more on here and try to debug such things by comparing the methods and routes.
Form facade is not included in laravel 5 by default. You shall install it by
composer require "illuminate/html":"5.0.*"
and updating in the app.php.
I have written a blog which gives a breif about this installation.
In Routes web.php
Your code is
Route::get('/admin',array('uses'=>'student#admin'));
which is wrong.
Actually submitting data in POST method its array of data so you need to route through post instead of get.
so correct code is
Route::post('/admin',array('uses'=>'student#admin'));
Follow this tutorial form Laracast it might helpful,
https://laracasts.com/series/laravel-from-scratch-2017/episodes/16
In routes.php, replace Route::get by Route::post.
You have no post route for your form data posting , use route match function for both http verb (get & post). use this
Route::match(['get', 'post'], '/admin', 'student#admin');
Also you need to change your admin method,
public function admin(Request $request){
if($request->isMethod('get')){
return \View::make('student.admin');
} else {
// your validation logic
}
}

Laravel : BadMethodCallException Method [store] does not exist

I just downloaded and started a new project with the latest Laravel 4.2. When trying to submit a form I get the following error : BadMethodCallException Method [store] does not exist
Here are my files : controller - admin/AdminController
<?php
namespace admin;
use Illuminate\Support\Facades\View;
use App\Services\Validators\ArticleValidator;
use Input, Notification, Redirect, Sentry, Str;
class AdminController extends \BaseController {
public function index() {
if (Input::has('Login')) {
$rules = array(
'email' => 'required',
'password' => 'required|min:3',
'email' => 'required|email|unique:users'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::to('admin\AdminController')->withErrors($validator);
} else {
// redirect
Session::flash('message', 'Successfully created user!');
return Redirect::to('admin\AdminController');
}
}
$data['title'] = ADMIN;
return View::make('admin.index', $data);
}
}
View page - admin/index.blade.php
<div class="container">
{{ Form::open(array('url' => ADMIN,'id' => 'login')) }}
<div id="icdev-login-wrap">
<div class="raw align-center logoadmin">{{ HTML::image('images/logo.png') }}</div>
<div id="icdev-login">
<h3>Welcome, Please Login</h3>
<div class="mar2_bttm input-group-lg"><input type="text" class="form-control loginput" placeholder="Email" name="email"></div>
<div class="mar2_bttm input-group-lg"><input type="password" class="form-control loginput" placeholder="Password" name="password"></div>
<div ><input type="submit" class="btn btn-default btn-lg btn-block cus-log-in" value="Login" /></div>
<div class="row align-center forgotfix">
<input type="hidden" name="Login" value="1">
</div>
</div>
<div>
</div>
</div>
{{ Form::close() }}
</div>
The error message tells you what the problem is: the method called store() doesn’t exist. Add it to your controller:
<?php
namespace admin;
use Illuminate\Support\Facades\View;
use App\Services\Validators\ArticleValidator;
use Input, Notification, Redirect, Sentry, Str;
class AdminController extends \BaseController {
public function index()
{
// leave code as is
}
public function store()
{
// this is your NEW store method
// put logic here to save the record to the database
}
}
A couple of points:
Use camel-casing for name spaces (i.e. namespace admin should be namespace Admin)
Read the Laravel documentation on resource controllers: http://laravel.com/docs/controllers#resource-controllers
You can also automatically generate resource controllers with an Artisan command. Run $ php artisan make:controller ItemController, replacing ItemController with the name of the controller, i.e. ArticleController or UserController.

Categories