Laravel 5.5, cant pass data after validation - php

i have this validation for registering new users but every time I submit it just reloads and stays in the page, I am lost, but when I use the Registration::create($request->all()) without validation from the top it pushes through and saves the data. please help
my controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Registration;
class RegistrationsController extends Controller
{
public function index(){
return view('registrations.create');
}
public function store(){
request()->validate([
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
$validatedUser = Registration::create([
'name' => request('name'),
'email' => request('email'),
'password' => bcrypt(request('password'))
]);
return redirect()->home();
}
}
here is my create.blade
<form action="{{ route('registrations.store') }}" method="POST">
{{ csrf_field() }}
<div class="form-group">
<label for="">Full Name</label>
<input type="text" class="form-control" name="name" id="name" aria-describedby="helpId" placeholder="Juan Dela Cruz">
<small id="helpId" class="form-text text-muted">Ex. Juan Dela Cruz</small>
</div>
<div class="form-group">
<label for="">Password</label>
<input type="password" class="form-control" name="password" id="password" placeholder="type password here..">
</div>
<div class="form-group">
<label for="">Confirm password</label>
<input type="password" class="form-control" name="password_confirm" id="password_confirm" placeholder="type password here..">
</div>
<div class="form-group">
<label for="">Email address</label>
<input type="email" class="form-control" name="email" id="email" aria-describedby="emailHelpId" placeholder="juandelacruz#gmail.com">
<small id="emailHelpId" class="form-text text-muted">Must be valid email address</small>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
for my route i just used Route::resource
thank you

This is probably because your password confirmation fails.
Your confirmation field must be named : password_confirmation and not password_confirm as the documentation say :
The field under validation must have a matching field of foo_confirmation. For example, if the field under validation is password, a matching password_confirmation field must be present in the input.

Related

data was not stored in laravel and not getting any errors

I tried to store data but data not store to database, the field in database and form input already match but still can't store data, and there is no actual message error. please help.
this is my controller:
public function store(Request $request)
{
$validatedData = $request->validate([
'kabupaten' => ['required'],
'provinsi' => ['required'],
'unit' => ['required'],
'satuan_kerja' => ['required'],
'nama_area' => ['required'],
'kode_area' => ['required']
]);
Area::create($validatedData);
return redirect('/dashboard/areas')->with('success','Area baru telah ditambahkan!');
}
this is the form input:
<form action="/dashboard/areas" method="POST">
#csrf
<div class="mb-3">
<label for="provinsi" class="form-label">Provinsi</label>
<input type="text" class="form-control" id="provinsi" name="provinsi" value="Jawa Tengah">
</div>
<div class="mb-3">
<label for="kabupaten" class="form-label">Kabupaten</label>
<input type="text" class="form-control" id="kabupaten" name="kabupaten" value="Brebes">
</div>
<div class="mb-3">
<label for="unit" class="form-label">Unit</label>
<input type="text" class="form-control" id="unit" name="unit" value="Pemerintah Kabupaten Brebes">
</div>
<div class="mb-3">
<label for="satuan_kerja" class="form-label">Satuan Kerja</label>
<input type="text" class="form-control" id="satuan_kerja" name="satuan_kerja" value="Pemerintah Desa Dumeling">
</div>
<div class="mb-3">
<label for="nama_area" class="form-label">Nama Area</label>
<input type="text" class="form-control" id="nama_area" name="nama_area">
</div>
<div class="mb-3">
<label for="kode_lokasi" class="form-label">Kode Lokasi</label>
<input type="text" class="form-control" id="kode_lokasi" name="kode_lokasi">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
And this is my area model:
class Area extends Model
{
use HasFactory;
protected $primaryKey = 'id_area';
protected $guarded = [
'id_area'
];
public function aset(){
return $this->hasMany(Aset::class, 'id_area');
}
}
Thank you if there anyone can help me with this problem, I really appreciate it.
So most likely your validation is failing, what you need to do is to display the results of the failed validation error messages, and you can do so in your blade file:
#if ($errors->any())
#foreach ($errors->all() as $error)
<div>{{$error}}</div>
#endforeach
#endif
You may read more on how to display the errors here: https://laravel.com/docs/9.x/validation#quick-displaying-the-validation-errors
You can as well display it per input field or change the class of the input method, etc.. check the #error directive from here: https://laravel.com/docs/9.x/validation#the-at-error-directive

Request contains null although the input has been entered

I'm trying to update my data with Laravel. I'm able to create, read, delete the data but somehow i cannot update my data. I already checked my controller,model,route and view but i don't think there's any typo or anything. It only redirects to it's index page without being updated although i have entered new input. There's no error message at all so i checked where is the problem. So i checked my update function in my controller and tried to show the request by echo "$request->kode_kontak"; and echo $request->kode_kontak; but it shows nothing which i assume that it's null/empty but when i echo "yes" it showed on the screen "yes" i tested this because i want to know if the function itself is working so the problem here is that the request contains null, no wonder i cannot update it. Why is the request isn't passed? why is it like this? and how to fix it?
Route for edit and update
Route::get('contact/{contact}/edit', 'ContactController#edit')->name('contact.edit');
Route::patch('contact/{contact}','ContactController#update')->name('contact.update');
Controller with edit and update function
use Illuminate\Http\Request;
use App\Contact;
use DB;
public function edit($kode_kontak){
$contact = DB::table('contact')->where('kode_kontak',$kode_kontak)->get();
return view('contact.edit',['contact' => $contact]);
}
public function update(Request $request){
DB::table('contact')->where('kode_kontak',$request->kode_kontak)->update([
'email' => $request->email,
'telepon' => $request->telepon,
]);
return redirect('contact');
}
Model
class Contact extends Model
{
public $timestamps = false;
protected $table = 'contact';
protected $fillable = [
'kode_kontak',
'kode_pegawai',
'email',
'telepon'
];
protected $primaryKey = 'kode_kontak';
}
View of edit.blade.php
<div id="contact">
<h2>Edit Contact</h2>
#foreach($contact as $p)
<form action="{{ route('contact.update', ['kode_pegawai' => $p->kode_pegawai]) }}" method="POST">
#csrf
#method('patch')
<div class="form-group">
<label for="kode_contact" class="control-label">Kode Kontak</label>
<input type="text" name="kode_kontak" id="kode_kontak" class="form-control" value="{{ $p->kode_kontak}}" disabled>
</div>
<div class="form-group">
<label for="kode_pegawai" class="control-label">Kode Pegawai</label>
<input type="text" name="kode_pegawai" id="kode_pegawai" class="form-control" value="{{ $p->kode_pegawai}}" disabled>
</div>
<div class="form-group">
<label for="email" class="control-label">Email</label>
<input type="text" name="email" id="email" class="form-control" value="{{ $p->email}}">
</div>
<div class="form-group">
<label for="telepon" class="control-label">Telepon</label>
<input type="text" name="telepon" id="telepon" class="form-control" value="{{ $p->telepon}}">
</div>
<div class="form-group">
<input class="btn btn-primary form-control" type="submit" value="Simpan">
</div>
</form>
#endforeach
</div>
Your issue is that you have disabled those inputs. Disabled inputs will not be submitted.
If you want to display the disabled inputs, but still PATCH the values, you will need to add hidden inputs with those values like:
<div class="form-group">
<label for="kode_contact" class="control-label">Kode Kontak</label>
<input type="text" id="kode_kontak" class="form-control" value="{{ $p->kode_kontak}}" disabled>
<input type="hidden" name="kode_kontak" value="{{ $p->kode_kontak}}">
</div>
<div class="form-group">
<label for="kode_pegawai" class="control-label">Kode Pegawai</label>
<input type="text" id="kode_pegawai" class="form-control" value="{{ $p->kode_pegawai}}" disabled>
<input type="hidden" name="kode_pegawai" value="{{ $p->kode_pegawai}}">
</div>
Hope that helps!
$request->kode_kontak is $contact here, $request->kode_kontak is not available in $request, change $contact instead :
public function update(Request $request, $contact){
DB::table('contact')->where('kode_kontak',$contact)->update([
'email' => $request->email,
'telepon' => $request->telepon,
]);
return redirect('contact');
}

POST request is not tallied with the form in Laravel Blade

I am a newbie in Laravel.
My problem is, when I tried to update my form, it kept saying that the tablename not found eventhough I already mentioned it inside my Model. But when I debugged, I found that the request is not the same from what I put inside my form.
But my form actually doesn't have that.
Any idea how this is happening, guys? Pretty sure I missed something but unsure what is it.
Event Model
class Event extends Model
{
//
protected $fillable = ['title', 'objective', 'date', 'venue', 'description', 'slug'];
public function getRouteKeyName()
{
return 'slug';
}
}
Event Controller
class EventController extends Controller
{
public function update(Request $request, Event $event)
{
//
$validated = $request->validate([
'title' => 'required|string|unique:event|min:5|max:100',
'objective' => 'required|string|min:5|max:2000',
'date' => 'required|string|min:5|max:2000',
'venue' => 'required|string|min:5|max:2000',
'description' => 'required|string|min:5|max:2000'
]);
// Create slug from title
$validated['slug'] = Str::slug($validated['title'], '-');
// Update Post with validated data
$event->update($validated);
// Redirect the user to the created post woth an updated notification
return redirect(route('events.edit', [$event->slug]))->with('notification', 'Event updated!');
}
Edit Blade Page
<form method="post" action="{{ route('events.update', [$event->slug]) }}">
#csrf
#method('patch')
#include('partials.errors')
<div class="field">
<label class="label">Title</label>
<div class="control">
<input type="text" name="title" value="{{ $event->title }}" class="input" placeholder="Title" minlength="5" maxlength="100" required />
</div>
</div>
<div class="field">
<label class="label">Objective</label>
<div class="control">
<textarea name="content" class="textarea" placeholder="Content" minlength="5" maxlength="2000" required rows="10">{{ $event->objective }}</textarea>
</div>
</div>
<div class="field">
<label class="label">Date</label>
<div class="control">
<input type="text" name="title" value="{{ $event->date }}" class="input" placeholder="Title" minlength="5" maxlength="100" required />
</div>
</div>
<div class="field">
<label class="label">Venue</label>
<div class="control">
<input type="text" name="title" value="{{ $event->venue }}" class="input" placeholder="Title" minlength="5" maxlength="100" required />
</div>
</div>
<div class="field">
<label class="label">Description</label>
<div class="control">
<textarea name="content" class="textarea" placeholder="Content" minlength="5" maxlength="2000" required rows="10">{{ $event->description }}</textarea>
</div>
</div>
<div class="field">
<div class="control">
<button type="submit" class="button is-link is-outlined">Update</button>
</div>
</div>
</form>
Thank you for your time!
You need to change the name attribute of Date, Venue and Description and Objective.
On your edit blade page, the input names are alternating 'title' and 'content'
can you rename the input names so that they can be unique

Input field not required (nullable) Laravel 5.6

I'm having an issue with my contact form. All the fields are required except for one field. Normally I would in migration insert nullable, but apparently, it doesn't work. I have tried to make a nullable in validation, but this doesn't work either. So I'm a bit confused.
public function up()
{
Schema::create('kontaktforms', function (Blueprint $table) {
$table->increments('id');
$table->string('navn');
$table->string('mobilnr');
$table->string('fastnetnr')->nullable();
$table->string('mail');
$table->string('emne');
$table->text('beskrivelse');
$table->timestamps();
});
}
public function store(Request $request)
{
$this->validate($request, [
'navn' => 'required',
'mobil' => 'required',
'email' => 'required',
'emne' => 'required',
'beskrivelse' => 'required'
]);
$kontakt = new Kontaktform([
'navn' => $request['navn'],
'mobilnr' => $request['mobil'],
'fastnetnr' => $request['fastnetnr'],
'mail' => $request['email'],
'emne' => $request['emne'],
'beskrivelse' => $request['beskrivelse']
]);
$kontakt->save();
Session::flash('success', 'Vi har nu modtaget din besked');
return redirect()->route('kontakt.create');
}
Form
<form id="form-contact" action="{{route('kontakt.store')}}" method="POST">
#csrf
<h1 class="display-4">Kontakt os</h1>
<div class="form-group">
<input name="navn" type="text" class="form-control" placeholder="Dit navn...">
</div>
<div class="form-group">
<input name="mobil" type="text" class="form-control" placeholder="Din mobil">
</div>
<div class="form-group">
<input name="fastnetnr" type="text" class="form-control" placeholder="Evt fastnetnr">
</div>
<div class="form-group">
<input name="email" type="email" class="form-control" placeholder="Din email">
</div>
<div class="form-group">
<input name="emne" type="text" class="form-control" placeholder="Emne">
</div>
<div class="form-group">
<textarea name="beskrivelse" class="form-control" placeholder="Skriv din besked her" rows="4"></textarea>
</div>
<br>
<input type="submit" class="btn btn-primary btn-block" value="Send">
<hr>
</form>
Do migration for nullable field as
$table->string('fieldname')->nullable();
and during validation either by using Validator or FormRequest confirm that you haven't added a required attribute
'fieldname' => 'required|integer'
you must have only
'fieldname' => 'integer'
I am not sure what are you trying to do but the table kontaktforms does not have any field called fastnetnr which you are trying to enter from your controller.
Maybe add the field in the migration, run migration again after rolling back and then try?

For Get and Post request a new session is being generated which throws TokenMismatchException in Laravel 5.1

I am working with simple login application, where there is a form of login on "GET" Request. On get request a session file is created in "storage/framework/session/" folder. That includes the csrf-token on the form equivalent to session token but, when I a submit that form with post request again a session file had been created which holds different csrf-token value. So, it seems when both token has been compared they didn't match ie. "form-csrf-token" and "session-csrf-token". Ultimately it throws TokenMismatchException.
I want to know that how can I fix this?
I don't want to use CSRF verification exclusion, because it will become a big security issue for me to not to use CSRF Verification.
I am using Form façade blade template to generate a Form.
Here is the code of route.php
Route::get('/', function(){
if(Auth::check()){
return redirect('home');
}
return view('pages.index');
});
Route::post('auth/login', 'Auth\AuthController#postLogin');
Route::post('auth/register', 'Auth\AuthController#postRegister');
Route::get('home', 'PageController#home')->middleware(['auth']);
Route::get('about','PageController#about');
// Authentication Routes...
Route::get('auth/login', 'Auth\AuthController#getLogin');
Route::get('auth/logout', 'Auth\AuthController#getLogout');
// Registration Routes...
Route::get('auth/register', 'Auth\AuthController#getRegister');
Auth\AuthController.php
class AuthController extends Controller
{
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
protected $redirectTo = '/home';
/**
* Create a new authentication controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'getLogout']);
}
/**
* Get a validator for an incoming registration request.
*
* #param array $data
* #return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'FirstName' => 'required|max:255',
'Gender' => 'required|in:Male,Female,Trans',
'DateOfBirth' => 'required|date|before:today',
'email' => 'required|email|max:255|unique:users,email',
'password' => 'required|min:6',
'confirmed' => 'required|same:password'
]);
}
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return User
*/
protected function create(array $data)
{
return User::create([
'FirstName' => $data['FirstName'],
'Surname' => $data['surname'],
'DateOfBirth' => $data['DateOfBirth'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'Gender' => $data['Gender']
]);
}
}
loginsignup.blade.php
<div class="w3-container">
<div class="w3-row w3-padding-top w3-right">
<div class="col left">
<h3>Create an account</h3>
<h5>It's free and always will be.</h5>
{!! Form::open(array('url'=>'auth/register','method'=>'POST','id'=>'formRegister')) !!}
<div class="w3-group">
<input type="text" class="w3-input register" id="FirstName" name="FirstName" required>
<label class="w3-label w3-text-theme">First Name</label>
</div>
<div class="w3-group">
<input type="text" class="w3-input register" id="surname" name="surname" required>
<label class="w3-label w3-text-theme">Surname</label>
</div>
<div class="w3-group">
<input type="date" class="w3-input register" id="DateOfBirth" name="DateOfBirth" required>
<label class="w3-label w3-text-theme">Date of Birth</label>
</div>
<div class="w3-group">
<input type="text" class="w3-input register" id="email" name="email" required>
<label class="w3-label w3-text-theme">Email</label>
</div>
<div class="w3-group">
<input type="password" class="w3-input register" id="password" name="password" required>
<label class="w3-label w3-text-theme">New password</label>
</div>
<div class="w3-group">
<input type="password" class="w3-input register" id="confirmed" name="confirmed" required>
<label class="w3-label w3-text-theme">Re-enter password</label>
</div>
<label class="w3-checkbox w3-text-theme">
<input type="radio" name="Gender" value="Male" checked>
<span class="w3-checkmark"></span> Male
</label>
<label class="w3-checkbox w3-text-theme">
<input type="radio" name="Gender" value="Female">
<span class="w3-checkmark"></span> Female
</label>
<label class="w3-checkbox w3-text-theme">
<input type="radio" name="Gender" value="Trans">
<span class="w3-checkmark"></span> Trans
</label>
<br><br>
<button class="w3-btn w3-theme"> Create an account </button>
{!! Form::close() !!}
</div>
<div class="col right">
<button class="btn facebook" data-provider="facebook"><i></i><span>Facebook</span></button>
<button class="btn twitter" data-provider="twitter"><i></i><span>Twitter</span></button>
<button class="btn plus" data-provider="google plus"><span class="i"><i></i></span><span>Google Plus</span></button>
<h3>Sign In</h3>
{!! Form::open(array('url'=>'auth/login','method'=>'POST','id'=>'formLogin')) !!}
<div class="w3-group">
<input type="email" class="w3-input" id="email" name="email" required>
<label class="w3-label w3-text-theme">Email or phone</label>
</div>
<div class="w3-group">
<input type="password" class="w3-input" id="password" name="password" required>
<label class="w3-label w3-text-theme">Password</label>
</div>
<label class="w3-checkbox">
<input type="checkbox" id="remember" name="remember">
<div class="w3-checkmark"></div> Stay Logged In
</label>
<div class="w3-group"> Forgot Your Password ?</div>
<button id="signInSubmit" type="submit" class="w3-btn w3-theme">Submit</button>
{!! Form::close() !!}
</div>
</div>
Steps I followed and analysed problem
This is when I first opened http://localhost:8000
After sending form using post request
I have found answer by self. Actually it's not a problem. It happens when a session has been generated for the system after being logged in.
You need to get logged out from application.
That's it.

Categories