Skip to content

Commit

Permalink
added fields with conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
AravindRam-Ranium committed Feb 29, 2024
1 parent 484c373 commit 5a52a45
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 17 deletions.
40 changes: 29 additions & 11 deletions src/Livewire/ConsentOptionFormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,21 @@ protected function getFormSchema(): array
$additionInfo = [];
foreach ($consentOption->fields as $field) {
$fieldName = "consents_info.$consentOption->id.{$field['name']}";
$fieldLabel = $field['label'] ?? '';
$columnSpan = $field['column_span'] ?? 1;
$options = array_combine(explode(',',$field['options']), explode(',',$field['options']));


$additionInfo[] = match ($field['type']) {
'text' => Forms\Components\TextInput::make($fieldName)->label($field['label'])->required((bool)$field['required']),
'email' => Forms\Components\TextInput::make($fieldName)->label($field['label'])->email()->required((bool)$field['required']),
'select' => Forms\Components\Select::make($fieldName)->label($field['label'])->options($options)->required((bool)$field['required']),
'textarea' => Forms\Components\Textarea::make($fieldName)->label($field['label'])->required((bool)$field['required']),
'number' => Forms\Components\TextInput::make($fieldName)->label($field['label'])->numeric()->required((bool)$field['required']),
'check' => Forms\Components\Checkbox::make($fieldName)->label($field['label'])->required((bool)$field['required']),
'radio' => Forms\Components\Radio::make($fieldName)->label($field['label'])->options($options)->required((bool)$field['required']),
'date' => Forms\Components\DatePicker::make($fieldName)->label($field['label'])->required((bool)$field['required']),
'datetime' => Forms\Components\DateTimePicker::make($fieldName)->label($field['label'])->required((bool)$field['required']),
'text' => $this->prepareField(Forms\Components\TextInput::make($fieldName)->label($fieldLabel)->columnSpan($columnSpan), $field, $consentOption),
'email' => $this->prepareField(Forms\Components\TextInput::make($fieldName)->label($fieldLabel)->email()->columnSpan($columnSpan), $field, $consentOption),
'select' => $this->prepareField(Forms\Components\Select::make($fieldName)->label($fieldLabel)->options($options)->columnSpan($columnSpan), $field, $consentOption),
'textarea' => $this->prepareField(Forms\Components\Textarea::make($fieldName)->label($fieldLabel)->columnSpan($columnSpan), $field, $consentOption),
'number' => $this->prepareField(Forms\Components\TextInput::make($fieldName)->label($fieldLabel)->numeric()->columnSpan($columnSpan), $field, $consentOption),
'check' => $this->prepareField(Forms\Components\Checkbox::make($fieldName)->label($fieldLabel)->columnSpan($columnSpan), $field, $consentOption),
'radio' => $this->prepareField(Forms\Components\Radio::make($fieldName)->label($fieldLabel)->options($options)->columnSpan($columnSpan), $field, $consentOption),
'date' => $this->prepareField(Forms\Components\DatePicker::make($fieldName)->label($fieldLabel)->columnSpan($columnSpan), $field, $consentOption),
'datetime' => $this->prepareField(Forms\Components\DateTimePicker::make($fieldName)->label($fieldLabel)->columnSpan($columnSpan), $field, $consentOption),
};
}

Expand Down Expand Up @@ -122,7 +126,6 @@ public function previousConsents($key)
public function submit(): void
{
$formData = $this->form->getState();
$consentInfo = $formData['consents_info'];

$conentIds = [];
foreach($formData['consents'] as $key => $value) {
Expand All @@ -139,7 +142,7 @@ public function submit(): void
[
'accepted' => in_array($consentOption->id, $conentIds),
'key' => $consentOption->key,
'fields' => (bool)$consentOption->additional_info ? $consentInfo[$consentOption->id] : []
'fields' => ((bool)$consentOption->additional_info && isset($formData['consents_info'])) ? $formData['consents_info'][$consentOption->id] : []
]
);
}
Expand All @@ -155,4 +158,19 @@ public function submit(): void

$this->redirect(request()->session()->get('url.saved'));
}

public function prepareField($component, $fieldOption, $consentOption)
{
if(isset($fieldOption['custom_rules']) && (bool)$fieldOption['custom_rules']) {

foreach ($fieldOption['rules'] as $key => $value) {

if($value['rule_type'] == "require_if_another_field") {
$anotherField = "consents_info.$consentOption->id.{$value['another_field']}";
$component->requiredIf($anotherField, $fieldOption['value_equals']);
}
}
}
return $component->required((bool)$fieldOption['required']);
}
}
33 changes: 27 additions & 6 deletions src/Resources/ConsentOptionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,38 @@ public static function form(Form $form): Form
'text' => 'Text Input',
'email' => 'Email Input',
'number' => 'Number Input',
'date' => 'Date Picker',
'datetime' => 'Date & Time Picker',
'textarea' => 'Text area',
'textarea' => 'Text area',
'select' => 'Select dropdown',
'radio' => 'Radio dropdown',
'radio' => 'Radio options',
'check' => 'Checkbox',
'date' => 'Date Picker',
'datetime' => 'Date & Time Picker',
])
->required(),
Forms\Components\TextInput::make('label'),
Forms\Components\TagsInput::make('options')->separator(',')->splitKeys(['Tab']),
Forms\Components\Select::make('column_span')
->options([
1 => '1 Column',
2 => '2 Columns',
3 => '3 Columns',
])
->default(1)
->required(),
Forms\Components\TextInput::make('label')->required(),
Forms\Components\TagsInput::make('options')->separator(',')->splitKeys(['Tab', ' ']),
Forms\Components\Toggle::make('required')->inline(false)->required(),
Forms\Components\Toggle::make('custom_rules')->inline(false)->live(),
Repeater::make('rules')->label('')->schema([
Forms\Components\Select::make('rule_type')
->options([
'require_if_another_field' => 'Required If another field matches',
]),
Forms\Components\TextInput::make('another_field')->label('Another field name'),
Forms\Components\TextInput::make('value_equals')->label('Value equals'),
])
->visible(fn(Get $get) => (bool)$get('custom_rules'))
->addActionLabel('Add Rule')
->columnSpanFull()
->columns(3)
])
->defaultItems(1)
->columns(3)
Expand Down

0 comments on commit 5a52a45

Please sign in to comment.