Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My solution #289

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Http/Controllers/BuildingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function create()
// TASK: Customize the validation error message to say "Please enter the name"
public function store(StoreBuildingRequest $request)
{
Building::create($validator->validated());
Building::create($request->validated());

return 'Success';
}
Expand Down
3 changes: 0 additions & 3 deletions app/Http/Controllers/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

class ItemController extends Controller
{
// TASK: generate that StoreItemRequest class
// with authorization returning TRUE
// and validation rules: name is required, and description is required
public function store(StoreItemRequest $request)
{
Item::create($request->validated());
Expand Down
7 changes: 3 additions & 4 deletions app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ class PostController extends Controller
{
public function store(Request $request)
{
$request->validate(
// ... TASK: write validation here so that "title" field
// would be required and unique in the "posts" DB table
);
$request->validate([
'title' => 'required|unique:posts'
]);

// Saving the post
Post::create(['title' => $request->title]);
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public function update(Request $request)
// <input name="profile[name]" ... />
// <input name="profile[email]" ... />
// Write validation rules, so both name and email are required
'profile.name' => 'required',
'profile.email' => 'required'
]);

auth()->user()->update($request->profile ?? []);
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ class UserController extends Controller
{
public function update(User $user, UpdateUserRequest $request)
{
// dd($request->all());
// TASK: change this line to not allow is_admin field to be updated
// Update only the fields that are validated in UpdateUserRequest
$user->update($request->all());
$user->update($request->validated());

return 'Success';
}
Expand Down
6 changes: 6 additions & 0 deletions app/Http/Requests/StoreBuildingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ public function rules()
'name' => 'required'
];
}
public function messages()
{
return [
'name.required' => 'Please enter the name'
];
}
}
29 changes: 29 additions & 0 deletions app/Http/Requests/StoreItemRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreItemRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'name' => 'required',
'description' => 'required'
];
}
}
21 changes: 21 additions & 0 deletions app/Rules/Uppercase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class Uppercase implements ValidationRule
{
/**
* Run the validation rule.
*
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (ucfirst($value) !== $value) {
$fail('The :attribute does not start with an uppercased letter');
}
}
}
6 changes: 6 additions & 0 deletions resources/views/products/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
<br />
<input type="text" name="name" />
<br />

@error('name')
<div>
<p>{{ $message }}</p>
</div>
@enderror
{{-- TASK: show the validation error for the specific "name" field --}}
{{-- using one Blade directive: pseudo-code below --}}
{{-- @directive --}}
Expand Down
11 changes: 10 additions & 1 deletion resources/views/projects/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@
{{-- TASK: add the validation errors here - with whatever HTML structure you want --}}
{{-- in case of title/description empty, visitor should see --}}
{{-- "The name field is required." and "The description field is required." --}}

@if ($errors->any){
<ul>
@foreach ($errors->all() as $error)
<li>
{{ $error }}
</li>
@endforeach
</ul>
}
@endif
<form method="POST" action="{{ route('projects.store') }}">
@csrf
Title:
Expand Down
2 changes: 1 addition & 1 deletion resources/views/teams/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Name:
<br />
{{-- TASK: change this field so it would contain old value after validation error --}}
<input type="text" name="name" />
<input type="text" name="name" value="{{ old('name') }}" />
<br /><br />
<button type="submit">Save</button>
</form>
Loading