Laravel request validation on array - laravel

Lets say I submit this form:
<form>
<input type="text" name="emails[]">
<input type="text" name="emails[]">
<input type="text" name="emails[]">
<input type="text" name="emails[]">
<input type="text" name="emails[]">
</form>
How do I then validate that at least one (anyone) of the $request->emails[] is filled?
I have tried this - however it does not work:
$request->validate([
'emails' => 'array|min:1',
'emails.*' => 'nullable|email',
]);
Laravel 7

Try this
$request->validate([
'emails' => 'array|required',
'emails.*' => 'email',
]);
Cordially

To meet your requirement, you may need custom rule
First create a rule, php artisan make:rule YourRuleName
inside YourRuleName.php
public function passes($attribute, $value)
{
foreach(request()->{$attribute} as $email) {
// if not null, then just return true.
// its assume in arrays some item have values
if ($email) { // use your own LOGIC here
return true;
}
}
return false;
}
Then,
$request->validate([
'emails' => [new YourRuleName],
]);

Related

delete if password is correct

i need to create a condition that clears the record but with a password.
if the password is correct execute the delete();
controller:
public function eliminar($id){
$registros = \App\Models\Registro::findOrFail($id);
$registros->delete();
return redirect('sistema')->with('mensaje', 'Registro Borrado con exito');
}
public function borrar($id){
// return $request->all();
$data = [
'category_name' => 'datatable',
'page_name' => 'multiple_tables',
'has_scrollspy' => 0,
'scrollspy_offset' => '',
'fechax' => Carbon::now(),
'borrar' => \App\Models\Registro::findOrFail($id),
'password' => 'PASSCODE',
];
return view('borrar')->with($data);
}
blade.php:
<h1>Do you want to delete the record?</h1>
<form action="{{ route('eliminar', $borrar) }}" class="d-inline" method="POST">
#method('DELETE')
#csrf
<button type="submit" class="btn btn-danger btn-sm">DELETE</button>
<div class="form-group col-md-6">
<label for="telefono">Password</label>
<input name="password" type="password" class="form-control" id="telefono" required>
</div>
</form>
the password is obtained statically
How can I make it delete without the password is identical?
help please
If the password is saved statically, inside in a variable, the following should do the job for you.
routes/web.php
Route::delete('/path/here', 'SomeController#destroy');
SomeController.php
public function destroy($id)
{
$model = YourModel::find($id);
if (! $model) {
session()->flash('error_message', 'Model not found with the given id: ', . $id);
return back();
}
// $password is the password that you have saved somewhere
if (request()->password_field_value == $password) {
$model->delete();
session()->flash('success_message', 'Model deleted successfully.');
return back();
}
session()->flash('error_message', 'Invalid password. Try again');
return back();
}

Undefined array key "password" in LoginController (Laravel 8)

I am trying to customize my login but it throws me the error that appears in the title.
This is the function in the LoginController:
public function login(Request $request) {
$request->validate([
'username' => 'required|string|email',
'password' => 'required|string',
'remember' => 'boolean',
]);
#:: Undefined array key "password"
if ($this->guard()->attempt(['a_username' => $request->username, 'a_password' => $request->password], $request->has('remember'))) {
return $this->sendLoginResponse($request);
}
return $this->sendFailedLoginResponse($request);
}
The question why I use these custom columns and not the ones that come with laravel by default. Is that I have 2 tables that are used to authenticate.
In my database I have the columns: a_username (it is the email)
a_password (is the password)
This is the login form:
<form class="js-validation-signin" action="{{ route('login') }}" method="POST">
#csrf
<div class="form-group">
<input type="email" class="form-control form-control-lg form-control-alt py-4" name="username" placeholder="Username" value="{{ old('username') }}">
</div>
<div class="form-group">
<input type="password" class="form-control form-control-lg form-control-alt py-4" name="password" placeholder="Password">
</div>
</form>
Thank you for your patience as I have to translate this problem from Spanish to English. :)
Eloquent user provider contain this code
public function validateCredentials(UserContract $user, array $credentials)
{
$plain = $credentials['password'];
return $this->hasher->check($plain, $user->getAuthPassword());
}
So to override default functionality you need:
In your user model add (override) method getAuthPassword
public function getAuthPassword()
{
return $this->a_password;
}
Update your code (change a_password to password):
if ($this->guard()->attempt(['a_username' => $request->username, 'password' => $request->password], $request->has('remember'))) {
return $this->sendLoginResponse($request);
}

Laravel use sync on a many to many that includes a multiple extra fields

I'm trying to use sync on a many to many that includes a status and a comment. I can sync the applications without status and comment just fine.
NewUserAccount Model
public function applications()
{
return $this->belongsToMany('App\Application', 'new_user_account_applications', 'new_user_id')->withPivot('application_comment', 'status');
}
Application Model
public function newUserAccounts()
{
return $this->belongsToMany('App\NewUserAccount', 'new_user_accounts_applications', 'new_user_id')->withPivot('application_comment', 'status');
}
My NewUserAccountController
public function store(StoreRequest $request)
{
$userAccount = NewUserAccount::create(array_merge(
$request->all(),
['submitted_by' => $requester->id],
['start_date' => Carbon::parse($request->input('start_date'))],
['account_expires' => $request->accountExpires('newAccountExpireDate')],
['company_id' => $requester->company_id],
['username' => $request->manuallyAssignId()]
));
// Here I sync applications and include application comment and status
$userAccount->applications()->sync($request->applications, ['application_comment' => $request->application_comment, 'status' => 0]);
....
}
My pivot showing status and comment correctly
My form. Here is where I'm not sure how to handle the comment and get it to save with each application pivot record.
#foreach($applications as $application)
<label class="k-checkbox">
<input value="{{ $application->id }}" name="applications[]" type="checkbox">{{ $application->application_name }} <span></span>
</label>
<div class="form-group col-lg-4 mb-3">
<label>Comments</label>
<textarea name="application_comment[]" class="form-control" rows="2"></textarea>
</div>
#endforeach
First, you need to set the correct index for the application_comment attribute in your textarea. It's needed to correctly determine the comment for each application.
#foreach($applications as $application)
...
<textarea name="application_comment[{{ $application->id }}]" class="form-control" rows="2"></textarea>
...
#endforeach
Then, you just need to format your data to:
$userAccount->applications()->sync([
application_id_1 => ['application_comment' => 'comment for application_id 1'],
application_id_2 => ['application_comment' => 'comment for application_id 2'],
...
]);
So, here it is
$applications = collect($request->applications)->mapWithKeys(function ($appId) use ($request) {
return [$appId => [
'application_comment' => $request->input('application_comment')[$appId],
'status' => 0,
]];
});
$userAccount->applications()->sync($applications);

Laravel multiple delete checkbox?

I want to delete only checked tasks. At the moment I have this:
<form method="POST" action="/destroy">
#foreach($tasks as $t)
<label>
<input type="checkbox" name="checked[]" value="$t->id">
</label>
#endforeach
<button type="submit">Submit!</button>
</form>
This is my Controller
public function destroy(Request $request)
{
$this->validate($request, [
'checked' => 'required',
]);
$checked = $request->input('checked');
Task::destroy($checked);
}
And this is my route
Route::post('/destroy', [
'uses' => 'Controller#destroy',
]);
I don't get no error but the system does not work
I fixed the problem! Thanks for your support!
The problem was that my id variable was a hash.

How to use a translation of the label to display an error, instead of the label's name, when registering a user?

When registering a new user, I use a RegisterFormRequest from Laravel.
How can I use the label name translations to display correctly the error message ?
Here is my html code,
<div>
<label for="password">
{{ Lang::get('form.password') }} *
</label>
<input name="password" type="password" required>
</div>
<div>
<label for="password_confirmation">
{{ Lang::get('form.password.confirm') }} *
</label>
<input name="password_confirmation" type="password" class="form-control" required>
</div>
Here is what is displayed. Note that the input field's "password" is used as is, but not any translations.
in the Http\Controllers\YourController.php
public function postRegister(RegisterFormRequest $request)
{
$validator = $this->registrar->validator($request->all());
$validator = $validator->setAttributeNames( [
'password' => 'My Password Name'
] );
will do the trick.
As #ali as implied, it might be better to put these values into your request class (it is where you can put the specific rules, so why not the specific label translation?).
in the Http\Controllers\YourController.php
public function postRegister(RegisterFormRequest $request)
{
$validator = $this->registrar->validator($request->all());
$validator = $validator->setAttributeNames( $request->messages() );
in the Http\Requests\RegisterFormRequest.php
public function messages()
{
return [
'password' => Lang::get('form.password'),
Laravel validation attributes "nice names"
And (as #mohamed-kawsara mentionned) in
resources/lang/de/validation.php
return [
...
"confirmed" => "The :attribute confirmation does not match.",
...
What you need is custom message in you request class.
Write this function in your request class.
public function messages()
{
return [
'password.required' => 'YOUR CUSTOM MESSAGE FOR PASSWORD REQUIREMENT'
];
}

Resources