66use ipl \Html \Attributes ;
77use ipl \Html \FormElement \FieldsetElement ;
88use ipl \Html \HtmlElement ;
9+ use ipl \Validator \CallbackValidator ;
10+ use ipl \Validator \ValidatorChain ;
911use ipl \Web \FormElement \ScheduleElement \Common \FieldsProtector ;
1012
1113class WeeklyFields extends FieldsetElement
@@ -76,20 +78,15 @@ public function getSelectedWeekDays(): array
7678 /**
7779 * Transform the given weekdays into key=>value array that can be populated
7880 *
79- * @param array $days
81+ * @param array $weekdays
8082 *
8183 * @return array
8284 */
83- public function loadWeekDays (array $ days ): array
85+ public function loadWeekDays (array $ weekdays ): array
8486 {
8587 $ values = [];
86- foreach ($ days as $ day ) {
87- $ weekDays = strtoupper ($ day );
88- if (! isset ($ this ->weekdays [$ weekDays ])) {
89- throw new InvalidArgumentException (sprintf ('Invalid weekday provided: %s ' , $ day ));
90- }
91-
92- $ values [$ weekDays ] = 'y ' ;
88+ foreach ($ this ->weekdays as $ weekday => $ _ ) {
89+ $ values [$ weekday ] = in_array ($ weekday , $ weekdays , true ) ? 'y ' : 'n ' ;
9390 }
9491
9592 return $ values ;
@@ -102,15 +99,13 @@ protected function assemble()
10299 $ fieldsWrapper = HtmlElement::create ('div ' , ['class ' => 'weekly ' ]);
103100 $ listItems = HtmlElement::create ('ul ' , ['class ' => ['schedule-element-fields ' , 'multiple-fields ' ]]);
104101
105- $ foundCheckedDay = false ;
106102 foreach ($ this ->weekdays as $ day => $ value ) {
107103 $ checkbox = $ this ->createElement ('checkbox ' , $ day , [
108104 'class ' => ['autosubmit ' , 'sr-only ' ],
109- 'value ' => $ this ->getPopulatedValue ( $ day , ' n ' )
105+ 'value ' => $ day === $ this ->default
110106 ]);
111107 $ this ->registerElement ($ checkbox );
112108
113- $ foundCheckedDay = $ foundCheckedDay || $ checkbox ->isChecked ();
114109 $ htmlId = $ this ->protectId ("weekday- $ day " );
115110 $ checkbox ->getAttributes ()->set ('id ' , $ htmlId );
116111
@@ -119,10 +114,6 @@ protected function assemble()
119114 $ listItems ->addHtml ($ listItem );
120115 }
121116
122- if (! $ foundCheckedDay ) {
123- $ this ->getElement ($ this ->default )->setChecked (true );
124- }
125-
126117 $ fieldsWrapper ->addHtml ($ listItems );
127118 $ this ->addHtml ($ fieldsWrapper );
128119 }
@@ -135,4 +126,26 @@ protected function registerAttributeCallbacks(Attributes $attributes)
135126 ->registerAttributeCallback ('default ' , null , [$ this , 'setDefault ' ])
136127 ->registerAttributeCallback ('protector ' , null , [$ this , 'setIdProtector ' ]);
137128 }
129+
130+ protected function addDefaultValidators (ValidatorChain $ chain ): void
131+ {
132+ $ chain ->add (
133+ new CallbackValidator (function ($ _ , CallbackValidator $ validator ): bool {
134+ $ valid = false ;
135+ foreach ($ this ->weekdays as $ weekday => $ _ ) {
136+ if ($ this ->getValue ($ weekday ) === 'y ' ) {
137+ $ valid = true ;
138+
139+ break ;
140+ }
141+ }
142+
143+ if (! $ valid ) {
144+ $ validator ->addMessage ($ this ->translate ('You must select at least one of these weekdays ' ));
145+ }
146+
147+ return $ valid ;
148+ })
149+ );
150+ }
138151}
0 commit comments