|
3 | 3 | namespace Code16\Formoj\Sharp;
|
4 | 4 |
|
5 | 5 | use Code16\Formoj\Models\Field;
|
| 6 | +use Code16\Formoj\Models\Section; |
6 | 7 | use Code16\Sharp\Form\Eloquent\WithSharpFormEloquentUpdater;
|
7 | 8 | use Code16\Sharp\Form\Fields\SharpFormCheckField;
|
8 | 9 | use Code16\Sharp\Form\Fields\SharpFormListField;
|
|
14 | 15 | use Code16\Sharp\Form\Layout\FormLayoutFieldset;
|
15 | 16 | use Code16\Sharp\Form\SharpForm;
|
16 | 17 | use Code16\Sharp\Utils\Fields\FieldsContainer;
|
| 18 | +use Illuminate\Validation\Rule; |
17 | 19 |
|
18 | 20 | class FormojFieldSharpForm extends SharpForm
|
19 | 21 | {
|
20 | 22 | use WithSharpFormEloquentUpdater;
|
21 |
| - |
22 |
| - protected ?string $formValidatorClass = FormojFieldSharpValidator::class; |
23 | 23 |
|
24 | 24 | function buildFormFields(FieldsContainer $formFields) : void
|
25 | 25 | {
|
@@ -125,27 +125,28 @@ function buildFormLayout(FormLayout $formLayout): void
|
125 | 125 | ->addColumn(6, function (FormLayoutColumn $column) {
|
126 | 126 | $column
|
127 | 127 | ->withFieldset(trans("formoj::sharp.fields.fields.fieldsets.identifiers"), function (FormLayoutFieldset $fieldset) {
|
128 |
| - $fieldset->withSingleField("label") |
129 |
| - ->withSingleField("identifier"); |
| 128 | + $fieldset |
| 129 | + ->withField("label") |
| 130 | + ->withField("identifier"); |
130 | 131 | })
|
131 |
| - ->withSingleField("type") |
132 |
| - ->withSingleField("required") |
133 |
| - ->withSingleField("help_text"); |
| 132 | + ->withField("type") |
| 133 | + ->withField("required") |
| 134 | + ->withField("help_text"); |
134 | 135 |
|
135 | 136 | })
|
136 | 137 | ->addColumn(6, function (FormLayoutColumn $column) {
|
137 | 138 | $column
|
138 |
| - ->withSingleField("max_size") |
139 |
| - ->withSingleField("accept") |
140 |
| - ->withSingleField("max_length") |
141 |
| - ->withSingleField("rows_count") |
142 |
| - ->withSingleField("options", function(FormLayoutColumn $column) { |
143 |
| - $column->withSingleField("label"); |
| 139 | + ->withField("max_size") |
| 140 | + ->withField("accept") |
| 141 | + ->withField("max_length") |
| 142 | + ->withField("rows_count") |
| 143 | + ->withListField("options", function(FormLayoutColumn $column) { |
| 144 | + $column->withField("label"); |
144 | 145 | })
|
145 |
| - ->withSingleField("radios") |
146 |
| - ->withSingleField("multiple") |
147 |
| - ->withSingleField("lowest_label") |
148 |
| - ->withSingleField("highest_label"); |
| 146 | + ->withField("radios") |
| 147 | + ->withField("multiple") |
| 148 | + ->withField("lowest_label") |
| 149 | + ->withField("highest_label"); |
149 | 150 | });
|
150 | 151 | }
|
151 | 152 |
|
@@ -177,7 +178,7 @@ function update($id, array $data)
|
177 | 178 | $field = $id
|
178 | 179 | ? Field::findOrFail($id)
|
179 | 180 | : new Field([
|
180 |
| - "section_id" => currentSharpRequest()->getPreviousShowFromBreadcrumbItems()->instanceId(), |
| 181 | + "section_id" => sharp()->context()->breadcrumb()->previousShowSegment()->instanceId(), |
181 | 182 | "order" => 100
|
182 | 183 | ]);
|
183 | 184 |
|
@@ -238,4 +239,38 @@ protected function castValue(?string $value, string $type)
|
238 | 239 |
|
239 | 240 | return $value;
|
240 | 241 | }
|
| 242 | + |
| 243 | + public function rules() |
| 244 | + { |
| 245 | + return [ |
| 246 | + 'label' => [ |
| 247 | + 'required', |
| 248 | + 'max:200' |
| 249 | + ], |
| 250 | + 'identifier' => [ |
| 251 | + 'required', |
| 252 | + 'max:100', |
| 253 | + 'alpha_dash', |
| 254 | + Rule::unique('formoj_fields', 'identifier') |
| 255 | + ->whereIn("section_id", |
| 256 | + Section::select("id") |
| 257 | + ->where("form_id", |
| 258 | + Section::find(sharp()->context()->breadcrumb()->previousShowSegment()->instanceId()) |
| 259 | + ->form_id |
| 260 | + ) |
| 261 | + ->pluck("id") |
| 262 | + ->all() |
| 263 | + ) |
| 264 | + ->ignore(sharp()->context()->instanceId()) |
| 265 | + ], |
| 266 | + 'type' => 'required', |
| 267 | + 'max_length' => 'integer|nullable', |
| 268 | + 'max_values' => 'integer|nullable', |
| 269 | + 'rows_count' => 'integer|nullable|required_if:type,' . Field::TYPE_TEXTAREA, |
| 270 | + 'options' => 'required_if:type,' . Field::TYPE_SELECT, |
| 271 | + 'options.*.label' => 'required', |
| 272 | + 'max_size' => 'required_if:type,' . Field::TYPE_UPLOAD . '|integer|nullable', |
| 273 | + 'accept' => ['nullable','regex:/^(\.[a-z]+,)*(\.[a-z]+)$/'] |
| 274 | + ]; |
| 275 | + } |
241 | 276 | }
|
0 commit comments