Installation:
composer require dms/dms-filter-bundle
Enable the bundle in config/bundles.php:
DMS\Bundle\FilterBundle\DMSFilterBundle::class => ['all' => true],
First Use Case:
Annotate an entity with basic filters (e.g., Trim, StripTags) to automatically sanitize form inputs:
use DMS\Filter\Rules as Filter;
class User {
#[Filter\Trim]
#[Filter\StripTags]
public string $name;
}
The bundle will now auto-filter form submissions for this entity.
Where to Look First:
config/packages/dms_filter.yaml (for bundle configuration).FormType classes (to see auto-filtering in action).#[Filter\Email], #[Filter\Numeric]). The bundle hooks into Symfony’s form system to apply these filters automatically during form submission.#[Filter\Email]
#[Filter\NotEmpty]
public string $email;
Result: Invalid emails or empty values trigger Symfony’s validation errors.use DMS\Filter\FilterManager;
$filterManager = $container->get('dms.filter.inner.filter');
$filteredData = $filterManager->filter($rawData, User::class);
DMS\Filter\RuleInterface:
class CustomRule implements RuleInterface {
public function filter($value): ?string {
return strtoupper($value);
}
}
services.yaml:
services:
App\Filter\CustomRule:
tags: [dms.filter.rule]
#[Filter\CustomRule]
public string $customField;
auto_filter_forms: true/false).$builder->add('name', TextType::class, [
'dms_filter' => false,
]);
#[Assert\Length(min: 3)]
#[Filter\Trim]
public string $username;
Circular Dependencies:
Trim removes required whitespace), validation may fail unexpectedly.#[Filter\Trim] #[Assert\NotBlank] with whitespace-only input).Performance:
FilterManager for batch processing and disable auto-filtering in forms.Annotation Caching:
php bin/console cache:clear) after adding new annotations or custom rules.Symfony 6+ Compatibility:
symfony/property-access and symfony/property-info. Ensure your composer.json includes:
"require": {
"symfony/property-access": "^6.0",
"symfony/property-info": "^6.0"
}
Log Filtered Values:
#[Filter\CustomRule] with logging to inspect filtered data:
class DebugRule implements RuleInterface {
public function filter($value): ?string {
error_log("Raw: $value");
return $value;
}
}
Disable Auto-Filtering:
auto_filter_forms: false in config/packages/dms_filter.yaml to isolate issues.Check Service Availability:
dms.filter.inner.filter is autowired correctly. If not, verify the bundle is enabled and dependencies are installed.Dynamic Rule Application:
FilterManager to conditionally apply rules:
$filterManager->addRule($property, new CustomRule(), $entity);
Rule Prioritization:
#[Filter\Order] to customize:
#[Filter\Order(10)] // Lower numbers run first
#[Filter\Trim]
public string $field;
Non-Entity Filtering:
FilterManager to filter arrays or objects without annotations:
$filterManager->filter($data, null, [
'field' => [new Trim(), new StripTags()]
]);
Case Sensitivity:
#[Filter\Trim] vs. #[Filter\trim]). Use the exact class name from the rules list.Null Handling:
#[Filter\Email]) may throw exceptions on null values. Use #[Filter\Default] to provide fallbacks:
#[Filter\Default('default@example.com')]
#[Filter\Email]
public ?string $email;
How can I help you explore Laravel packages today?