I can't get back()->withInput() to work, the documentation seems a little sparse on how it should work - laravel

I'm using blade templates and this is how my form looks:
<form action="{{ route("user-sessions.store") }}" method="POST">
#csrf
<div class="form-group">
<label for="e-mail">E-mail Address</label>
<input name="email_address" class="form-control" type="email" />
</div>
<div class="form-group">
<label for="password">Password</label>
<input name="password" class="form-control" type="password" />
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" />
</div>
</form>
Does back()->withInput() only work with {{ Form::open() }}? If so, the 7.x documentation doesn't say that it seems. I would think if you need a 3rd party library to get this to work, the documentation should say so. If that's not the issue, then what am I doing wrong?
I'm using Laravel 7.x.

if you use html collective, it automatically puts old values in form inputs. But when use pure html to create form inputs you have to use old method for input values. like this:
<form action="{{ route("user-sessions.store") }}" method="POST">
#csrf
<div class="form-group">
<label for="e-mail">E-mail Address</label>
<input name="email_address" class="form-control" type="email" value="{{old('email_address')}}"/>
</div>
<div class="form-group">
<label for="password">Password</label>
<input name="password" class="form-control" type="password" value="{{old('password')}}"/>
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" />
</div>
</form>

Related

How to submit traditional blade form from livewire component?

I am trying to reuse laravel's default authentication components in livewire component. But when the login form is submitting I am getting a console error
wire-directives.js:86 Uncaught TypeError: Cannot read properties of
null (reading 'match')
The login form working perfectly fine when I am using the default login blade. But when I am reusing the login form in my livewire component, it is not working. Here is my code:
In my livewire component, I have this
<div>
<x-login-form/>
</div>
and the login form component is laravels default login form:
<form role="form" class="text-start" method="POST" action="{{ route('login') }}">
#csrf
<div class="input-group input-group-outline my-3">
<input type="email" id="email" class="form-control" name="email" :value="old('email')" required placeholder="email">
</div>
<div class="input-group input-group-outline mb-3">
<input type="password" class="form-control" id="password" type="password" name="password" required autocomplete="current-password" placeholder="password">
</div>
<div class="form-check form-switch d-flex align-items-center mb-3">
<input class="form-check-input" type="checkbox" id="remember_me" name="remember">
<label class="form-check-label mb-0 ms-2" for="rememberMe">Remember Me</label>
</div>
<div class="text-center">
<button type="submit" class="btn bg-gradient-primary w-100 my-4 mb-2">Sign In</button>
</div>
#if (Route::has('password.request'))
<p class="text-xs text-center">
Forgot your password?
</p>
#endif
</form>

Submitting form doesn't work after using Recaptcha v3

I'm trying to use Recaptcha v3 on my form but submitting the form only works when Recaptcha is removed.
When Recaptcha is enabled, I can click on the submit button but nothing happens. I don't even get an error message in the console even though I can see the protected by reCAPTCHA logo in the frontend on the bottom right.
I'm using Laravel 6.
My form template looks like this:
<div class="row mt-150">
<div class="col-12 text-center text-white">
<h1>Contact</h1>
</div>
</div>
<div class="row justify-content-center">
<div class="col-md-4 col-sm-12">
<form action="{{route('contact_form')}}" method="POST">
#csrf
<div class="form-group">
<label for="email" class="text-white">Email address</label>
<input type="email" class="form-control" id="email" name="email" required>
</div>
<div class="form-group">
<label for="textarea" class="text-white">Message</label>
<textarea class="form-control" id="textarea" rows="3" name="message" required></textarea>
</div>
<input type="submit"
id="submitButton"
class="g-recaptcha btn btn-primary"
data-sitekey="MY-PUBLIC-KEY"
data-callback='onSubmit'
data-action='submit'
name="submit">
</form>
</div>
</div>
<script src="https://www.google.com/recaptcha/api.js"></script>
There is also an app.js included which contains the captcha callback function:
function onSubmit(token) {
document.getElementById("submitButton").submit();
}
window.onSubmit = onSubmit;
Can someone give me a hint what I'm doing wrong implementing recaptcha?
I tried to do it just like this:
Google reCaptcha v3
This will not work because you are trying to trigger submit() on a button. Give the form an id and then trigger submit() on the form
<form action="{{route('contact_form')}}" method="POST" id="contactForm">
#csrf
<div class="form-group">
<label for="email" class="text-white">Email address</label>
<input type="email" class="form-control" id="email" name="email" required>
</div>
<div class="form-group">
<label for="textarea" class="text-white">Message</label>
<textarea class="form-control" id="textarea" rows="3" name="message" required></textarea>
</div>
<input type="submit"
id="submitButton"
class="g-recaptcha btn btn-primary"
data-sitekey="MY-PUBLIC-KEY"
data-callback='onSubmit'
data-action='submit'
name="submit">
</form>
Then in javascript get the form and trigger submit on it
function onSubmit(token) {
document.getElementById("contactForm").submit();
}
window.onSubmit = onSubmit;

i m trying to view my edit form. but it gives route missing parameters for update route

here is my code.
Editpost.blade.php
<form action="{{ route('update_post') }}" method="POST" enctype="multipart/form-data">
#csrf
<div class="container">
<div class="header">
<h1>Edit Post</h1>
<p>Please fill in this form to update the post.</p>
</div>
<input type="hidden" name="id" value="{{ $post->id }} ">
<label for="name"><b>Post Name</b></label>
<input type="text" placeholder="Enter Post Name" name="name" value="{{ $post->name }}"><br><br>
<label for="link"><b>Post Link</b></label>
<input type="url" placeholder="Enter Link" name="url" value="{{ $post->url }}"><br><br>
<label for="image"><b>Post Image</b></label>
<input type="file" placeholder="Upload Image" name="image" value="{{ $post->image }}"><br><br>
<input type="submit" value="submit">
{{--
<div class="clearfix">
<button type="submit" class="cancelbtn">Sign Up</button>
<button type="button" class="signupbtn">Sign In</button>
</div>
--}}
</div>
</form>
web.php
route::get('admin/show-post/edit-post/{id}','MyController#edit_post')->name('edit_post');
route::post('admin/show-post/edit-post/update-post/{id}','MyController#update_post')->name('update_post');
this error happened
Facade\Ignition\Exceptions\ViewException
Missing required parameters for [Route: update_post] [URI: admin/show-post/edit-post/update-post/{id}]. (View: C:\wamp64\www\portfolio\resources\views\editpost.blade.php)
http://localhost/portfolio/admin/show-post/edit-post/6
You have to modify your form action, to pass the id you want to update
action="{{ route('update_post', ['id' => $post->id]) }}

Laravel csrf_field() Is Blank

I am learning about Packages in Laravel 5.4 and ultimately I will upload them to github.
This is my directory structure :
laravel
-vendor
--student
---myPackage
----src
-----Views
------myView.blade.php
the myView.blade.php has a form in it, and a {{csrf_field()}} function call, but when I inspect the output in the browser, the value attribute of the hidden input <input name="_token" value="" type="hidden"> is empty.
How do I make this work ?
SOLUTION
Apparantly, I needed to make sure that the web middleware is in use
for my routes, so I changed Route::get('/Register/{group}',
'Student\myPackage\Http\User#create'); to
Route::get('/Register/{group}',
'Student\myPackage\Http\User#create')->middleware('web');
Route:
Route::get('/Register/{group}', 'Student\myPackage\Http\User#create');
Route::post('/Register/{group}', 'Student\myPackage\Http\User#store');
Controller:
public function create($group){
return view('StudentUser::app', ['group' => $group]);
}
public function store($group)
{
dd(request()->all());
}
Form:
<form class="form-horizontal" role="form" method="POST" action="/Register/{{$group}}">
<div class="tab-content">
<div id="ap-about" class="tab-pane active">
{!! csrf_token() !!}
<div class="form-group">
<label for="name" class="col-md-4 control-label">Name</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control" name="name" value="" required autofocus>
</div>
</div>
<div class="form-group">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email" value="" required>
</div>
</div>
<div class="form-group">
<label for="password" class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>
</div>
</div>
<div class="form-group">
<label for="password-confirm" class="col-md-4 control-label">Confirm Password</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="button" class="btn btn-primary">
Next
</button>
</div>
</div>
</div>
<div id="ap-personal" class="tab-pane">
<div class="form-group">
<label for="address" class="col-md-4 control-label">Address</label>
<div class="col-md-6">
<input id="address" type="text" class="form-control" name="personal[]" value="" required autofocus>
</div>
</div>
<div class="form-group">
<label for="url" class="col-md-4 control-label">url</label>
<div class="col-md-6">
<input id="url" type="text" class="form-control" name="personal[]" value="" required autofocus>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Register
</button>
</div>
</div>
</div>
</div>
</form>

Select/Option Laravel 5.3

I want to use select and option in laravel 5.3 in simple HTML form. How to use select and option in laravel 5.3 in HTML form?
My coding:
#extends('layout.navigation')
<html>
<head>
</script>
</head>
<div class="row" id="contact_form">
<div class="col-sm-4">
<form action="{{route('online_form')}}" method="post" enctype="multipart/form-data">
{!!csrf_field()!!}
<br/><br/>
<div class="panel panel-default" id="form_panel">
<div class="panel-body">
<p>fill the form </p>
<input type="text" name="username" class="form-control" placeholder="Name" required>
<br/>
<input type="text" name="email" class="form-control" placeholder="Email" required><br/>
<p style="margin-top:20px;font-size:14px">Gender :&nbsp&nbsp&nbsp&nbsp <label
class="radio-inline"><input type="radio" style="margin-top:1px" name="gender"
value="Male">Male</label>
<label class="radio-inline"><input type="radio" style="margin-top:1px"
name="gender" value="female">Female<br/></label>
<p style="margin-top:20px;font-size:14px"> Select Language
:&nbsp&nbsp&nbsp&nbsp<label class="checkbox-inline"><input type="checkbox"
name="language" value="english">English</label>
<label class="checkbox-inline"><input type="checkbox" name="language"
value="hindi">Hindi</label>
<br/><br/>
<select id="state">
<option value="rajasthan">Rajasthan</option>
<option value="madhya pradesh">Madhya Pradesh</option>
</select>
<input type="submit" name="submit" value="Enter" class="btn btn-primary" >
</form>
</div>
</div>
To get the state value in the controller method, you can use Input facade.
$state = Input::get('state');
You can include the following as a dependency in composer "laravelcollective/html": "~5.0" and then include it in your config/app
Now, you can create your select dropdown using:
echo Form::select('gender', array('Male', 'Female'));
echo Form::select('language', array('English', 'Hindi'));
and so on.
For more information on this collective,
check this out.

Resources