mirror of
https://github.com/10h30/Test-Laravel-Validation.git
synced 2026-06-05 15:07:56 +09:00
Complete all tasks
This commit is contained in:
@@ -9,14 +9,14 @@ class PostController extends Controller
|
|||||||
{
|
{
|
||||||
public function store(Request $request)
|
public function store(Request $request)
|
||||||
{
|
{
|
||||||
$request->validate(
|
$request->validate([
|
||||||
|
'title' => 'required|unique:posts'
|
||||||
// ... TASK: write validation here so that "title" field
|
// ... TASK: write validation here so that "title" field
|
||||||
// would be required and unique in the "posts" DB table
|
// would be required and unique in the "posts" DB table
|
||||||
);
|
]);
|
||||||
|
|
||||||
// Saving the post
|
// Saving the post
|
||||||
Post::create(['title' => $request->title]);
|
Post::create(['title' => $request->title]);
|
||||||
|
|
||||||
return 'Success';
|
return 'Success';
|
||||||
}
|
}}
|
||||||
}
|
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ class ProfileController extends Controller
|
|||||||
public function update(Request $request)
|
public function update(Request $request)
|
||||||
{
|
{
|
||||||
$request->validate([
|
$request->validate([
|
||||||
|
'profile.name' => 'required',
|
||||||
|
'profile.email' => 'required'
|
||||||
// TASK: imagine that in the Blade the fields are
|
// TASK: imagine that in the Blade the fields are
|
||||||
// <input name="profile[name]" ... />
|
// <input name="profile[name]" ... />
|
||||||
// <input name="profile[email]" ... />
|
// <input name="profile[email]" ... />
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ class UserController extends Controller
|
|||||||
{
|
{
|
||||||
// TASK: change this line to not allow is_admin field to be updated
|
// TASK: change this line to not allow is_admin field to be updated
|
||||||
// Update only the fields that are validated in UpdateUserRequest
|
// Update only the fields that are validated in UpdateUserRequest
|
||||||
$user->update($request->all());
|
$user->update($request->except(['is_admin']));
|
||||||
|
|
||||||
return 'Success';
|
return 'Success';
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,4 +30,13 @@ class StoreBuildingRequest extends FormRequest
|
|||||||
'name' => 'required'
|
'name' => 'required'
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function messages(): array
|
||||||
|
|
||||||
|
{
|
||||||
|
|
||||||
|
return [
|
||||||
|
'name.required' => 'Please enter the name'
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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'
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
<?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 title does not start with an uppercased letter');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Generated
+1459
-1152
File diff suppressed because it is too large
Load Diff
@@ -9,6 +9,10 @@
|
|||||||
{{-- @directive --}}
|
{{-- @directive --}}
|
||||||
{{-- {{ $message }} --}}
|
{{-- {{ $message }} --}}
|
||||||
{{-- @endDirective --}}
|
{{-- @endDirective --}}
|
||||||
|
@error('name')
|
||||||
|
{{ $message }}
|
||||||
|
@enderror
|
||||||
|
|
||||||
<br /><br />
|
<br /><br />
|
||||||
<button type="submit">Save</button>
|
<button type="submit">Save</button>
|
||||||
</form>
|
</form>
|
||||||
|
|||||||
@@ -4,6 +4,18 @@
|
|||||||
{{-- in case of title/description empty, visitor should see --}}
|
{{-- in case of title/description empty, visitor should see --}}
|
||||||
{{-- "The name field is required." and "The description field is required." --}}
|
{{-- "The name field is required." and "The description field is required." --}}
|
||||||
|
|
||||||
|
@if ($errors->any())
|
||||||
|
|
||||||
|
<div class="alert alert-danger">
|
||||||
|
<ul>
|
||||||
|
@foreach ($errors->all() as $error )
|
||||||
|
<li>{{ $error }}</li>
|
||||||
|
@endforeach
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
@endif
|
||||||
|
|
||||||
<form method="POST" action="{{ route('projects.store') }}">
|
<form method="POST" action="{{ route('projects.store') }}">
|
||||||
@csrf
|
@csrf
|
||||||
Title:
|
Title:
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
Name:
|
Name:
|
||||||
<br />
|
<br />
|
||||||
{{-- TASK: change this field so it would contain old value after validation error --}}
|
{{-- 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 />
|
<br /><br />
|
||||||
<button type="submit">Save</button>
|
<button type="submit">Save</button>
|
||||||
</form>
|
</form>
|
||||||
|
|||||||
Reference in New Issue
Block a user