Skip to content

Commit

Permalink
الاختبار الخاص بقواعد التحقق
Browse files Browse the repository at this point in the history
  • Loading branch information
hmdee committed Nov 30, 2024
1 parent 8f62e93 commit 1258bb2
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 8 deletions.
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'
]);

// Saving the post
Post::create(['title' => $request->title]);
Expand Down
3 changes: 3 additions & 0 deletions app/Http/Controllers/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class ProfileController extends Controller
public function update(Request $request)
{
$request->validate([
'profile.name' => 'required',
'profile.email' => 'required'

// TASK: imagine that in the Blade the fields are
// <input name="profile[name]" ... />
// <input name="profile[email]" ... />
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->except('is_admin'));

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' => 'Please enter the name'
];
}
}
55 changes: 55 additions & 0 deletions app/Http/Requests/StoreItemRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace App\Http\Requests;


use Illuminate\Validation\Rule;
use Illuminate\Foundation\Http\FormRequest;

class StoreItemRequest extends FormRequest
{
/**
* 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<string, mixed>
*/
public function rules()
{
return [

'name' => [
'required',
],
'description ' => [
'required',



],

'category_id' => [
'required', // التأكد من وجود category_id
'exists:categories,id' // التأكد من أن category_id يشير إلى فئة موجودة في جدول categories
],
];
}
public function messages()
{
return [
'title.required' => ' مطلوب حيا او ميتاا.',
'title.unique' => 'هذا العنوان موجود بالفعل.',
'text.required' => 'يجب كتابة نص البوست.',
'text.min' => 'يجب أن يكون النص مكونًا من 10 أحرف على الأقل.',
];
}
}
40 changes: 40 additions & 0 deletions app/Rules/Uppercase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class Uppercase implements Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
return ucfirst($value) === $value;
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The title does not start with an uppercased letter';
}
}
5 changes: 5 additions & 0 deletions resources/views/products/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
{{-- @directive --}}
{{-- {{ $message }} --}}
{{-- @endDirective --}}
@error('name')
<div class="error" style="color: red;">
{{ $message }}
</div>
@enderror
<br /><br />
<button type="submit">Save</button>
</form>
11 changes: 9 additions & 2 deletions resources/views/projects/create.blade.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
{{-- Form without any design --}}

{{-- 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())
<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') }}">
@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>

0 comments on commit 1258bb2

Please sign in to comment.