Skip to content

Commit

Permalink
Complete tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
juancorax committed Dec 23, 2024
1 parent 8f62e93 commit 516a28a
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 5 deletions.
5 changes: 3 additions & 2 deletions app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ class PostController extends Controller
{
public function store(Request $request)
{
$request->validate(
$request->validate([
// ... TASK: write validation here so that "title" field
// would be required and unique in the "posts" DB table
);
'title' => 'required|unique:posts',
]);

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

auth()->user()->update($request->profile ?? []);
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function update(User $user, UpdateUserRequest $request)
{
// 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->safe()->except('is_admin'));

return 'Success';
}
Expand Down
14 changes: 13 additions & 1 deletion app/Http/Requests/StoreBuildingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,19 @@ public function authorize()
public function rules()
{
return [
'name' => 'required'
'name' => 'required',
];
}

/**
* Get the error messages for the defined validation rules.
*
* @return array<string, string>
*/
public function messages(): array
{
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');
}
}
}
4 changes: 4 additions & 0 deletions resources/views/products/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
{{-- @directive --}}
{{-- {{ $message }} --}}
{{-- @endDirective --}}
@error('name')
<div>{{ $message }}</div>
@enderror

<br /><br />
<button type="submit">Save</button>
</form>
11 changes: 11 additions & 0 deletions resources/views/projects/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,24 @@

<form method="POST" action="{{ route('projects.store') }}">
@csrf

Title:
<br />
<input type="text" name="title" />

<br /><br />

Description:
<br />
<input type="text" name="description" />
<br /><br />
<button type="submit">Save</button>

@if ($errors->any())
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
@endif
</form>
5 changes: 4 additions & 1 deletion resources/views/teams/create.blade.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<form method="POST" action="{{ route('teams.store') }}">
@csrf

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>

0 comments on commit 516a28a

Please sign in to comment.