Laravel validation with exist function - validation

I have laravel change password code, with fields: oldpassword, newpasword, confirmedpassword
Now to validate the form the newpasword must be the same as confirmedpassword, and the old must be existed for this user in database
this is my view code:
<div class="form-group">
<label for="oldpsw">your current password</label>
<input type="password" class="form-control" id="oldpsw"
placeholder="your current password" name="oldpsw">
#if ($errors->has('oldpsw'))
<span class="help-block">
<strong>{{ $errors->first('oldpsw') }}</strong>
</span>
#endif
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password">new password</label>
<input type="password" class="form-control" id="password" placeholder="new
password" name="password" required>
#if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
<div class="form-group">
<label for="password_confirmation">Password Confirmation</label>
<input type="password" class="form-control" id="password_confirmation"
placeholder="Password Confirmation" name="password_confirmation" required>
</div>
my controller:
return Validator::make($data, ['oldpsw' => 'required|string','password' => 'required|string|confirmed','password_confirmation' => 'required|string',
'password' =>Rule::exists('accounts')->where(function ($query) { $query->where('student_no', Auth::user()->student_no);}), ]);
If I want to use |confirmed my field must be as the name of model fillable field, but if I want to check the db existence it requires the field also as the same column in db.
My Db column is:password, what can I do to solve this problem
Edit: I mean for the two cases both fields must to be named: password !

Related

form control error on form submission in laravel app

on form submission, for some fields form-control error is working on one text area it is not working, this is description area + in control error i want to include that submitter should fill min 350 words or more. here is exact from blade template
control error is working on this:
<div class="form-row mb-3">
<div class="col-md-4">
<label for="item_title" class="text-black">{{ __('backend.item.title') }}</label>
<input id="item_title" type="text" class="form-control #error('item_title') is-invalid #enderror" name="item_title" value="{{ old('item_title') ? old('item_title') : $item->item_title }}">
#error('item_title')
<span class="invalid-tooltip">
<strong>{{ $message }}</strong>
</span>
#enderror
control error is not working on this fill
<div class="form-row mb-3">
<div class="col-md-12">
<label for="item_description" class="text-black">{{ __('backend.item.description') }}</label>
<textarea class="form-control #error('item_description') is-invalid #enderror" id="item_description" rows="5" name="item_description">{{ old('item_description') ? old('item_description') : $item->item_description }}</textarea>
#error('item_description')
<span class="invalid-tooltip">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
i applied the control but its not working

Login form in Laravel is giving the error 419 Page Expired

It used to work somoothly, then I added Sociallite and then it some point it broke the default login with email and password.
Now every time I login with username and password I get the error "419 Page Expired" and it's literally diving me crazy because I have tried everything, with no luck.
View login.blade.php:
<form method="POST" action="{{ route('login') }}">
#csrf
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control #error('email') is-invalid #enderror" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus>
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control #error('password') is-invalid #enderror" name="password" required autocomplete="current-password">
#error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<div class="col-md-6 offset-md-4">
<div class="form-check">
<input class="form-check-input" type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}>
<label class="form-check-label" for="remember">
{{ __('Remember Me') }}
</label>
</div>
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-8 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Login') }}
</button>
#if (Route::has('password.request'))
<a class="btn btn-link" href="{{ route('password.request') }}">
{{ __('Forgot Your Password?') }}
</a>
#endif
</div>
</div>
</form>
Route web.php:
Route::post('login', [
'as' => '',
'uses' => 'Auth\LoginController#login'
]);
I searched the web and found some ppl talking about clearing cash, clearing cookies, checking cash and storage permissions, clearing SESSION_DOMAIN and I did all, but it's still the same.
Please anyone is still facing such issue in Larvel 7?

I am putting the correct password in laravel but it display error password does not match|Password and cpassword fields not match

Blade code is here:
<div class="">
<input class="input--style-1 text-success form-control pl-3" type="password" placeholder="Password" name="Password">
<span class="text-danger font-italic" >{{$errors->first('Password')}}</span>
</div><br>
<div class="">
<input class="input--style-1 text-success form-control pl-3" type="password" placeholder="Confirm_Password" name="C_Password">
<span class="text-danger font-italic" >{{$errors->first('C_Password')}}</span>
</div><br>
DonorRequest.php code is here:
public function rules()
{
return [
'Name'=>'required|min:2|max:20|alpha_dash',
'Blood_Group'=>'required',
'Email'=>'required|Email:rfc,dns',
'Contact'=>'required|numeric',
'Gender'=>'required',
'Location'=>'required',
'Password'=>'required|min:6',
'C_Password'=>'required|min:6|confirmed'
];
}
public function messages()
{
return[
'Location.required'=>'Kindly Search And Select Location From Map'
];
}
I am new on Laravel so i am not able to find this error. what can be problem in backend?
Since your password input name is Password the name of the confirmation input must be Password_confirmation
So change this:
<div class="">
<input class="input--style-1 text-success form-control pl-3" type="password" placeholder="Confirm_Password" name="C_Password">
<span class="text-danger font-italic" >{{$errors->first('C_Password')}}</span>
</div><br>
To this:
<div class="">
<input class="input--style-1 text-success form-control pl-3" type="password" placeholder="Confirm_Password" name="Password_confirmation">
<span class="text-danger font-italic" >{{$errors->first('C_Password')}}</span>
</div><br>
According to #miken32 validation on the confirmation field is not needed, so you can remove this line 'C_Password'=>'required|min:6|confirmed'.
See: https://laravel.com/docs/7.x/validation#rule-confirmed

Edited the out-the-box authentication form in Laravel, but it will not submit to the database now

I set up a new app in laravel and created the out of the box authentication with php artisan make:auth . I needed to add some additional fields to the forms and followed a tutorial (which I can't even find again) and re ran the migrations. The new fields went to the database and all looks good. However, when I try to register, the form does not submit to the database, it does not redirect, and many of the fields get cleared out.
I found one article on stack that mentioned my form view might be messed up with the register button so I've been making sure all my divs are closed and whatnot but it's not fixing it. I also thought the issue may be related to my radio buttons and check boxes so I removed those to test it without and still had the same issue.
my register controller:
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* #var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* 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, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'phone' => ['required', 'string', 'max:15'],
'program' => ['required'],
'disability'=>['string', 'max:255'],
'terms' => ['required'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return \App\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'phone' => $data['phone'],
'program' =>$data['program'],
'disability'=>$data['disability'],
'terms' => $data['terms'],
'password'=> Hash::make($data['password']),
]);
}
}
below is my form (sorry for sharing the whole thing)
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Register') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('register') }}">
#csrf
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control #error('name') is-invalid #enderror"
name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="email"
class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control #error('email') is-invalid #enderror"
name="email" value="{{ old('email') }}" required autocomplete="email">
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="phone number"
class="col-md-4 col-form-label text-md-right">{{ __('Phone Number') }}</label>
<div class="col-md-6">
<input id="phone number" type="text"
class="form-control #error('phone number') is-invalid #enderror"
name="phone number" value="{{ old('phone') }}" required autocomplete="phone"
autofocus>
#error('phone')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="program"
class="col-md-4 col-form-label text-md-right">{{ __('Program') }}</label>
<div class="col-md-6 ">
<div class="form-inline"><input id="veterans" type="radio" class="form-inline"
name="program" value="Veteran Program ($35.00/month)"
{{ (old('program')== 'veteran') ? 'checked':''}}>
Veteran Program ($35.00/month
</div>
<div class="form-inline"><input id="masters" type="radio" class="form-inline"
name="program" value="Masters Program ($50.00/month)"
{{ (old('program')== 'masters') ? 'checked':''}}>
Masters Program ($50.00/month)
</div>
<div class="form-inline"><input id="adaptive" type="radio" class="form-inline"
name="program" value="Non-Veteran Adaptive Program ($50.00/month)"
{{ (old('program')== 'adaptive') ? 'checked':''}}> Masters Adaptive Program
($50.00/month)
</div>
#error('program')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="type"
class="col-md-4 col-form-label text-md-right">{{ __('Disability Type (If applicable)') }}</label>
<div class="col-md-6">
<input id="disability" type="text" class="form-control" name="disability"
value="{{ old('disability') }}" autofocus>
</div>
</div>
<div class="form-group row">
<label for="waiver"
class="col-md-4 col-form-label text-md-right">{{ __('Terms and Conditions') }}</label>
<div class="col-md-6 ">
<div class="form-inline"><input id="waiver" type="checkbox" class="form-inline"
name="waiver" value="waiver" {{ (old('waiver')== '1') ? 'checked':''}}> By
checking this box, I agree to the ORCA terms & conditions. </div>
#error('waiver')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="password"
class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>
<div class="col-md-6">
<input id="password" type="password"
class="form-control #error('password') is-invalid #enderror" name="password"
required autocomplete="new-password">
#error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="password-confirm"
class="col-md-4 col-form-label text-md-right">{{ __('Confirm Password') }}</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control"
name="password_confirmation" required autocomplete="new-password">
</div>
</div>
<div class="form-group row">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Register') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
I expect it to create the account, login and redirect. Unfortunately, it's not throwing any error messages which is making it more difficult to troubleshoot.
I tried to comment but I can't yet, at first please check names of the phone and terms inputs, cause for the phone input you put "phone number" at name attribute, and for the terms input, you put "waiver" in name attribute,
can you please share the whole controller after
The disability field needed to be update to allow it to be blank. Added 'nullable' to the validator function on the register controller and it worked. Also need to update that column in the migration so the database doesn't error.

how should i give input box red colour in validation in laravel5.4

When user click on the submit button I want to give that text box color as red.
but Laravel comes up with the validation with message..
How can we do the validation as red color text box?
Here is my validation in controller:
protected function validator(array $data) {
return Validator::make($data, [
'first_name' => 'required|max:255',
'last_name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
'contact_no' => 'required|numeric',
]);
}
here is my view:
<div class="form-group{{ $errors->has('first_name') ? ' has-error' : '' }}">
<label for="name" class="col-md-4 control-label">First Name</label>
<div class="col-md-6">
<input id="first_name" type="text" class="form-control" name="first_name" value="{{ old('name') }}" placeholder="first name" required autofocus>
#if ($errors->has('first_name'))
<span class="help-block">
<strong>{{ $errors->first('first_name') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('last_name') ? ' has-error' : '' }}">
<label for="name" class="col-md-4 control-label">Last Name</label>
<div class="col-md-6">
<input id="last_name" type="text" class="form-control" name="last_name" value="{{ old('last_name') }}" placeholder="last name" required autofocus>
#if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<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="{{ old('email') }}" placeholder="email" required>
#if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
</div>
Can any one help me..please
Laravel will addclass has-error according to following line form-group{{ $errors->has('...') ? ' has-error' : '' }}, So you just need to add CSS style.
.has-error .form-control {
border-color: #a94442;
}

Resources