Laravel 5 Add errors in an array using After Validation Hook - php

I'm trying to add a function in my app where users are allowed to change their account password. I have three fields and my view looks like this:
<form class="form" role="form" action="{{ url('users/updatePassword') }}" method="post">
{{ csrf_field() }}
<div class="form-group label-floating {{ $errors->has('oldpassword') ? 'has-error' : '' }}">
<label class="control-label" for="oldpassword">Old Password</label>
<input type="password" name="oldpassword" class="form-control">
#if ($errors->has('oldpassword'))
<span class="help-block">
<strong>{{ $errors->first('oldpassword') }}</strong>
</span>
#endif
</div>
<div class="form-group label-floating {{ $errors->has('newpassword') ? 'has-error' : '' }}">
<label class="control-label" for="newpassword">New Password</label>
<input type="password" name="newpassword" class="form-control">
#if ($errors->has('newpassword'))
<span class="help-block">
<strong>{{ $errors->first('newpassword') }}</strong>
</span>
#endif
</div>
<div class="form-group label-floating">
<label class="control-label" for="newpassword_confirmation">Confirm Password</label>
<input type="password" name="newpassword_confirmation" class="form-control">
</div>
<div class="form-group">
<button class="btn btn-raised btn-primary">Change</button>
</div>
</form>
Firstly, I want to check if all fields are completely filled up and for that I used Validator. And then check if the oldpassword is match from the database so I use if (Auth::attempt(array('password' => $request->oldpassword))) condition. I also found in the laravel 5.2 documentation the After Validation hook. I don't know what is wrong but it seems it don't validates the oldpassword field when I typed a wrong password.
My controller:
$validator = Validator::make($request->all(), [
'oldpassword' => 'required|max:255',
'newpassword' => 'required|min:6|max:255|confirmed',
]);
$validator->after(function($validator) use($request) {
if (Auth::attempt(array('password' => $request->oldpassword))) {
$validator->errors()->add('oldpassword', 'Old password dont match in our database.');
}
});
if ($validator->fails()) {
// Toastr
$title = "Oops!";
$message = "Please make sure to fill all required fields.";
$options = [
'progressBar' => false,
'positionClass' => 'toast-top-right',
'timeOut' => 6000,
];
Toastr::error($message, $title, $options);
return redirect()->back()
->withErrors($validator);
} else {
return 'success'; // for testing only
}
Any idea regarding this?

According to your code when you enter correct oldpassword you get the error. So change if(Auth::attempt..... to if(!Auth:attempt.... And also if you use Auth:attempt you have to logout user again(this method also requires unique field like username or email to identify the user). so it's better if you use following method
if (!\Hash::check($request->get('oldpassword'), \Auth::user()->password)) {
$validator->errors()->add('oldpassword', 'Old password dont match in our database.');
}

Related

Display error for specific query when attempting to login user laravel

I want to be able to display error message if a certain query returns false when attempting to log in user.
for example, as in my case i want to show a message to a user that his account has been deactivated if the deactivated column is set to 'yes'
Here is my code login method, i tried adding a second error, but both error returns if login fails, regardless of the failed query
public function login(Request $request)
{
$message = array(
'required.email' => 'Email is required',
'required.password' => 'Password is required',
);
$this->validate($request, [
'email' => 'required|email',
'password' => 'required|min:8',
]);
$email = $request->email;
$pass = $request->password;
if (Auth::attempt([
'email' => $email,
'password' => $pass,
'verified' => 'yes'
], $request->get('remember'))) {
return redirect()->intended(route('user.account.index'));
} else {
return redirect()->back()->withErrors(
[
'email' => 'These credentials do not match our records or this user has been deactivated!.',
'verified' => 'This user account has been deactivated! Contact support!'
]
);
}
}
And here is my login view
<form action="{{ route('login') }}" method="post">
#csrf
<span>Login Your Account</span>
<br><br>
#if (Session::has('message'))
<small class="alert alert-success">{{ Session::get('message') }}</small>
<br><br>
#endif
#if (Session::has('status'))
<small class="alert alert-info" role="alert">
{{ Session::get('status') }}
</small>
<br><br>
#endif
#error('verified')
<small class="alert alert-danger" role="alert">
{{ $message }}
</small>
<br><br>
#enderror
<div class="form-group">
<input type="email" class="form-control" placeholder="Email Address" name="email">
#error('email')
<br>
<small class="alert alert-danger" role="alert">
{{ $message }}
</small>
#enderror
</div>
<div class="form-group">
<input type="password" class="form-control" placeholder="Password" name="password">
#error('password')
<br>
<small class="alert alert-danger" role="alert">
{{ $message }}
</small>
#enderror
</div>
<div class="text-right">
Forgot Password?
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="form-checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}>
<label class="custom-control-label" for="form-checkbox">{{ __('Remember Me') }}</label>
</div>
<button type="submit" class="btn btn-danger">Login to account</button>
</form>
I will suggest using session flash messages instead of redirect witherror method. It has the advantage of being able to run custom messages in multiple situations. You can implement it like this: https://www.itsolutionstuff.com/post/laravel-5-implement-flash-messages-with-exampleexample.html
I also recommend checking this out to figure out how to customize your messages based on conditions.
In Laravel, the best way to pass different types of flash messages in the session

How to validate bcrypt password in Laravel?

I have a change password form with fields for old and new password
The old password field is named old_password.
In my controller i am validating the passwords like this
$this->validate($request, [
'old_password' => 'required|min:6|exists:users,password',
'password' => 'required|min:6:max:30',
]);
But its not working as im comparing the old_password directly with password without function bcrypt().
So how can i compare the old_password field with the already stored bcrypted password in user field.
Note: i want to give the validation error back that password does not
match if it is different
This is how I would make such a function
The HTML form
<form action="{{ url('admin/admin-admin-password-update/'.$admin->id) }}" method="post">
{{ csrf_field() }}
<div class="form-group {{ $errors->has('new_password') ? 'has-error' : '' }}">
<label for="new_password" class="form-label">New Password (Minimum of 6 characters. No spaces.) <span class="required-alert">*</span></label>
<input type="password" class="form-control" name="new_password" id="new_password" />
#if ($errors->has('new_password'))
<div class="help-block">
{{ $errors->first('new_password') }}
</div>
#endif
</div>
<div class="form-group {{ $errors->has('confirm_password') ? 'has-error' : '' }}">
<label for="confirm_password" class="form-label">Confirm New Password <span class="required-alert">*</span></label>
<input type="password" class="form-control" name="confirm_password" id="confirm_password" />
#if ($errors->has('confirm_password'))
<div class="help-block">
{{ $errors->first('confirm_password') }}
</div>
#endif
</div>
<div class="form-group clearfix">
<button type="submit" class="btn btn-primary">Save New Password</button>
</div>
</form>
controller code
public function updatePassword(Request $request)
{
$this->admin = Auth::user();
$this->id = $this->admin->id;
$this->validate($request, [
'current_password' => 'required',
'password' => 'required|min:6',
'confirm_password' => 'required|same:password',
]);
if (Hash::check($request->input('current_password'), $this->admin->password)) {
$this->admin->password = Hash::make($request->input('password'));
$this->admin->save();
return redirect()->back()->with('success', 'Your password has been updated.');
} else {
return redirect()->back()->with('warning', 'The current password you provided could not be verified');
}
}
You need to compare the hash results of both old_password and the other password you wish to compare.
// when old_password is not encrypted but other_password is:
bcrypt('old_password') === hash_of_other_password
One way of doing so with $this->validate and not writing custom rules would be to use $request->merge(). That way you can use the confirmed rule or even exists if you have the hashed value stored in your database.

Update the password value only when i change the password or else it retain the same value

I am trying to change the password value by getting old password it working fine but what my problem is when i try to update my field it save the new password value as empty(because i leave this field empty) i want to update the new password only when i change the new password value or else it maintain the current password in the new password field
here is inside my controller
public function profileupdate(Request $request,$id)
{
if(Auth::Check())
{
$request_data = $request->All();
$validator = $this->validator($request_data);
if($validator->fails())
{
$this->throwValidationException($request, $validator);
}
else
{
$current_password = Auth::User()->password;
if(Hash::check($request_data['current-password'], $current_password))
{
$user_id = Auth::User()->id;
$admin = Admin::find($user_id);
$admin->update([
'name'=>$request['name'],
'job_title'=> $request['job_title'],
'email'=>$request['email'],
'phone_number'=>$request['phone_number'],
'password'=> Hash::make($request['password']),
]);
return redirect('/admin/profile')
->with('message', 'Admin Profile updated successfuly!');
}
else
{
return redirect('/admin/profile')
->with('password', 'Please enter correct password!');
}
}
}
else
{
return redirect()->to('/');
}
}
Inside my view
<div class="control-group{{ $errors->has('current-password') ? ' has-error' : '' }}">
<label class="control-label"> Current Password:</label>
<div class="controls">
<input type="password" class="form-control" id="current-password" name="current-password" placeholder="Password">
#if(Session::has('password')) <span class="help-inline"> <strong>{{Session::get('password')}} </strong></span> #endif
#if ($errors->has('current-password'))
<span class="help-inline">
<strong>{{ $errors->first('current-password') }}</strong>
</span>
#endif
</div>
</div>
<div class="control-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label class="control-label"> New Password:</label>
<div class="controls">
<input type="password" class="form-control" id="password" name="password" placeholder="Password">
#if ($errors->has('password'))
<span class="help-inline">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
</div>
<div class="control-group{{ $errors->has('password_confirmation') ? ' has-error' : '' }}">
<label class="control-label">Confirm Password:</label>
<div class="controls">
<input type="password" class="form-control" id="password_confirmation" name="password_confirmation" placeholder="Re-enter Password">
#if ($errors->has('password_confirmation'))
<span class="help-inline">
<strong>{{ $errors->first('password_confirmation') }}</strong>
</span>
#endif
</div>
</div>
Simply build the array which updates the values dynamically instead of statically. The majority of the array is still static, but the password is now dynamically added, depending of there was a value passed to the password field in your form.
// Values which are always updated
$update_values = [
'name'=>$request['name'],
'job_title'=> $request['job_title'],
'email'=>$request['email'],
'phone_number'=>$request['phone_number']
];
// If the password-field isn't empty, we add it to the values that are updated
if (!empty($request['password']))
$update_values['password'] = Hash::make($request['password']);
// Execute the update
$admin->update($update_values);

Laravel Auth modify for two kinds of users

I'm currently trying to modify the laravel Auth two be able to register two different kinds of users, a seller and a buyer. Both have the same form, except one field, that only the seller has, called companyName.
So what I did is putting a dropdown for registration instead of the normal register button, this is what I got there:
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
Registrieren
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
<li>
Als Käufer
Als Händler
</li>
</ul>
</div>
Then I made a route for this two kinds of registrations, like this:
Route::get('/register/{userType}', 'Auth\RegisterController#showRegistrationForm');
In the controller then I simply overwrote this showRegistrationForm function to pass the userType into my view, just like that:
public function showRegistrationForm($userType)
{
return view('auth.register', ['userType'=> $userType]);
}
And in my view, I got this:
#extends('master')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Registrieren
als <?php if ($userType == 'customer') echo "Käufer";if ($userType == 'seller') echo "Verkäufer";?></div>
<div class="panel-body">
<form class="form-horizontal" role="form" method="POST" action="{{ url('/register') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('sex') ? ' has-error' : '' }}">
<label for="sex" class="col-md-4 control-label">Anrede</label>
<div class="col-md-6">
<select class="form-control" id="sex">
<option value="male">Herr</option>
<option value="female">Frau</option>
</select>
</div>
#if ($errors->has('sex'))
<span class="help-block">
<strong>{{ $errors->first('sex') }}</strong>
</span>
#endif
</div>
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label for="firstName" class="col-md-4 control-label">Vorname</label>
<div class="col-md-6">
<input id="firstName" type="text" class="form-control" name="firstName"
value="{{ old('firstName') }}" required autofocus>
#if ($errors->has('firstName'))
<span class="help-block">
<strong>{{ $errors->first('firstName') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label for="name" class="col-md-4 control-label">Nachname</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control" name="name"
value="{{ old('name') }}" required autofocus>
#if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
</div>
</div>
<?php if ($userType == 'seller'){ ?>
<div class="form-group{{ $errors->has('companyName') ? ' has-error' : '' }}">
<label for="companyName" class="col-md-4 control-label">Firmenname</label>
<div class="col-md-6">
<input id="companyName" type="text" class="form-control" name="companyName"
value="{{ old('companyName') }}" required autofocus>
#if ($errors->has('companyName'))
<span class="help-block">
<strong>{{ $errors->first('companyName') }}</strong>
</span>
#endif
</div>
</div>
<?php } ?>
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Addresse</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email"
value="{{ old('email') }}" required>
#if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Passwort</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>
#if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password_confirmation') ? ' has-error' : '' }}">
<label for="password-confirm" class="col-md-4 control-label">Passwort
wiederholen</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control"
name="password_confirmation" required>
#if ($errors->has('password_confirmation'))
<span class="help-block">
<strong>{{ $errors->first('password_confirmation') }}</strong>
</span>
#endif
</div>
</div>
<div style="display:none;" class="form-group{{ $errors->has('role') ? ' has-error' : '' }}">
<label for="role" class="col-md-4 control-label">Deine Rolle:</label>
<div class="col-md-6">
<input name="role" type="radio"
<?php if ($userType == 'customer') echo "checked";?> value="Käufer"> Käufer<br/>
<input name="role" type="radio"
<?php if ($userType == 'seller') echo "checked";?> value="Verkäufer"> Verkäufer
#if ($errors->has('role'))
<span class="help-block">
<strong>{{ $errors->first('role') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Registrieren
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
So mostly basic, just few more fields then with the auth without modification and the companyName only showing up when accessed over the route /register/seller.
My RegisterController is of course also a bit modified, or especially the create function, it looks like this now:
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'firstName' => $data['firstName'],
'sex' => $data['sex'],
'email' => $data['email'],
'username' => $data['username'],
'password' => bcrypt($data['password']),
'role' => $data['role'],
'templateURL' => ""
]);
if($data['role'] == 'Verkäufer'){
Complaint::create([
'user_id' => $user->id,
'complaintCount' => 0
]);
}
switch($data['role']){
case 'Käufer':
$user->attachRole(2);
break;
case 'Verkäufer':
$user->attachRole(3);
$user->companyName = $data['companyName'];
$user->save();
break;
default:
$user->attachRole(2);
break;
}
return $user;
}
And now comes the problem: As you can see, in my "invidual" views, which is basically just one anyway, I still post to the url /register, which I thought should work, but it doesn't.... Any ideas why this is not working? I also tried to add individual routes, so something like that:
Route::post('/register/seller', 'Auth\RegisterController#create');
Route::post('/register/buyer', 'Auth\RegisterController#create');
but thats not working as well. In both cases, I just get the same window back, as if there was an error (so my data still entered (expect the password), but nothing is registered or entered in the db, but as well there are no errors showing up, neither in my view, nor in the console.
What's interesting as well is the network tab, it seems like it definetely sends the request to /register as it shows up there with status code 302, but as well there's the route /register/customer again, and I'm wondering why...What's also interesting is that I think that somehow it kinda works, as if I enter a password with less then 6 characters or 2 different passwords, I get an error, so somehow the form seems to be posted, but nothing is entered into the db....
Any ideas why this happens like this and whats the problem?
First of all, I'd like to suggest you a Polymorphic approach to saving the users. Right now, you have only 2 user types, what if you get a third user type (say retailer or wholesaler or blah blah)... and for each of them, you will require different fields which may or may not fit in for all user types...
So, go with this
class User
{
public function profile()
{
return $this->morphTo();
}
}
class Seller
{
public function user()
{
return $this->morphOne('App\User', 'profile');
}
}
class Buyer
{
public function user()
{
return $this->morphOne('App\User', 'profile');
}
}
Now, In your routes, add these
Route::get('login', 'LoginController#show')->name('login.show');
Route::post('login', 'LoginController#login')->name('login.post');
Route::get('register', 'RegisterController#show')->name('register.show');
Route::post('register', 'RegisterController#register')->name('register.post');
Route::get('logout', 'LoginController#logout')->name('login.logout');
Now, in your form add a dropdown/radio button for User Type selection (you can also make seprate and run different routes and make these fields hidden);
Say,
<select name="type">
<option value="1">Buyer</option>
<option value="2">Seller</option>
<select>
Your RegisterController#register can be as follows:
use App\Buyer;
use App\Seller;
use Validator;
class RegisterController extends Controller
{
public function show()
{
return view('auth.register');
}
public function register()
{
$inputs = request()->all();
$validator = $this->validator($inputs);
if($validator->fails()) {
return redirect()->back()->withErrors($validator)->withInput();
}
$userInputs = array_only($inputs, ['name', 'email', 'password']);
$userInputs['password'] = Hash::make($userInputs['password']);
switch($inputs['type'])
{
case 1:
$sellerInputs = array_only($inputs, ['company_name']);
$seller = Seller::create();
$user = $seller->user()->create($userInputs);
case 2:
$buyer = Buyer::create();
$user = $buyer->user()->create($userInputs);
default:
$user = null;
break;
}
if(!$user) {
return redirect()->back(); //Show flash messsage etc... and redirect back to show an error
}
auth()->attempt(array_only($inputs, ['email', 'password']));
return redirect(route('some.route'));
}
protected validator($inputs)
{
$rules = [
'name' => 'required|min:1|max:50',
'email' => 'required|email|min:1|max:100',
'password' => 'required|min:6|max:25',
// Other rules
];
$messages = [
// Any special messages if required...
];
return Validator::make($inputs, $rules, $messages);
}
}
Use same kind of coding structure in LoginController... I am simply going to write the login behind logging in below
public function login()
{
$inputs = request()->all();
//Validator etc...
if(auth()->attempt(array_only($inputs, ['email', 'password']))) {
return redirect(route('some.route'));
} else {
return redirect()->back(); // Again... Show some error flash message
}
}
Note :- I have not tested the code but I am 99% sure this should work... I wrote all this down myself... Took a damn half an hour almost!
Hope everything is answered and you understood. Let me know in the comments below if you have any other query :)

Laravel form data not saving in neo4j graph DB

I wrote a laravel form, its screen shot is as given below:
And I use neo4j for storing that form data.
Here is the code:
app/views/duck-form.blade.php
<!doctype html>
<html>
<head>
<title>Laravel Form Validation!</title>
<!-- load bootstrap -->
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<style>
body { padding-bottom:40px; padding-top:40px; }
</style>
</head>
<body class="container">
<div class="row">
<div class="col-sm-8 col-sm-offset-2">
<div class="page-header">
<h1><span class="glyphicon glyphicon-flash"></span> Register! </h1>
</div>
#if ($errors->has())
<div class="alert alert-danger">
#foreach ($errors->all() as $error)
{{ $error }}<br>
#endforeach
</div>
#endif
<!-- FORM STARTS HERE -->
<form method="POST" action="/ducks" novalidate>
<div class="form-group #if ($errors->has('name')) has-error #endif">
<label for="name">Name</label>
<input type="text" id="name" class="form-control" name="name" placeholder="Enter your name" value="{{ Input::old('name') }}">
#if ($errors->has('name')) <p class="help-block">{{ $errors->first('name') }}</p> #endif
</div>
<div class="form-group #if ($errors->has('email')) has-error #endif">
<label for="email">Email</label>
<input type="text" id="email" class="form-control" name="email" placeholder="Enter your email id" value="{{ Input::old('email') }}">
#if ($errors->has('email')) <p class="help-block">{{ $errors->first('email') }}</p> #endif
</div>
<div class="form-group #if ($errors->has('password')) has-error #endif">
<label for="password">Password</label>
<input type="password" id="password" class="form-control" name="password">
#if ($errors->has('password')) <p class="help-block">{{ $errors->first('password') }}</p> #endif
</div>
<div class="form-group #if ($errors->has('password_confirm')) has-error #endif">
<label for="password_confirm">Confirm Password</label>
<input type="password" id="password_confirm" class="form-control" name="password_confirm">
#if ($errors->has('password_confirm')) <p class="help-block">{{ $errors->first('password_confirm') }}</p> #endif
</div>
<button type="submit" class="btn btn-success">Submit</button>
</form>
</div>
</div>
</body>
</html>
1. I added 'Artdarek\Neo4j\Neo4jServiceProvider' to providers array in app/config/app.php.
2. I added neo4j configuration in app/config/database.php
'neo4j' => [
'default' => [
'host' => 'localhost',
'port' => 7474,
'username' => null,
'password' => null,
],
],
3. Then I added a controller for that form:
<?php
class DuckController extends BaseController {
public function showWelcome()
{
return View::make('duck');
}
}
4. This is my routes.php.
<?php
Route::get('/', function()
{
return View::make('hello');
});
// route to show the duck form
Route::get('ducks', function()
{
return View::make('duck-form');
});
// route to process the ducks form
Route::post('ducks', array('before' => 'csrf', function()
{
// create the validation rules ------------------------
$rules = array(
'name' => 'required', // just a normal required validation
'email' => 'required|email|unique:ducks', // required and must be unique in the ducks table
'password' => 'required',
'password_confirm' => 'required|same:password' // required and has to match the password field
);
// create custom validation messages ------------------
$messages = array(
'required' => 'The :attribute is really really really important.',
'same' => 'The :others must match.'
);
// do the validation ----------------------------------
// validate against the inputs from our form
$validator = Validator::make(Input::all(), $rules, $messages);
// check if the validator failed -----------------------
if ($validator->fails()) {
// redirect our user back with error messages
$messages = $validator->messages();
// also redirect them back with old inputs so they dont have to fill out the form again
// but we wont redirect them with the password they entered
return Redirect::to('ducks')
->withErrors($validator)
->withInput(Input::except('password', 'password_confirm'));
} else {
// validation successful ---------------------------
// our duck has passed all tests!
// let him enter the database
// create the data for our duck
$duck = new Duck;
$duck->name = Input::get('name');
$duck->email = Input::get('email');
$duck->password = Hash::make(Input::get('password'));
// save our duck
$duck->save();
// redirect ----------------------------------------
// redirect our user back to the form so they can do it all over again
return Redirect::to('ducks')
->with('messages', 'Hooray!');
}
}));
5. This is my model file for form:
<?php
class Duck extends Eloquent {
protected $fillable = array('name', 'email', 'password');
}
6. This is my model for neo4j:
<?php
//use Illuminate\Auth\EloquentUserProvider;
class database extends Eloquent {
public function index($name, $email, $password, $password_confirm) {
$formData = Neo4j::makeNode();
$formData->setProperty('name',$name)
->setProperty('email',$email)
->setProperty('password',$password)
->setProprty('password_confirm',$password_confirm)
->save();
}
}
When I click on that submit button in that form, I get this error:
[ Edit ]
I was working on it:
This is the new error which I got:
Is it a csrf token issue?
It is pointing to:
Route::filter('csrf', function()
{
if (Session::token() != Input::get('_token'))
{
throw new Illuminate\Session\TokenMismatchException;
}
});
I'm unable to resolve it since 5 hours and data isn't getting stored in neo4j DB. How can I fix it?
When using the CSRF Protection Filter, your form must be declared in your blade as such:
{{ Form::open(array('method' => 'POST', 'action' => URL::to("/ducks"))) }}
And closed:
{{ Form::close() }}
This will render the same form in your html as using:
<form method="POST" action="{{ URL::to("/ducks") }}">...</form>
But will also add the hidden _token element that you are missing:
<input type="hidden" name="_token" value="value_of_token"/>
Hope that helps!
Edit
Or, if you don't want to recreate your <form>, you may simply use:
{{ Form::token() }}
Somewhere inside your existing <form> tag to create it.
Your code looks fine, might be you need to run
composer dump-autoload

Categories