@@ -37,25 +37,13 @@ class ConfigSectionForm extends ConfigForm
3737 /** @var string Event emitted when the form has successfully renamed a configuration section */
3838 public const ON_RENAME = 'rename ' ;
3939
40- /**
41- * Whether the form is used for creating a new configuration section
42- *
43- * @var bool
44- */
40+ /** @var bool Whether the form is used for creating a new configuration section */
4541 protected bool $ isCreateForm = false ;
4642
47- /**
48- * Whether the form allows deletion of the configuration section
49- *
50- * @var bool
51- */
43+ /** @var bool Whether the form allows deletion of the configuration section */
5244 protected bool $ allowDeletion = true ;
5345
54- /**
55- * Whether the form allows renaming of the configuration section
56- *
57- * @var bool
58- */
46+ /** @var bool Whether the form allows renaming of the configuration section */
5947 protected bool $ allowRename = true ;
6048
6149 /**
@@ -119,6 +107,11 @@ public function isValid(): bool
119107 return parent ::isValid ();
120108 }
121109
110+ /**
111+ * Whether the form is used for creating a new configuration section
112+ *
113+ * @return bool
114+ */
122115 public function isCreateForm (): bool
123116 {
124117 return $ this ->isCreateForm ;
@@ -131,16 +124,17 @@ public function isCreateForm(): bool
131124 *
132125 * @return static
133126 *
134- * @throws LogicException If the form has already been assembled or if the form is a creation form
127+ * @throws LogicException If the form has already been assembled or if the form
128+ * is a creation form
135129 */
136130 public function setAllowDeletion (bool $ allowDeletion = true ): static
137131 {
138132 if ($ this ->isCreateForm ()) {
139- throw new LogicException ('Can never delete a new configuration section. ' );
133+ throw new LogicException ('Can never delete a new configuration section ' );
140134 }
141135
142136 if ($ this ->hasBeenAssembled ) {
143- throw new LogicException ('Form has already been assembled. ' );
137+ throw new LogicException ('Form has already been assembled ' );
144138 }
145139
146140 $ this ->allowDeletion = $ allowDeletion ;
@@ -167,16 +161,17 @@ public function allowDeletion(): bool
167161 *
168162 * @return $this
169163 *
170- * @throws LogicException If the form has already been assembled or if the form is a creation form
164+ * @throws LogicException If the form has already been assembled or if the form
165+ * is a creation form
171166 */
172167 public function setAllowRename (bool $ allowRename = true ): static
173168 {
174169 if ($ this ->isCreateForm ()) {
175- throw new LogicException ('Can never rename a new configuration section. ' );
170+ throw new LogicException ('Can never rename a new configuration section ' );
176171 }
177172
178173 if ($ this ->hasBeenAssembled ) {
179- throw new LogicException ('Form has already been assembled. ' );
174+ throw new LogicException ('Form has already been assembled ' );
180175 }
181176
182177 $ this ->allowRename = $ allowRename ;
@@ -241,11 +236,7 @@ protected function handleRename(): void
241236 $ this ->config ->removeSection ($ oldName );
242237 $ this ->section = $ newName ;
243238 parent ::onSuccess ();
244- $ this ->emit (static ::ON_RENAME , [
245- $ this ,
246- $ oldName ,
247- $ this ->section ,
248- ]);
239+ $ this ->emit (static ::ON_RENAME , [$ this , $ oldName , $ this ->section ]);
249240 }
250241
251242 /**
@@ -267,6 +258,11 @@ public function shouldDelete(): bool
267258 return $ deleteButton ->hasBeenPressed ();
268259 }
269260
261+ /**
262+ * Check if the form has a delete button
263+ *
264+ * @return bool
265+ */
270266 public function hasDeleteButton (): bool
271267 {
272268 return $ this ->hasElement (static ::DELETE_BUTTON_NAME );
@@ -297,12 +293,17 @@ protected function addSectionNameElement(array $params = []): void
297293 $ params ['ignore ' ] = true ;
298294 $ params ['label ' ] ??= $ this ->translate ('Name ' );
299295 $ params ['validators ' ][] = new CallbackValidator (function ($ value , CallbackValidator $ validator ) {
300- if (Str:: isEmpty ( $ value ) || $ value === $ this ->section ) {
296+ if ($ value === $ this ->section ) {
301297 return true ;
302298 }
303299
300+ if (Str::isEmpty ($ value )) {
301+ $ validator ->addMessage ($ this ->translate ('Please enter a name ' ));
302+ return false ;
303+ }
304+
304305 if ($ this ->config ->hasSection ($ value )) {
305- $ validator ->addMessage ($ this ->translate ('An entry with this name already exists. ' ));
306+ $ validator ->addMessage ($ this ->translate ('An entry with this name already exists ' ));
306307 return false ;
307308 }
308309
@@ -312,19 +313,19 @@ protected function addSectionNameElement(array $params = []): void
312313 $ this ->addElement ('text ' , static ::NAME_ELEMENT_NAME , $ params );
313314 }
314315
315- protected function getIniKeyFromName (string $ name ): ? array
316+ protected function getIniKeyFromName (string $ name ): array
316317 {
317- return $ this ->section === null ? null : [$ this ->section , $ name ];
318+ if ($ this ->section === null ) {
319+ return [];
320+ }
321+
322+ return [$ this ->section , $ name ];
318323 }
319324
320325 protected function onSuccess (): void
321326 {
322327 if ($ this ->isCreateForm ()) {
323328 $ this ->section = $ this ->getValue (static ::NAME_ELEMENT_NAME );
324-
325- if (Str::isEmpty ($ this ->section )) {
326- throw new LogicException ('Section must be set before saving a new configuration section. ' );
327- }
328329 parent ::onSuccess ();
329330 } elseif ($ this ->shouldDelete ()) {
330331 $ this ->handleDelete ();
@@ -413,12 +414,14 @@ protected function save(): void
413414 if (Str::isEmpty ($ value ) && $ this ->shouldRevertIfEmpty ($ element )) {
414415 $ value = $ this ->config ->get ($ this ->section , $ element );
415416 }
417+
416418 if (Str::isEmpty ($ value )) {
417419 unset($ configSection [$ element ]);
418420 } else {
419421 $ configSection ->$ element = $ value ;
420422 }
421423 }
424+
422425 $ this ->config ->setSection ($ this ->section , $ configSection );
423426 $ this ->config ->saveIni ();
424427 }
0 commit comments