Laravel 4 form post issue - php

my problem is that when i submit my form it pass through the get method.
The workflow is form submit -> get -> post and it should be form submit -> post.
I need to have a condition in my get method to validate de array is not null
My code:
Routes
Route::get('/pre-register-birth',array('as'=>'pre-register-birth', 'uses'=>'UserController#preRegisterBirthData'));
Route::post('/pre-register-birth', 'UserController#preRegisterBirthDataPost');
View
{{ Form::open(array('method' => 'POST', 'action' => 'UserController#preRegisterBirthDataPost',
'class'=>'form-horizontal', 'id'=>'regist-form')) }}
Controller
public function preRegisterBirthData()
{
$user = Session::get('user');
if ($user)
return View::make('user/pre-register-birth')->with('tempUser', $user);
else
return Redirect::Route('pre-register-get');
}
public function preRegisterBirthDataPost()
{
$validator = Validator::make(Input::all(),
array(
'birthplace' => 'required|max:100',
'birthdate' => 'required|max:100|date|date_format:Y-m-d'
)
);
if ($validator->fails()) {
return Redirect::Route('pre-register-birth')
->withErrors($validator)
->withInput();
} else {
$user = array(
'email' => Input::get('email'),
'pass' => Input::get('pass'),
'name' => Input::get('name'),
'surname' => Input::get('surname'),
'birthplace' => Input::get('birthplace'),
'birthdate' => Input::get('birthdate'),
'hourKnow' => Input::get('hourKnow'),
'dataCorrect' => Input::get('dataCorrect'),
'news' => Input::get('news'),
);
return Redirect::Route('pre-register-terms')->with('user', $user);
}
}

I think I've seen this issue before. Laravel for whatever odd reason doesn't like the action => ... So, change your form declaration from a to b:
// A
{{ Form::open(array('method' => 'POST', 'action' => 'UserController#preRegisterBirthDataPost',
'class'=>'form-horizontal', 'id'=>'regist-form')) }}
// B
{{ Form::open(array('method' => 'POST', 'url' => 'pre-register-birth',
'class'=>'form-horizontal', 'id'=>'regist-form')) }}
Notice I changed action => ... to url => ... It's a small change, but it might solve it. However, you may need to add in:
Route::post('/pre-register-birth', array('as'=> 'pre-register-birth', 'uses' => 'UserController#preRegisterBirthDataPost'));
So it recognizes the named route.
Hope this helps!

Related

Laravel patch method not supported (even on update method)

HTML PATCHING FORM
{{ Form::model($model , [ 'route' => ['admin.friendship.update', $model->id] , 'class'=>'needs-validation ajax-form', 'method' => 'post' ]) }}
#method('PATCH')
{{ Form::select('user_id' , $users , null , ['id' => 'sender', 'class' => 'form-control']) }}
{{ Form::select('friend_id' , $users , null , ['id' => 'reciever', 'class' => 'form-control']) }}
{{ Form::select('status' , ['-2' => -2 , '-1' => -1 , '0' => 0 , '1' => 1] , null , ['id' => 'status', 'class' => 'form-control']) }}
{{ Form::close() }}
Update method:
public function update(FriendshipsAdminForm $request, Friendship $friendship)
{
$friendship->update($request->validated());
return redirect()->route('admin.friendship.index');
}
Request form
class FriendshipsAdminForm extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'user_id' => 'required',
'friend_id' => 'required',
'status' => 'required',
];
}
}
Route:
Route::resource('friendship', \App\Http\Controllers\Admin\FriendshipController::class);
This is my form and it's really strange. maybe I can not see a thing that is blocking me from doing this. there is not typo or syntax mistake. Is there anything about patch method I should consider even when trying update method?
UPDATE
ERROR MESSAGE , ERROR STATUS 405
UPDATE2
I'm using ajax for it.
I'm using ajax the way I use above with near 30 models and all are more complex than this one but all are updating and working nicely but this one is strange
Since you using ajax, #method is not working in that. You need to activate PATCH in your webserver, or change your ajax code to submit form in post and add _method in params.
For example in jQuery
$.ajax({
...
method: 'POST',
data:{
...
'_method': 'PATCH',
}
});

Trying to create two way login form in Laravel - 'Array to string conversion'

I have normal login form which work great. Now I'm trying to make second simple step when user enter his password and user name and if they are correct to redirect him to new page where he must enter pass phrase in order to continue.
I'm not sure if this is correct what I have make so far. This is my route:
Route::get ('/users/login', ['uses' => 'UsersController#login', 'before' => 'guest']);
Route::post('/users/login', ['uses' => 'UsersController#loginSubmit', 'before' => 'guest']);
// new
Route::get('/users/auth', ['uses' => 'UsersController#loginAuth', 'before' => 'guest']);
Route::post('/users/auth', ['uses' => 'UsersController#loginSubmitAuth', 'before' => 'auth|csrf']);
This is my auth.blade.php
{{ Form::open(['class' => 'form-horizontal']) }}
<div class="form-group"> {{ Form::textarea('key', ['class' => 'form-control', 'id' => 'key', 'autocomplete' => 'off']) }} </div><br/>
<hr />
<div class="row">
<button type="submit" class="btn btn-primary col-xs-4 col-xs-offset-4">Login</button>
</div>
<hr />
{{ Form::close() }}
This is my controller
public function login() {
return View::make('site.users.login');
}
public function loginSubmit() {
$validatorRules = array(
'captcha' => 'required|captcha',
'username' => 'required|alpha_dash',
'password' => 'required|min:6'
);
Input::merge(array_map('trim', Input::all()));
$validator = Validator::make(Input::all(), $validatorRules);
if ($validator->fails()) {
return Redirect::to('/users/login')->withErrors($validator->errors())->withInput(Input::except(['captcha']));
}
$user = User::where('username', Input::get('username'))->first();
if (!$user) {
$validator->messages()->add('username', 'Invalid login or password.');
return Redirect::to('/users/login')->withErrors($validator->errors())->withInput(Input::except(['captcha']));
}
if (!Hash::check(Input::get('password'), $user->password)) {
$validator->messages()->add('username', 'Invalid login or password.');
return Redirect::to('/users/login')->withErrors($validator->errors())->withInput(Input::except(['captcha']));
}
//$user->last_login = \Carbon\Carbon::now();
//$user->save();
//Session::put('user', ['user_id' => $user->user_id]);
return Redirect::to('/users/auth');
}
public function loginAuth() {
return View::make('site.users.auth');
}
public function loginSubmitAuth() {
$validatorRules = array(
'key' => 'required',
'captcha' => 'required|captcha'
);
Input::merge(array_map('trim', Input::all()));
$validator = Validator::make(Input::all(), $validatorRules);
if ($validator->fails()) {
return Redirect::to('/users/auth')->withErrors($validator->errors())->withInput(Input::except(['captcha']));
}
$user = User::where('key', Input::get('key'))->first();
if (!$user) {
$validator->messages()->add('key', 'Invalid Key.');
return Redirect::to('/users/auth')->withErrors($validator->errors())->withInput(Input::except(['captcha']));
}
$user->last_login = \Carbon\Carbon::now();
$user->save();
Session::put('user', ['user_id' => $user->user_id]);
return Redirect::to('/');
}
Current error is 'Array to string conversion'
Any help is appreciated. Thank's
The issue of second parameter in Form::textarea you need to pass null to make it empty in second parameter and array in third parameter like,
<div class="form-group">
{{ Form::textarea('key',null, ['class' => 'form-control', 'id' => 'key', 'autocomplete' => 'off']) }}
</div><br/>
You can refer to Creating a Textarea Input Field
Can you try if this fixes your problem:
{{ Form::textarea('key', null, ['class' => 'form-control', 'id' => 'key', 'autocomplete' => 'off']) }}
It's this way because the value argument comes second and it is mandatory in difference with the third one which is optional

"MethodNotAllowedHttpException" retuned from Laravel 5.2 update

I am getting MethodNotAllowedHttpException retuned when updating a form in Laravel 5.2. I understand there could be an issue with the put method.
The form sending from the index:
{!! Form::model('Customers', ['route'=>['products.update', Auth::user()->id]]) !!}
{{ Form::hidden('business', Auth::user()->name, array('class' => 'form-control', 'required' => '','maxlength'=>'255'))}}
{{ Form::label('post', 'Mailbox')}}
{{ Form::checkbox('post',1, null, array('class' => 'form-control'))}}
The Controller is:
public function update(Request $request, $id)
{
$this->validate($request, array (
'post' => '',
'mailbox' => '',
'conum' => '',
'prefix' => '',
'telans' => '',
'TC' => 'required',
));
//store
$post = Customers::find($id);
$post->post = $request->input('post');
$post->postpro = $request->input('mailbox');
$post->telans = $request->input('telans');
$post->conum = $request->input('conum');
$post->prefix = $request->inut('prefix');
$post->tc = $request->input('TC');
//save
$post->save();
//session flash message
//Session::flash('success','This customer has now been added');
//redirect
return redirect('/home');}
And the route is as follows:
Route::resource('products', 'ProductsController');
Thank you
Your forgot the quotes, replace this :
PHP
$post->post = $request->input(post);
with this :
$post->post = $request->input('post');
do not forget to set the _method as put.

Laravel MethodNotAllowedHttpException, but my methods match

I have a registration form that I'm creating using the blade templating like so:
{{ Form::open(array('url'=>'registerUser', 'method'=>'POST', 'class'=>'loginForm SignUp')) }}
In routes.php, I have the following route:
Route::post('registerUser', 'UsersController#doRegister');
But when I submit the form, I get a MethodNotAllowedHttpException. Pretty much every other question I've found online about this was a case of the form having the GET method while the route had POST, but mine match, so I'm pretty confused.
Edit: Here's my full routes file:
Route::get('', 'UsersController#showLogin');
Route::get('login', 'UsersController#showLogin');
Route::post('doLogin', 'UsersController#doLogin');
Route::get('signUp', 'UsersController#showSignUp');
Route::post('authenticateCode', 'UsersController#authenticateCode');
Route::post('requestCode', 'UsersController#requestCode');
Route::get('showRegister', 'UsersController#showRegister');
Route::post('registerUser', 'UsersController#doRegister');
Route::get('dashboard', 'DashboardController#showDashboard');
Here's the controller function:
public function doRegister() {
$rules = array(
'fname' => 'required|alpha|min:2',
'lname' => 'required|alpha|min:2',
'email' => 'required|email|unique:users',
'phone' => 'required|alpha_num|min:7',
'company' => 'required|alpha_spaces|min:2',
'password' => 'required|alpha_num|between:6,12|confirmed', // these password rules need to be improved
'password_confirmation' => 'required|alpha_num|between:6,12'
);
$validator = Validator::make(Input::all(), $rules);
if($validator->passes()) {
$user = User::create(array(
'fname' => Input::get('fname'),
'lname' => Input::get('lname'),
'email' => Input::get('email'),
'password' => Hash::make(Input::get('password')),
'phone' => Input::get('phone'),
'company' => Input::get('company')
));
// Update the invite key that was passed to the register page
// with this new user's ID.
$key = InviteKey::find(Input::get('key'));
$key->user_id = $user->id;
$key->save();
return Redirect::to('login')->with('message', 'Thank you for registering!');
} else {
return Redirect::to('register')
->with('message', 'The following errors occurred')
->withErrors($validator)
->withInput(Input::except('password'));
}
}

Laravel 4 Form passing 2 parameters

Currently i'm working on a project to manage my cost on fuel.
Now i try to pass 2 parameters in a Form::open() which sadly doesn't work.
The reason why i think i need to pass 2 parameters at once is because my url is Sitename/car/{id}/tank/{id}
What am i doing wrong?
edit.blade.php
Form::open(array('class' => 'form-horizontal', 'method' => 'put', 'action' => array('TankController#update', array($aid, $id))))
Problem Code
'action' => array('TankController#update', array($aid, $id)
-Results in the following error:
Parameter "tank" for route "car.{id}.tank.update" must match "[^/]++" ("" given) to generate a corresponding URL.
TankController.php
public function edit($id, $tid)
{
$tank = Tank::find($tid);
if(!$tank) return Redirect::action('TankController#index');
return View::make('Tank.edit', $tank)->with('aid', $id);
}
public function update($id, $tid)
{
$validation = Validator::make(Input::all(), Tank::$rules);
if($validation->passes()){
$tank = Tank::find($tid);
$tank->kmstand = Input::get('kmstand');
$tank->volume = Input::get('volume');
$tank->prijstankbeurt = Input::get('prijstankbeurt');
$tank->datumtank = Input::get('datumtank');
$tank->save();
return Redirect::action('TankController#index', $id)->with('success', 'Tankbeurt succesvol aangepast');
} else return Redirect::action('TankController#edit', $id)->withErrors($validation);
}
Route.php
Route::resource('car', 'CarController');
Route::resource('car/{id}/tank', 'TankController');
Route::controller('/', 'UserController');
-Url Structure
SITENAME/car/2/tank/2/edit
I've also looked into the api documents but found nothing.
http://laravel.com/api/source-class-Illuminate.Html.FormBuilder.html
Thanks in advance
Try this:
Form::open(array('class' => 'form-horizontal', 'method' => 'put', 'action' => array('TankController#update', $aid, $id)))
This is the best solution
Form::open (array ('method'=>'put', 'route'=>array($routeName [,params...]))
An example:
{{ Form::open(array('route' => array('admin.myroute', $object->id))) }}
https://github.com/laravel/framework/issues/491
Try to use in your blade:
For Laravel 5.1
{!! Form::open([
'route' => ['car.tank.update', $car->id, $tank->id],
'method' => 'put',
'class' => 'form-horizontal'
])
!!}

Categories