I am creating referral system so I have the following routes
// Registration routes...
Route::get('auth/register/{id}', 'Auth\AuthController#getRegister');
Route::post('auth/register', 'Auth\AuthController#postRegister');
and my RegisterUser.php is changed to
public function getRegister($id)
{
return view('auth.register')->withName($id);
}
and my blade looks like
<div class="form-group">
<label class="col-md-4 control-label">Company</label>
<div class="col-md-6">
<input type="text" class="form-control" name="company" value="{{ old('company') }}" readonly disabled>
</div>
</div>
in AuthController I have:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'company' => $data['company'],
'password' => bcrypt($data['password']),
]);
}
and the value="{{ old('company') }}" is causing the problem. When it is like that it works. But I want the value to be value="{{$name}}" given from return view('auth.register')->withName($id); So when I go to route auth/register/something in the input field I have got the 'something' so it is working but I have the error code "Undefined index: company". When I remove the value at all it is working but I need this value. Any suggestions would be helpful.
The Problem of your code is the disabled attribute in the input "company", why ? well a disabled element isn't editable and isn't sent on submit. so Laravel doesn't receive it so you will be able to access via the helpers old.
Remove the disabled attribute and the magic happens.
<input type="text" class="form-control" name="company" value="{{ old('company') }}" readonly >
Related
I am getting data from backend and I want users to verify and update. What I am doing is passing the data from controller and fill them inside a form in view blade. When the user verifies the data and submit them, I pass them to validation in my method inside controller. As soon as the validation start laravel throws error:
\Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException",
"The GET method is not supported for this route. Supported methods:
POST.
Below is one the method in FormsController.php passing the variable that have been collected from database to view forms.oneedit.blade.php
public function editingone(Request $request)
{
$nameinst = $request->nameinst;
$firstnm = $request->firstnm;
$lastnm = $request->lastnm;
$phn = $request->phn;
$myArray['nameinst'] = $nameinst;
$myArray['firstnm'] = $firstnm;
$myArray['lastnm'] = $lastnm;
$myArray['phn'] = $phn;
return view('forms.oneedit', $myArray);
}
Below I receive the data in forms.oneedit.blade.php in view
<form class="w-full max-w-lg border-4 rounded-lg p-2" action="{{ route('updateeditedone.fomrone') }}" method="post">
#csrf
<input class="#error('firstname') border-red-500 #enderror"
id="firstname" name="firstname" type="text" value="{{ old('firstname', $firstnm) }}" required>
#error('firstname')
<div class="text-red-500 mt-2 text-sm">
{{ $message }}
</div>
#enderror
......................
<input class="
#error('lastname') border-red-500 #enderror"
id="lastname" name="lastname" type="text" value="{{ old('lastname', $lastnm) }}" required>
#error('lastname')
<div class="text-red-500 mt-2 text-sm">
{{ $message }}
</div>
#enderror
......................
<button type="submit">Submit</button> </form>
Below are among the routes in web.php
Route::post('/editone/formone', [FormsController::class,'editingone'])->name('edit.fomrone');
Route::post('/update/edited/formone', [FormsController::class,'updateeditedngone'])->name('updateeditedone.fomrone');
Below is the method that I am trying to validate the values in FormsController.php where the error occurs
public function updateeditedngone(Request $request)
{
$this->validate($request, [
'nameinstitute'=> 'required|max:255',
'firstname' => 'max:255',
'lastname' => 'max:255',
'phone' => 'required|max:255',
]); }
NB: If I remove the validation process inside the controller and just get the value it works, something like below:
$val = $request->nameinstitute;
dd($val);
With the above I correctly get the values before validation, But if I try to validate them first the error is thrown. Thanks in advance.
Update:
I have edited the validation method so as to direct to a certain view as suggested but still the same error
public function updateeditedngone(Request $request)
{
$validator = Validator::make($request->all(),
[ 'nameinstitute'=> 'required|max:255','firstname' => 'max:255',
'lastname' => 'max:255',
'phone' => 'required|max:255']);
if ($validator->fails())
{
Session::flash('error', $validator->messages()->first());
return redirect()->back()->withInput();
}
dd($validator);
return redirect()->route('form.checkbtn');
php artisan view:clear php artisan cache:clear php artisan route:clear
Run it from CMD
It works for me
I have a webpage with 4 different urls
www.sample.com\home
www.sample.com\about
www.sample.com\products
www.sample.com\contact
I have a contact form in all the pages of my webpage.
I need to know the Page, from where the contact form is submitted from either(home, about, products or services).
I use laravel mailer to send mail, once the contact form is submitted.
Contact form:
<input type="hidden" name="url" value="{{substr(strrchr(url()->current(),'/'),1)}}">
<form method="POST" action="sendEmail">
<label for="name">Name</label>
<input type="text" name="name" id="name" value="{{ old('name') }}" />
<label for="email">Email</label>
<input type="email" name="email" id="email" value ="{{ old('email') }}"/>
<label for="message">Message</label>
<textarea name="body" id="message" rows="5"> {{ old('message') }}</textarea>
<button class ="primary"> Submit </button>
</form>
Controller:
use Illuminate\Support\Facades\Request as PostRequest;
public function store()
{
$data = request()->validate([
'name' => 'required',
'email' => 'required|email',
'body' => 'required'
]);
// To get the current URL
$currentPage = PostRequest::input('url');
\Mail::send('E-mail view', $data, $currentPage, function($message) use ($data, $currentPage){
$message->to('abc#xyz.com')
->from($data['email'], $data['name'])
->replyTo($data['email'], $data['name'])
->returnPath($currentPage)
->subject('Notification');
});
return back();
}
I need the URL as home, about, products, contact, from where the Contact form is submitted from not the form action sendEmail inside the E-mail View blade file
Email View Blade:
<p> $name </p>
<p> $email</p>
<p> $currentPage </p>
It throws an Error
Function name must be a string
How to pass the current URL from Where the Form is submiited from (home, about,..) to Mail?
Could anyone please help?
Many thanks.
Try like this
use Illuminate\Support\Facades\Request as PostRequest;
public function store()
{
$data = request()->validate([
'name' => 'required',
'email' => 'required|email',
'body' => 'required'
]);
// To get the current URL
$currentPage = request()->url();
$data['currentPage'] = $currentPage;
\Mail::send('E-mail view', $data, function($message) use ($data, $currentPage){
$message->to('abc#xyz.com')
->from($data['email'], $data['name'])
->replyTo($data['email'], $data['name'])
->returnPath($currentPage)
->subject('Notification');
});
return back();
}
I'm trying to insert my data to database from form.
My URL to create the data is web.com/siswa/create
But when I click submit system show error MethodNotAllowedHttpException.
How I can fix it? Is there anything wrong with my code?
Here is my form:
<form action="{{ url('siswa') }}" method="POST">
<div class="form-group">
<label for="exampleInputEmail1">NISN</label>
<input type="text" class="form-control" name="nisn" id="nisn" placeholder="NISN"></div>
<div class="form-group">
<label for="exampleInputEmail1">Nama Siswa</label>
<input type="text" class="form-control" name="nama_siswa" id="nama_siswa" placeholder="Nama Siswa"> </div>
<button type="submit" class="btn btn-success btn-sm font-weight-bold">Submit</button></form>
Controller:
public function tambah()
{
return view('siswa.create');
}
public function store(Request $request)
{
$siswa = new \App\Siswa;
$siswa->nisn = $request->nisn;
$siswa->nama_siswa = $request->nama_siswa;
$siswa->tanggal_lahir = $request->tanggal_lahir;
$siswa->jenis_kelamin = $request->jenis_kelamin;
$siswa->save();
return redirect('siswa');
}
Route:
Route::get('/siswa/create', [
'uses' => 'SiswaController#tambah',
'as' => 'tambah_siswa'
]);
Route::get('/siswa', [
'uses' => 'SiswaController#store',
'as' => 'simpan_siswa'
]);
change your store function route from get to post
Route::post('/siswa', [
'uses' => 'SiswaController#store',
'as' => 'simpan_siswa'
]);
Use Csrf protection field in your form for the session timeout error
{{ csrf_field() }}
OR
<input type="hidden" name="_token" id="csrf-token" value="{{ Session::token() }}" />
OR if you are using Form builder
{!! Form::token() !!}
In Route please use post instead of get
Route::post('/siswa','SiswaController#store');
and also include {{ csrf_field() }} in form
you are using method="POST" on your form but in on your route you are using Route::get
Use Route::post for your route
In your form you've given POST method, but your router doesn't have any POST handler. So all you have to do is , when you are trying to store data from form to DB you have to post the data, and the router should handle it.
Try this
Route::post('/siswa', [
'uses' => 'SiswaController#store',
'as' => 'simpan_siswa'
]);
You are using POST method in your form and using GET in route.
try this
Route::post( '/siswa', [
'uses' => 'SiswaController#store',
'as' => 'simpan_siswa'
] );
I used 5.4 and I've an index action in convert controller which shows the form and then have another action calculate in the convert controller. So the form has from-currency, amount, to-currency input and all of them are required.
Here's the validation I've for calculate action:
$this->validate(request(), [
'from_currency' => 'required|min:3|max:3|alpha',
'to_currency' => 'required|min:3|max:3|alpha',
'amount' => 'required|numeric',
]);
If the validation failed I want when showing the errors and the form it will prepopulate the input already.
Is there like a function I can use for Request ? I know how to get the domain/path inside blade like Request::root() and I also tried Request::input('from_currency) in the view but not work.
I even tried to set the view data like 'from_currency' => request('from_currency') and it's blank. Any idea?
When you are validating your form your request if it fail you can redirect to the same page with all the input which was submited
$validator = Validator::make($request->all(), [
'from_currency' => 'required|min:3|max:3|alpha',
'to_currency' => 'required|min:3|max:3|alpha',
'amount' => 'required|numeric',
]);
if ($validator->fails()) {
return redirect('index')
->withErrors($validator)
->withInput();
}
and in your blade view you can show the old value by ussing the old helper like this
<input type="text" name="from_currency" value="{{ old('from_currency') }}">
<input type="text" name="to_currency" value="{{ old('to_currency') }}">
<input type="text" name="amount" value="{{ old('amount') }}">
Try this
In your blade file, make sure your inputs have this:
<input type="text" ... value="{{ old('from_currency') }}" ... >.
Then in your controller...
if($validation->fails()) {
return redirect()->back()->withInput();
}
You can also user Validate instead of Validator::make.
eg
$this->validate($request, [
'question' => "required|min:10|max:100",
'answer' => "required|min:20|max:300",
'rank' => "required|numeric|gt:0|lt:100",
]);
Then in your form use
<input type="text" class="form-control" id="question" name="question" value="{{ old('question') }}">
This will automatically redirect back with input if the validator fails.
This way, you DO NOT have to include
if($validation->fails()) {
return redirect()->back()->withInput();
}
I am writing an application in Laravel 5.1. According to the documentation I can authenticate and validate my login forms using their AuthController.
The only field I want to validate is a code-input field.
<form method="POST">
<div class="form-group">
<label for="code-input">Code</label>
<input type="text" name="code-input" class="form-control" id="code-input" placeholder="Type your code here">
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}" />
<button type="submit" class="btn btn-default">Submit</button>
</form>
However, whenever I set my validation method in the AuthController to check only that field and submit an empty form I get an error that the email field is required and the password field is required. I mean, what?
protected function validator(array $data)
{
return Validator::make($data, [
'code-input' => 'required|max:255'
]);
}
My routes are also working correctly:
Route::get('/', 'Auth\AuthController#getLogin');
Route::post('/', 'Auth\AuthController#postLogin');
Route::get('/logout', 'Auth\AuthController#getLogout');
Does anybody know what is going on?
The validator method is just for creating users (registration). Login is all handled by the AuthenticatesUsers trait postLogin method, which requires email and password.
There are ways to change the "email" requirement easily, but to get rid of both fields and replace with a code, you'll have to write yourself a new postLogin method.
Did you check the postLogin function at AuthenticatesUsers.php? It could have a line of codes something like this which results the validation errors:
$this->validate($request, [
$this->loginUsername() => 'required', 'password' => 'required',
]);