@@ -53,16 +53,60 @@ public function rules(): array
5353 ],
5454 'description ' => 'nullable|string|max:200 ' ,
5555 'comment_state ' => 'sometimes|string|in:0,4 ' ,
56- 'can_download ' => 'sometimes ' ,
57- 'is_sensitive ' => 'sometimes|boolean ' ,
56+ 'can_download ' => 'nullable|boolean ' ,
57+ 'can_comment ' => 'nullable|boolean ' ,
58+ 'can_duet ' => 'nullable|boolean ' ,
59+ 'can_stitch ' => 'nullable|boolean ' ,
60+ 'is_sensitive ' => 'nullable|boolean ' ,
5861 'alt_text ' => 'nullable|sometimes|string|max:2000 ' ,
59- 'contains_ai ' => 'sometimes |boolean ' ,
60- 'contains_ad ' => 'sometimes |boolean ' ,
62+ 'contains_ai ' => 'nullable |boolean ' ,
63+ 'contains_ad ' => 'nullable |boolean ' ,
6164 'lang ' => [
6265 'sometimes ' ,
6366 'string ' ,
6467 Rule::in (app (IntlService::class)->keys ()),
6568 ],
6669 ];
6770 }
71+
72+ /**
73+ * Prepare the data for validation.
74+ */
75+ protected function prepareForValidation (): void
76+ {
77+ $ fields = ['can_download ' , 'can_comment ' , 'can_duet ' , 'can_stitch ' , 'is_sensitive ' , 'contains_ai ' , 'contains_ad ' ];
78+ foreach ($ fields as $ key ) {
79+ if ($ this ->has ($ key )) {
80+ $ this ->merge ([
81+ $ key => $ this ->toBoolean ($ this ->input ($ key )),
82+ ]);
83+ }
84+ }
85+ }
86+
87+ /**
88+ * Converts a value to a boolean.
89+ * Handles 'true', 'false', 1, 0, '1', '0', true, false.
90+ *
91+ * @param mixed $value
92+ * @return bool|mixed
93+ */
94+ private function toBoolean ($ value )
95+ {
96+ if (is_string ($ value )) {
97+ $ value = strtolower ($ value );
98+ if (in_array ($ value , ['true ' , '1 ' ])) {
99+ return true ;
100+ }
101+ if (in_array ($ value , ['false ' , '0 ' ])) {
102+ return false ;
103+ }
104+ }
105+
106+ if (is_int ($ value ) && in_array ($ value , [0 , 1 ])) {
107+ return (bool ) $ value ;
108+ }
109+
110+ return $ value ;
111+ }
68112}
0 commit comments