1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Requests; |
4
|
|
|
|
5
|
|
|
use App\Http\Models\Administrators; |
6
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
7
|
|
|
use Illuminate\Support\Facades\Auth; |
8
|
|
|
|
9
|
|
|
use Illuminate\Support\Facades\Request; |
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class AdministratorsRequest |
13
|
|
|
* Classe qui modélisera le formulaire d'administrateurs |
14
|
|
|
* @package App\Http\Requests |
15
|
|
|
*/ |
16
|
|
|
class AdministratorsRequest extends FormRequest |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Retourne un tableau de validation par champ |
22
|
|
|
* @return array |
23
|
|
|
*/ |
24
|
|
|
public function rules() |
25
|
|
|
{ |
26
|
|
|
// get id since router |
27
|
|
|
$id = $this->route('id'); |
28
|
|
|
|
29
|
|
|
if($id == null) { |
30
|
|
|
return [ |
31
|
|
|
'firstname' => 'required|max:255', |
32
|
|
|
'lastname' => 'required|max:255', |
33
|
|
|
'description' => 'required|min:10', |
34
|
|
|
'email' => 'required|email|max:255|unique:administrators', |
35
|
|
|
'password' => 'required|confirmed|min:6', |
36
|
|
|
'image' => 'required|image', |
37
|
|
|
]; |
38
|
|
|
}else{ |
39
|
|
|
return [ |
40
|
|
|
'firstname' => 'required|max:255', |
41
|
|
|
'lastname' => 'required|max:255', |
42
|
|
|
'description' => 'required|min:10', |
43
|
|
|
'email' => 'required|email|max:255|unique:administrators,email,' . $id, |
44
|
|
|
'password' => 'confirmed|min:6', |
45
|
|
|
'image' => 'image', |
46
|
|
|
]; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Customisation des messages par champs |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
public function messages() |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
return [ |
57
|
|
|
'title.required' => 'Un titre pour le film!', |
58
|
|
|
'required' => ':attribute est obligatoire', |
59
|
|
|
'min' => 'Ce champ doit faire plus de :min caractères', |
60
|
|
|
'max' => 'Ce champ doit faire moins de :max caractères', |
61
|
|
|
'integer' => 'Ce champ doit être un chiffre', |
62
|
|
|
'regex' => ':attribute a un mauvais format', |
63
|
|
|
'date_format' => 'Le format de date doit etre valide', |
64
|
|
|
'image' => "Le format de l'image est invalide", |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Autoriser l'accès de mon formulaire |
71
|
|
|
* pour tout utilisateur |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
public function authorize() |
75
|
|
|
{ |
76
|
|
|
return true; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: