Skip to content

Commit

Permalink
Merge pull request #3 from IsaacHatilima/album
Browse files Browse the repository at this point in the history
Validation refactoring
  • Loading branch information
IsaacHatilima authored Oct 9, 2024
2 parents c4e12be + c726c40 commit 473e75e
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 57 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/laravel.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
name: Laravel

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

Expand Down
5 changes: 1 addition & 4 deletions app/Http/Requests/ConfirmPasswordRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ public function rules(): array

public function messages(): array
{
return [
'required' => 'Password is required.',
'current_password' => 'Password is incorrect.',
];
return ConfirmPasswordValidation::messages();
}
}
20 changes: 7 additions & 13 deletions app/Http/Requests/PasswordUpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,12 @@ public function rules(): array

public function messages(): array
{
return [
'current_password.required' => 'Current password is required.',
'current_password.current_password' => 'Your current password is incorrect.',

'password.required' => 'New password is required.',
'password.regex' => 'New password must contain at least one upper and lower case letter, one number, and one special character (!, $, #, %, @, ?).',
'password.min' => 'New password must be at least 8 characters long.',
'password.required_with' => 'New password confirmation is required.',
'password.same' => 'Password and confirmation must match.',

'password_confirmation.required' => 'Please confirm your new password.',
'password_confirmation.same' => 'Password confirmation does not match the new password.',
];
return array_merge(
NewPasswordValidation::messages(),
[
'current_password.required' => 'Current password is required.',
'current_password.current_password' => 'Your current password is incorrect.',
]
);
}
}
23 changes: 18 additions & 5 deletions app/Http/Requests/ProfileUpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Http\Requests;

use App\Validations\NewEmailValidation;
use App\Validations\RequiredInputValidation;
use App\Validations\UpdateWithEmailValidation;
use Illuminate\Contracts\Validation\Rule;
Expand All @@ -18,13 +17,27 @@ class ProfileUpdateRequest extends FormRequest
public function rules(): array
{
return array_merge(
NewEmailValidation::rules(),
RequiredInputValidation::rules('first_name'),
RequiredInputValidation::rules('last_name'),
(new RequiredInputValidation('first_name'))->rules(),
(new RequiredInputValidation('last_name'))->rules(),
UpdateWithEmailValidation::rules($this->user()->id),
[
'date_of_birth' => ['nullable', 'date', 'date_format:Y-m-d'],
'gender' => ['nullable', 'in:male,female'],
'gender' => ['nullable', 'in:male,female, diverse'],
]
);
}

public function messages(): array
{
return array_merge(
(new RequiredInputValidation('first_name'))->messages(),
(new RequiredInputValidation('last_name'))->messages(),
[
'date_of_birth.date' => 'Value must be a valid date.',
'date_of_birth.regex' => 'Invalid date format.',

'gender.in' => 'Invalid gender.',

]
);
}
Expand Down
43 changes: 18 additions & 25 deletions app/Http/Requests/RegisterRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,28 @@ public function rules(): array
return array_merge(
NewEmailValidation::rules(),
NewPasswordValidation::rules(),
RequiredInputValidation::rules('first_name'),
RequiredInputValidation::rules('last_name'),
(new RequiredInputValidation('first_name'))->rules(),
(new RequiredInputValidation('last_name'))->rules()
);
}

public function messages(): array
{
return [
'email.required' => 'Email is required.',
'email.string' => 'Email MUST be a string.',
'email.lowercase' => 'Email MUST be lowercase letters.',
'email.email' => 'Invalid email format.',
'email.max' => 'Email is too long.',
'email.unique' => 'Email is already in use.',


'password.required' => 'Password is required.',
'password.regex' => 'Password must contain at least one upper and lower case letter, one number, and one special character (!, $, #, %, @, ?).',
'password.min' => 'Password must be at least 8 characters long.',
'password.required_with' => 'Password confirmation is required.',
'password.same' => 'Password and confirmation must match.',

'password_confirmation.required' => 'Please confirm your password.',
'password_confirmation.same' => 'Password confirmation does not match the new password.',

'first_name.required' => 'First Name is required.',
'first_name.string' => 'First Name MUST be a string.',
'last_name.required' => 'Last Name is required.',
'last_name.string' => 'Last Name MUST be a string.',
];
return array_merge(
NewEmailValidation::messages(),
(new RequiredInputValidation('first_name'))->messages(),
(new RequiredInputValidation('last_name'))->messages(),
[
'password.required' => 'Password is required.',
'password.regex' => 'Password must contain at least one upper and lower case letter, one number, and one special character (!, $, #, %, @, ?).',
'password.min' => 'Password must be at least 8 characters long.',
'password.required_with' => 'Password confirmation is required.',
'password.same' => 'Password and confirmation must match.',

'password_confirmation.required' => 'Please confirm your password.',
'password_confirmation.same' => 'Password confirmation does not match the new password.',

]
);
}
}
8 changes: 8 additions & 0 deletions app/Validations/ConfirmPasswordValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,12 @@ public static function rules(): array
]
];
}

public static function messages(): array
{
return [
'password.required' => 'Password is required.',
'password.current_password' => 'Password is not correct.'
];
}
}
18 changes: 15 additions & 3 deletions app/Validations/NewEmailValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,22 @@ public static function rules(): array
'required',
'string',
'lowercase',
'email:rfc,dns',
'max:255',
'unique:'.User::class
'email:rfc' . (!app()->environment('production') ? '' : ',dns'),
'max:50',
'unique:' . User::class,
]
];
}

public static function messages(): array
{
return [
'email.required' => 'Email is required.',
'email.string' => 'Email MUST be a string.',
'email.lowercase' => 'Email MUST be lowercase letters.',
'email.email' => 'Invalid email format.',
'email.max' => 'Email is too long.',
'email.unique' => 'Email is already in use.',
];
}
}
14 changes: 14 additions & 0 deletions app/Validations/NewPasswordValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,18 @@ public static function rules(): array
]
];
}

public static function messages(): array
{
return [
'password.required' => 'New password is required.',
'password.regex' => 'New password must contain at least one upper and lower case letter, one number, and one special character (!, $, #, %, @, ?).',
'password.min' => 'New password must be at least 8 characters long.',
'password.required_with' => 'New password confirmation is required.',
'password.same' => 'Password and confirmation must match.',

'password_confirmation.required' => 'Please confirm your new password.',
'password_confirmation.same' => 'Password confirmation does not match the new password.',
];
}
}
21 changes: 19 additions & 2 deletions app/Validations/RequiredInputValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,31 @@

class RequiredInputValidation
{
public static function rules(string $attribute): array
private string $attribute;

public function __construct(string $attribute)
{
$this->attribute = $attribute;
}

public function rules(): array
{
return [
$attribute => [
$this->attribute => [
'required',
'string',
'min:2'
]
];
}

public function messages(): array
{
$formattedAttribute = ucwords(str_replace('_', ' ', $this->attribute));

return [
$this->attribute.'.required' => $formattedAttribute.' is required.',
$this->attribute.'.string' => $formattedAttribute.' MUST be a string.',
];
}
}
18 changes: 17 additions & 1 deletion app/Validations/UpdateWithEmailValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,25 @@ public static function rules(int $userId): array
{
return [
'email' => [
'required', 'string', 'lowercase', 'email:rfc,dns', 'max:255',
'required',
'string',
'lowercase',
'email:rfc' . (!app()->environment('production') ? '' : ',dns'),
'max:50',
Rule::unique(User::class)->ignore($userId)
]
];
}

public static function messages(): array
{
return [
'email.required' => 'Email is required.',
'email.string' => 'Email MUST be a string.',
'email.lowercase' => 'Email MUST be lowercase letters.',
'email.email' => 'Invalid email format.',
'email.max' => 'Email is too long.',
'email.unique' => 'Email is already in use.',
];
}
}
2 changes: 1 addition & 1 deletion resources/js/Pages/Auth/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function handleGoogleLogin()
:class="{ 'border-red-500': form.errors.email }"
v-model="form.email"
autofocus
autocomplete="username"/>
autocomplete="email"/>

<InputError class="mt-2" :message="form.errors.email" />
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,14 @@ function submitForm() {
<RadioGroupItem id="female" value="female"/>
<Label for="female">Female</Label>
</div>
<div class="flex items-center space-x-2">
<RadioGroupItem id="diverse" value="diverse"/>
<Label for="diverse">Diverse</Label>
</div>
</RadioGroup>
</div>

<div v-if="mustVerifyEmail && user.email_verified_at === null">
<div v-if="mustVerifyEmail && user.email_verified_at == null">
<p class="text-sm mt-2 text-gray-800">
Your email address is unverified.
<Link
Expand Down

0 comments on commit 473e75e

Please sign in to comment.