Laravel: Says input required when it's there? - php

I'm trying to code a login system in Laravel and it keeps telling me the text box is required. I've added the textbox, rechecked the names are correct and I've made sure I enter text on submit of the form it belows to.
It keeps saying "The credentials.username field is required.", if I remove the required from the validation it will say it for password too.
HTML:
<form method="post">
<div id="login-columns">
<div id="login-column-1">
<label for="credentials-email">Username</label>
<input id="credentials-email" name="credentials.username" tabindex="2" type="text">
<input id="credentials-remember-me" name="_login_remember_me" tabindex="5" type="checkbox">
<label class="sub-label" for="credentials-remember-me">Keep me logged in</label>
</div>
<div id="login-column-2">
<label for="credentials-password">Password</label>
<input id="credentials-password" name="credentials.password" tabindex="3" type="password">
</div>
<input name="_token" type="hidden" value="{{ csrf_token() }}">
<div id="login-column-3">
<input style="margin: -10000px; position: absolute;" type="submit" value="Login"> <a class="button" href="#" id="credentials-submit" tabindex="4"><b></b><span>Login</span></a>
</div>
<div id="login-column-4">
888 Online
</div>
</div>
</form>
PHP:
public function onPost(Request $request)
{
$validator = Validator::make($request->all(), [
'credentials.username' => 'required|exists:users',
'credentials.password' => 'required'
]);
if ( $validator->fails()) {
return Redirect::back()->withErrors($validator->messages());
}
else {
if (!Auth::attempt(['username' => $request->input('credentials.username'), 'password' => $request->input('credentials-password')])) {
return Redirect::back()->withMessage('Failed Authentication')->withColor('danger');
}
else {
$user = Auth::user();
$user->save();
return Redirect::to('/home');
}
}
}

I believe the issue you are seeing is that Laravel treats 'credentials.username' as a "Nested Attribute". For instance if you are rendering a view 'layout.head' it will automatically look for the file head.blade.php inside the layout folder.
I think in this case it's assuming you are passing it an array like:
<input id="credentials-email" name="credentials[username]" tabindex="2" type="text">
<input id="credentials-password" name="credentials[password]" tabindex="3" type="password">
Have you tried:
<input id="credentials-email" name="credentials_username" tabindex="2" type="text">
<input id="credentials-password" name="credentials_password" tabindex="3" type="password">
There is a very brief mention of nested attributes on the Laravel validation documentation page: https://laravel.com/docs/5.4/validation

Don't use dot inside name input.
Replace credentials.username by credentials_username.
Or even better, just username.

In laravel validation dot(.) Represent array of items. In this case you need to modify your input field name like this <input name="credentials[username]"> <input name="credentials[password]">

Related

How to fix edit interface getting only first word of a column in table in Laravel 5.7?

My edit interface edit.blade.php only gets the first word of the name from my database, this is what the index.blade.php looks like
and when i click on the edit icon of the 3rd line it leads me to edit.blade.php which gives me this
"Nom d'établissement" textfield only gets the first word from the database
Everything looks fine in the database:
this is my edit.blade.php form:
<form method="post" action="{{ route('etablissements.update', $etablissement->id) }}">
#method('PATCH')
#csrf
<div class="col-5">
<div class="form-group">
<label for="nom">Nom Etablissement :</label>
<input type="text" class="form-control" name="nom" value={{ $etablissement->nom }} />
</div>
<div class="form-group">
<label for="price">E-Mail :</label>
<input type="text" class="form-control" name="email" value={{ $etablissement->email }} />
</div>
<div class="form-group">
<label for="quantity">Telephone:</label>
<input type="text" class="form-control" name="telephone" value={{ $etablissement->telephone }} />
</div>
</div>
<button type="submit" class="btn btn-primary">Confirmer</button>
</form>
this is edit function in the controller:
public function edit($id)
{
$etablissement = Etablissement::find($id);
return view('etablissements.edit', compact('etablissement'));
}
and this is update function in the controller:
public function update(Request $request, $id)
{
$request->validate([
'nom'=>'required',
'email'=> 'required',
'telephone' => 'required|numeric'
]);
$etablissement = Etablissement::find($id);
$etablissement->nom = $request->get('nom');
$etablissement->email = $request->get('email');
$etablissement->telephone = $request->get('telephone');
$etablissement->save();
return redirect('/etablissements')->with('success', 'Utilisateur édité');
}
Quote the value attribute.
<input type="text" class="form-control" name="nom" value="{{ $etablissement->nom }}" />
Without quotes, the second word in$etablissement->nom is interpreted as another attribute rather than part of the value of the value attribute.
The email and telephone values are showing up correctly because there are no spaces, but you should quote those as well just in case.

$this->validate causes "Failed to load response data"

I have the following external form:
<form method="POST" action="http://infused.local/leads/post">
<div class="form-group">
<label>first_name</label>
<input type="text" name="first_name" class="form-control">
</div>
<div class="form-group">
<label>last_name</label>
<input type="text" name="last_name" class="form-control">
</div>
<div class="form-group">
<label>email</label>
<input type="text" name="email" class="form-control">
</div>
<div class="form-group">
<label>postal_code</label>
<input type="text" name="postal_code" class="form-control">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
Which points to the following controller method:
public function post()
{
$this->validate(request(), [
'email' => 'required|email',
]);
echo 'hello';
}
Via this route:
Route::post('leads/post', 'LeadController#post');
I've disabled CSRF protection for the form route.
When I submit the form, I get "Failed to load response data" from
Chrome.
When I remove the $this->validate call, I get "hello".
Why is this not working?
You need to return JSON and you can use the helper function:
response()->json([
'message' => 'hello'
]);
Then in your JS:
console.log(response.message);
OK apparently the validate method just redirects you back to the previous page if there are errors, unless the response is expected to be JSON. Ended up using the Validator facade instead.
Edit: Thanks for the downvotes. This website is cancer.

Submit form with php WordPress

Hello I work with WordPress , and I create a custom Modal with form to add a Group called 'abonnement' I added the table in Database and this is my issue:
Form
<form name="abonnementCreate" action="create.php" method="POST">
<div class="form-group">
<label>Nom de l'abonnemnt</label>
<input type="text" class="form-control" id="nomA" name="nomA">
</div>
<div class="form-group">
<label>Durée</label>
<input type="number" class="form-control" id="dureeA">
<small id="dureeHelp" class="form-text text-muted">En jours</small>
</div>
<div class="form-group">
<label>Nombre de produit autorisé</label>
<input type="number" class="form-control" id="nbProdA">
</div>
<div class="form-group">
<label>Frais d'abonnement (€)</label>
<input type="number" class="form-control" id="fraisA">
</div>
<button type="submit" name="create" class="btn btn-primary">Valider</button>
</form>
and this is the action of the creation :
create.php
<?php
include'admin.php';
global $wpdb;
if(isset( $_POST["create"]))
{
$wpdb->insert('abonnement', array(
'nom' => '.$_POST["nomA"].',
'duree' => '.$_POST["dureeA"].',
'nbProd' => '.$_POST["nbProdA"].',
'frais' => '.$_POST["fraisA"].'
));
}
?>
I have $_POST["create"] and the condition is true but $_POST[" any field"] is always null,
Someone please can help me ?
you can never catch the post value from id of the form input field.only way that you can get value using $_POST[] is using the "name" attribute of the input field.
<input type="text" class="form-control" id="nomA" name="name1">
this input field value can be accessed from form action php file using $_POST["name1"]

PHP not getting any response from the form on the previous page

I have the file "1.php" and the file "2.php" ...
In "1.php", there's a HTML form:
<form action="2.php" method="post">
<div class="form-group">
<label for="db_username">Field 1:</label>
<input type="text" class="form-control" id="db_username">
</div>
<div class="form-group">
<label for="db_password">Field 2:</label>
<input type="password" class="form-control" id="db_password">
</div>
<div class="form-group">
<label for="db_passphrase">Field 3:</label>
<input type="text" class="form-control" id="db_passphrase">
</div>
<button type="submit" class="btn btn-default">Next</button>
</form>
While in "2.php", the action must be applied in PHP .. like that:
<?php
// Process of Step "1"
$db_username = $_POST['db_username'];
$db_password = $_POST['db_password'];
$db_passphrase = $_POST['db_passphrase'];
if( !isset($db_username) || !isset($db_password) || !isset($db_passphrase) ) {
header("Location: 1.php?error=1");
die();
}
?>
However .. the values of $_POST['db_username'], $_POST['db_password'] and $_POST['db_passphrase'] is empty...
You need to use the name attribute to later access the $_POST data, as in:
<input type="text" class="form-control" id="db_username" name="db_username">
You need to give the inputs a name and acces that value through $_POST['name'].
id is not relevant for php form handling
You have to give name for the textfields like this
<input type="text" class="form-control" id="db_username" name="db_username">
Put name instead of id. it should work

Multiple form authentication in the same domain

I have a website with two form authentication in different pages, have different input name and link to different pages . The problem is that when I save my authentication to a browser (chrome) of a form , the browser fill in the fields with the same data in the other form . How is it possible?
First form
<form action="" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label for="exampleInputEmail1">Email</label>
<input type="email" name="private_email" class="form-control" id="email1" value="" placeholder="Enter email" required>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" name="private_password" value="" id="password1" placeholder="Password" required>
</div>
<input type="submit" name="login" class="btn btn-default" value="Login">
</form>
Second Form (It is a form of a cms)
<form action="http://escuolainsieme.it/shop/login" method="post" id="login_form" class="box">
<h3 class="page-subheading">Sei già registrato?</h3>
<div class="form_content clearfix">
<div class="form-group form-ok">
<label for="email">Indirizzo email</label>
<input class="is_required validate account_input form-control" data-validate="isEmail" type="text" id="email" name="email" value="">
</div>
<div class="form-group">
<label for="passwd">Password</label>
<span><input class="is_required validate account_input form-control" type="password" data-validate="isPasswd" id="passwd" name="passwd" value=""></span>
</div>
<p class="lost_password form-group">Hai dimenticato la password?</p>
<p class="submit">
<input type="hidden" class="hidden" name="back" value="my-account"> <button type="submit" id="SubmitLogin" name="SubmitLogin" class="button btn btn-default button-medium">
<span>
<i class="icon-lock left"></i>
Entra
</span>
</button>
</p>
</div>
</form>
Login.php
<?php session_start(); // Starting Session
$error = ''; // Variable To Store Error Message
if (isset($_POST['private_login'])) {
if (empty($_POST['private_email']) || empty($_POST['private_password'])) {
$error = "<div class='alert alert-danger'>Compila tutti i campi</div>";
} else {
$email = mysqli_real_escape_string(conn(), $_POST['private_email']);
$password = mysqli_real_escape_string(conn(), $_POST['private_password']);
$cls_utente = new utente();
if ($cls_utente->check_user($email, $password) == 1) {
$_SESSION['login_user'] = $email; // Initializing Session
$_SESSION['is_logged'] = true;
} else {
$error .= "<div class='alert alert-danger'>Email o password errati</div>";
}
}}?>
You can try using autocomplete="false" or autocomplete="off" to disable it, not sure if it will work but give it a try.
As far as i am concerned it's not possible to make the browser 'realize' that they are different forms and they should not be auto-filled with the same data.
Have a look at these 2 answers, answer1 answer2 for more information how browser detects the forms.
You must add another one column in your user table, example if you add type column the value set default super admin,moderator, user. Now you can check the login authentication with this column.If user-name and password and type is equal redirect to particular page.so you can redirect different page depends upon the user type..

Categories