File: /mnt/data/ghayatcom/ghayatcom-api/app/Http/Requests/ChangePasswordRequest.php
<?php
namespace App\Http\Requests;
use App\Library\CustomFailedValidation;
use Illuminate\Support\Facades\Hash;
class ChangePasswordRequest extends CustomFailedValidation
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'old_password' => [
'required',
'string',
function ($attribute, $value, $fail) {
// Ensure current password is the same
if (!Hash::check($value, $this->user()->password)) {
$fail(__('digimed_validation.form_validation_error.change_password_incorrect_current_password'));
}
},
],
'new_password' => [
'required',
'string',
'min:8',
'max:20',
'confirmed',
function ($attribute, $value, $fail) {
// Ensure new password is not the same as the old one
if (Hash::check($value, $this->user()->password)) {
$fail(__('digimed_validation.form_validation_error.change_password_old_and_new_password'));
}
// Ensure the password contains at least one special character
if (!preg_match('/^[a-zA-Z0-9!@#$%^&*]+$/', $value)) {
$fail(__('digimed_validation.form_validation_error.change_password_regex'));
}
},
],
];
}
public function messages()
{
return [
'old_password.required' => __('digimed_validation.form_validation_error.change_password_old_password_required'),
'new_password.required' => __('digimed_validation.form_validation_error.change_password_new_password_required'),
'new_password.min' => __('digimed_validation.form_validation_error.change_password_new_password_min'),
'new_password.max' => __('digimed_validation.form_validation_error.change_password_new_password_max'),
'new_password.confirmed' => __('digimed_validation.form_validation_error.change_password_new_password_confirmed'),
];
}
}