Form request validation not firing - php

On successful form submission, I should be seeing 'in here' within the browser but I get re-directed to a page not found view:
Sorry, the page you are looking for could not be found.
My form control:
{!! Form::open(['url' => 'prize_draw_store', 'class' => 'wffm-form-module text-left']) !!}
{!! Form::label('fullname', 'Name:', array('class' => 'control-label')) !!}
{!! Form::text('fullname', null, array('required', 'class'=>'form-control text-box single-line', 'placeholder'=>'')) !!}
{!! Form::label('email_address', 'Email Address:', array('class' => 'control-label')) !!}
{!! Form::text('email_address', null, array('required', 'class'=>'form-control text-box single-line', 'placeholder'=>'')) !!}
<div class="form-submit-border">
{{ Form::submit('Submit', array('class' => 'btn btn-default')) }}
</div>
{!! Form::close() !!}
My routes are well-defined:
/routes/web.php
use App\Http\Controllers\PrizeDrawController;
use App\Http\Requests\PrizeDrawFormRequest;
Route::get('/',
['as' => 'prize_draw', 'uses' => 'PrizeDrawController#create']);
Route::post('/',
['as' => 'prize_draw_store', 'uses' => 'PrizeDrawController#store']);
PrizeDrawController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\PrizeDrawFormRequest;
class PrizeDrawController extends Controller
{
public function create() {
return view('front.pages.home');
}
public function store(PrizeDrawFormRequest $request) {
var_dump('in here');
return redirect('thankyou')->with('status', 'Form submitted!');
}
}
PrizeDrawFormRequest.php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class PrizeDrawFormRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'email_address' => 'required|email',
'fullname' => 'required'
];
}
}
I'm expecting the code to flow like thus:
1. http://aacomp.local/prize_draw_store -- POST form submission
2. App\Http\Controllers\PrizeDrawController#store
3. PrizeDrawController::store(PrizeDrawFormRequest $request)
4. Redirect a user to thankyou page
We don't seem to reach step 3 though? I must be missing something basic -
are you able to help?
Thanks in advance.

The problem lies with:
Route::post('/',
['as' => 'prize_draw_store', 'uses' => 'PrizeDrawController#store']);
Changing this route to
Route::post('/foobar',
['as' => 'prize_draw_store', 'uses' => 'PrizeDrawController#store']);
and updating the form action:
{!! Form::open(['url' => 'foobar', ...
say - will get the desired results. Thanks to Darius. V for this.

Related

Having hard time understanding Forms from laravel collectives

I'm new to Laravel. I've created a form and trying to delete one of the post. I just want to know how opening a form from Laravel collectives works.
Currently, this is how my form looks like in show.blade.php
{!! Form::open(['action' => ['PostsController#destroy', $post->id], 'method' => 'POST', 'class' => 'pull-right']) !!}
{{ Form::hidden('_method', 'DELETE') }}
{{ Form::submit('Delete', ['class' => 'btn btn-danger']) }}
{!! Form::close() !!}
The above form gives me the error Action PostsController#destroy not defined.
But when I add 'url' => 'posts/' in Form i.e.
{!! Form::open(['url' => 'posts/', 'action' => ['PostsController#destroy', $post->id], 'method' => 'POST', 'class' => 'pull-right']) !!}
The above error disappears.
Below is the destroy() function in PostsController
class PostsController extends Controller
{
public function destroy($id)
{
$post = Post::find($id);
$post->delete();
return redirect('/posts')->with('success','Post removed');
}
}
web.php
// --- Posts Routing
Route::get('/posts', [App\Http\Controllers\PostsController::class, 'index'])->name('posts');
MY QUESTIONS
I'm redirecting in the destroy() function in PostsController, why url is necessary with action in Form from Laravel
collectives to avoid the above error?
When to use 'url' and when to use action?
I've laravel 4.2.10.

Laravel Action App\Http\Controllers\Admin\ConcursoController#store not defined

I want to save some form data, and I'm Getting error.
The Error
Action App\Http\Controllers\Admin\ConcursoController#store not defined. (0)
My Form
{!! Form::open(['action'=>'Admin\ConcursoController#store', 'method' => 'POST']) !!}
<div class="form-group">
{{Form::label('company','Entidade')}}
{{Form::text('company','',['class' => 'form-control', 'placeholder' => 'Nome da entidade aquĆ­..'])}}
</div>
{{Form::submit('submeter', ['class'=>'btn btn-primary'])}}
{!! Form::close() !!}
My Route
$this->group(['middleware' => ['auth:admin'], 'namespace' => 'Admin', 'prefix' => 'admin'], function(){
$this->get('/', 'AdminController#index')->name('admin.home');
$this->resource('concursos', 'ConcursoController');
});
Controller index Method
public function index()
{
$concursos = Concurso::all();
$title = 'Concursos';
return view('admin.concursos.index',compact('title'))->with('concursos',$concursos);
}
Controller Create method
public function create()
{
return view('admin.concursos.create');
}
Controller Store Method
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'body' => 'required',
]);
//Criar concurso
$concurso = new Concurso;
$concurso->title = $request->input('title');
$concurso->body = $request->input('body');
$concurso->save();
return redirect('/admin/concursos')->with('Success', 'Concurso Adicionado');
}
Laravel version 5.7.14
Probably check this file: App\Http\Controllers\Admin\ConcursoController and see if you have a function/method called 'store'. The error is pretty straightforward that script can't find that function.
replace your form
['route' => ['concursos.store']
like
{!! Form::model($transactions, ['route' => ['transaction.store'], 'method' => 'POST','class'=>"form-horizontal"]) !!}

Laravel Contact form on Single Page website

I'm trying to get the correct routes for the Laravel Contact form on a single page website but I'm not sure how to apply the routes so far, since i've done it with sites that aren't single page (parallax - like website/ scrollable).
These are the routes I am creating, so everything stays on the homepage(no redirects at all because it's a one page scrollable website)
Route::get('/', 'ContactUsController#create')->name('contact.create');
Route::post('/', 'ContactUsController#store')->name('contact.store');
My Controller looks like this: Please note that the create controller returns the view to my index which of course is routed like so ('/'),
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Mail\ContactEmail;
class ContactUsController extends Controller
{
public function create()
{
return view('index');
}
public function store(Request $request)
{
$contact = [];
$contact['name'] = $request -> get('name');
$contact['phone'] = $request -> get('phone');
$contact['email'] = $request -> get('email');
$contact['subject'] = $request -> get('subject');
$contact['message'] = $request -> get('message');
//send mail logic here
Mail::to(config('mail.support.address'))->send(new ContactEmail($contact));
flash('Your Message has been sent!) -> success();
return redirect()-> route('/');
}
}
And below is my Contact Form:
{!! Form::open(['route' => 'contact.store', 'class' => 'text-light '])!!}
{!! Form::label('name', 'Your Name', ['class' => 'text-light'])!!}
{!! Form::text('name', null, ['class' => 'form-control text-light'])!!}
{!! Form::label('phone', 'tel', ['class' => 'text-light'])!!}
{!! Form::text('phone', null, ['class' => 'form-control text-light'])!!}
{!! Form::label('email', 'Email', ['class' => 'text-light'])!!}
{!! Form::text('email', null, ['class' => 'form-control text-light'])!!}
{!! Form::label('subject', 'Subject', ['class' => 'text-light'])!!}
{!! Form::text('subject', null, ['class' => 'form-control text-light'])!!}
{!! Form::label('message', 'Your Message Here..', ['class' => 'text-light'] )!!}
{!! Form::textarea('message', null, ['class' => 'form-control text-light'])!!}
{!! Form::submit('Submit', ['class' => 'btn btn-info']) !!}
{!! Form::close() !!}
#if($errors -> any())
<div class="alert alert-danger">
<ul>
#foreach($errors -> all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
#include('flash::message')
Solved problem with solution :
//routes for contact form
Route::get('/contacts', 'ContactUsController#create')->name('contact.create');
Route::post('/contacts', 'ContactUsController#store')->name('contact.store');
Everything else same.
Thanks guys for all support.
//Create the routes in web.php
Route::get('contact',['as' => 'contact, 'uses' => 'ContactController#create']);
Route::post('contact',['as' => 'contact', 'uses' => 'ContactController#store']);
// Create the ContactController using the command
php artisan make:controller ContactController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\ContactRequest;
use Mail;
class ContactController extends Controller
{
public function create()
{
return view('front.contact.index');
}
public function store(ContactRequest $request)
{
\Mail::send('emails.contact',
array(
'name' => $request->get('name'),
'email' => $request->get('email'),
'user_message' => $request->get('message')
), function($message)
{
$message->from('hello#outlined.co');
$message->to('poornima#outlined.co', 'outlined')->subject('New Contact
Request from outlined');
});
return back()->with('status', 'your message has been received');
}}

Laravel PHP, trying to redirect to an id

I am a beginner with php and laravel. I have recently created a form at the bottom of my website, that when you click the submit button it sends the data to an email address.
However if you don't fill it in correctly and click submit it refreshes the page and returns to the top of the page. If you submit the form correctly it goes to /contact.
What I am desperate to do is keep the page at the same point on the website (with the form on the screen) instead of refreshing the screen. Same if the form is submitted correctly I would like the screen to stay still.
Routes/web.php:
Route::get('/', 'PagesController#home');
Route::get('about', 'PagesController#about');
Route::get('contact',
['as' => 'contact', 'uses' => 'AboutController#create']);
Route::post('contact',
['as' => 'contact_store', 'uses' => 'AboutController#store']);
ContactFormRequest.php:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class ContactFormRequest 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 [
'name' => 'required',
'email' => 'required|email',
'number' => 'required',
'message' => 'required',
];
}
}
AboutController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\ContactFormRequest;
class AboutController extends Controller
{
public function create()
{
return view('pages.contact');
}
public function store(ContactFormRequest $request)
{
\Mail::send('emails.contact',
array(
'name' => $request->get('name'),
'email' => $request->get('email'),
'number' => $request->get('number'),
'user_message' => $request->get('message')
), function($message)
{
$message->from('sketchsitestest#gmail.com');
$message->to('sketchsitestest#gmail.com', 'Admin')->subject('Testing');
});
return \Redirect::route('contact')->with('message', 'Thanks for contacting us!');
}
}
contact.blade.php
<div class="one-half">
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
{!! Form::open(array('route' => 'contact_store', 'class' => 'form')) !!}
<div class="form-group">
{!! Form::text('name', null,
array('required',
'class'=>'form-control',
'placeholder'=>'Your name')) !!}
</div>
<div class="form-group">
{!! Form::text('email', null,
array('required',
'class'=>'form-control',
'placeholder'=>'Your e-mail address')) !!}
</div>
<div class="form-group">
{!! Form::number('number', null,
array('required',
'class'=>'form-control',
'placeholder'=>'Your Number')) !!}
</div>
<div class="form-group">
{!! Form::text('message', null,
array('required',
'class'=>'form-control',
'placeholder'=>'Your message')) !!}
</div>
<div class="form-group">
{!! Form::submit('Contact Us!',
array('class'=>'form-button btn btn-primary')) !!}
</div>
{!! Form::close() !!}
</div>
I am also new to using stackoverflow, so I apologise ahead if I have missed anything or done anything incorrectly. Please let me know if you need anything more.
Can't really understand your problem, but try to add this in your store() method:
return \Redirect::back()->with('message', 'Thanks for contacting us!');
But it looks like you want to submit/validate your forms without refreshing the page, so the only solution for this is to use AJAX requests.
May be this can be something. Using the validation helper.
function store(ContactFormRequest $request)
{
$this->validate($request, [
'name' => 'required',
'email' => 'required',
'number' => 'required',
'message' => 'required',
]);
// If !$this->validate Laravel will automatic return to the previous page (the form page)
// And you can grab the errors with $errors->first('name'), $errors->first('email') etc
\Mail::send('emails.contact',
array(
'name' => $request->get('name'),
'email' => $request->get('email'),
'number' => $request->get('number'),
'user_message' => $request->get('message')
), function($message)
{
$message->from('sketchsitestest#gmail.com');
$message->to('sketchsitestest#gmail.com', 'Admin')->subject('Testing');
});
return \Redirect::route('contact')->with('message', 'Thanks for contacting us!');
}
Grab the errors like this:
#if ($errors->has('message'))
<span class="help-block">
<strong>{{ $errors->first('message') }}</strong>
</span>
#endif
Here you the docs about the validation, if you are using Laravel 5.3

MethodNotAllowed when trying to do an update.

I'm using PHPstorm with Laravel 5.2.3, and currently trying to update a persons information by reusing a form from my create page. I've followed everything to the T from the laracast tutorials, but for some reason I'm getting the MethodNotAllowed error when I hit submit on an update.
Routes
Route::group(['middleware' => ['web']], function()
{
...
Route::get('create','ResourceController#create');
Route::post('create', 'ResourceController#store');
Route::resource('pages', 'ResourceController');
});
Controller
class ResourceController extends Controller
{
...
public function create()
{
return view('pages.create');
}
public function store(Requests\CreateNewContactRequest $request)
{
ContactPerson::create($request->all());
return redirect('resource');
}
public function edit($id)
{
$user = ContactPerson::findOrFail($id);
return view('pages.edit')->with(compact('user'));
}
public function update($id, Request $request)
{
$user = ContactPerson::findOrFail($id);
$user->update($request->all());
return redirect('pages.resource');
}
}
Edit View
#extends('app')
#section('content')
{!! Form::model($user, ['method' => 'PATCH', 'action' => ['ResourceController#update', $user->id]]) !!}
<div class="form">
{!! Form::label('first', 'First Name: ') !!}
{!! Form::text('First_Name', null, ['class' => 'form']) !!}
</div>
<div class="form">
{!! Form::label('last', 'Last Name: ') !!}
{!! Form::text('Last_Name', null, ['class' => 'form']) !!}
</div>
...
{!! Form::close() !!}
View Source
It shows that it's a POST method with laravel spoofing it as a PATCH.
Error
It generates wrong action in <form> tag. Try using:
'route' => ['pages.update', $user->id]
instead of:
'action' => ['ResourceController#update', $user->id]
Is it works?.

Categories