Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions config/custom-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,30 @@
'cluster' => null,
],

/*
|--------------------------------------------------------------------------
| Field Settings
|--------------------------------------------------------------------------
|
| Configure default settings for custom fields.
|
*/
'fields' => [
Comment thread
ManukMinasyan marked this conversation as resolved.
'description_max_length' => 255,
],

/*
|--------------------------------------------------------------------------
| Section Settings
|--------------------------------------------------------------------------
|
| Configure default settings for custom field sections.
|
*/
'sections' => [
'description_max_length' => 255,
],

/*
|--------------------------------------------------------------------------
| Database Configuration
Expand Down
6 changes: 5 additions & 1 deletion src/Filament/Integration/Base/AbstractFormComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ function (Field $field) use ($customField): Field {
$this->coreVisibilityLogic->shouldAlwaysSave($customField) ||
filled($state)
)
->required($this->validationService->isRequired($customField))
->when(
$this->validationService->isRequired($customField) && $customField->typeData->dataType->isBoolean(),
fn (Field $field): Field => $field->markAsRequired(),
fn (Field $field): Field => $field->required($this->validationService->isRequired($customField)),
)
->rules(fn (Field $component): array => $this->getFieldValidationRules(
$customField,
$component->getRecord()?->getKey()
Expand Down
2 changes: 1 addition & 1 deletion src/Filament/Management/Schemas/FieldForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public static function schema(bool $withOptionsRelationship = true): array
]),
Textarea::make('settings.description')
->label(__('custom-fields::custom-fields.field.form.description'))
->maxLength(255)
->maxLength(config('custom-fields.fields.description_max_length', 255))
->rows(2)
->live(onBlur: true)
->columnSpanFull()
Expand Down
2 changes: 1 addition & 1 deletion src/Filament/Management/Schemas/SectionForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public static function schema(): array
fn (Get $get): bool => $get('type') ===
CustomFieldSectionType::SECTION
)
->maxLength(255)
->maxLength(config('custom-fields.sections.description_max_length', 255))
->nullable()
->columnSpan(12),
...self::visibilitySchema(),
Expand Down
4 changes: 4 additions & 0 deletions src/Services/ValidationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ private function getTypeSpecificRules(CustomField $customField, string|int|null
$rules[] = $decimalPlaces === 0 ? 'integer' : 'decimal:0,'.$decimalPlaces;
}

if ($this->isRequired($customField) && $customField->typeData->dataType->isBoolean()) {
$rules[] = 'accepted';
}

return $rules;
}

Expand Down
42 changes: 42 additions & 0 deletions tests/Feature/Integration/ValidationCapabilitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,3 +458,45 @@ function validationSubmitEdit(Post $post, array $customFields): Testable
->assertHasNoFormErrors()
->assertRedirect();
});

// ===========================================================================
// Required boolean (checkbox/toggle) validation
// ===========================================================================

it('rejects required checkbox with false value', function (): void {
createField($this, 'agree_terms', 'checkbox', ['required' => true]);

validationSubmitCreate($this, ['agree_terms' => false])
->assertHasFormErrors(['custom_fields.agree_terms']);
});

it('accepts required checkbox with true value', function (): void {
createField($this, 'agree_terms', 'checkbox', ['required' => true]);

validationSubmitCreate($this, ['agree_terms' => true])
->assertHasNoFormErrors()
->assertRedirect();
});

it('rejects required toggle with false value', function (): void {
createField($this, 'opt_in', 'toggle', ['required' => true]);

validationSubmitCreate($this, ['opt_in' => false])
->assertHasFormErrors(['custom_fields.opt_in']);
});

it('accepts required toggle with true value', function (): void {
createField($this, 'opt_in', 'toggle', ['required' => true]);

validationSubmitCreate($this, ['opt_in' => true])
->assertHasNoFormErrors()
->assertRedirect();
});

it('accepts non-required checkbox with false value', function (): void {
createField($this, 'newsletter', 'checkbox');

validationSubmitCreate($this, ['newsletter' => false])
->assertHasNoFormErrors()
->assertRedirect();
});