Installation:
composer require avalia/shtumi-useful-bundle
(Note: The original README uses a legacy deps file method; Composer is preferred for modern Laravel/Symfony projects.)
Register the Bundle (in config/bundles.php for Symfony/Laravel):
return [
// ...
Shtumi\UsefulBundle\ShtumiUsefulBundle::class => ['all' => true],
];
Enable Twig Form Theming (in config/packages/twig.yaml):
twig:
form_themes: ['@ShtumiUsefulBundle/fields.html.twig']
Load jQuery (in a base layout template):
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
// In a Symfony FormBuilder
$builder->add('user', Shtumi\UsefulBundle\Form\Type\AjaxAutocompleteType::class, [
'label' => 'Search Users',
'route' => 'user_autocomplete', // Custom route for autocomplete data
'min_length' => 2,
]);
Route Definition (in config/routes.yaml):
user_autocomplete:
path: /api/user/autocomplete
controller: App\Controller\UserController::autocompleteAction
Workflow:
UserRepository::findByName()).AjaxAutocompleteType in forms with route pointing to the controller.{% extends '@ShtumiUsefulBundle/AjaxAutocomplete/autocomplete.html.twig' %}).Integration with SonataAdmin:
// In a SonataAdmin service
$formMapper
->add('user', 'shtumi_ajax_autocomplete', [
'route' => 'sonata_admin_user_autocomplete',
'route_parameters' => ['admin' => 'sonata.admin.user'],
]);
$builder->add('country', EntityType::class, ['class' => Country::class]);
$builder->add('region', Shtumi\UsefulBundle\Form\Type\DependentFilteredEntityType::class, [
'entity' => Region::class,
'property' => 'name',
'dependent_property' => 'country',
'dependent_entity' => Country::class,
]);
$builder->add('date_range', Shtumi\UsefulBundle\Form\Type\DateRangeType::class, [
'widget' => 'single_text', // or 'split'
'start_date_options' => ['label' => 'Start Date'],
'end_date_options' => ['label' => 'End Date'],
]);
DateRange constraint:
use Shtumi\UsefulBundle\Validator\Constraints\DateRange;
/**
* @Assert\DateRange(
* startDate = "start_date",
* endDate = "end_date",
* message = "Invalid date range"
* )
*/
$qb = $this->createQueryBuilder('u');
$qb->select('IF(u.is_active = 1, "Yes", "No") as status');
$qb->orWhere('DATE_DIFF(u.created_at, CURRENT_DATE) > 30');
Shtumi\UsefulBundle\Doctrine\DQL\Function\AbstractFunction.protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->add('name')
->add('user', 'shtumi_ajax_autocomplete', [
'route' => 'sonata_admin_user_autocomplete',
'route_parameters' => ['admin' => 'sonata.admin.user'],
]);
}
jQuery Dependency:
Route Configuration:
data and label fields. Example:
return new JsonResponse($users->map(fn($u) => ['id' => $u->id, 'label' => $u->name]));
Doctrine DQL Functions:
DATE_DIFF require Doctrine DBAL support. For MySQL, ensure your platform is configured (e.g., Doctrine\DBAL\Platforms\MySQLPlatform).IFNULL for NULL-safe operations:
$qb->select('IFNULL(u.email, "N/A") as email');
Twig Theming:
templates/ShtumiUsefulBundle/ to customize appearance. Clear cache after changes:
php bin/console cache:clear
Autocomplete Not Loading:
{"data": [...]}).Dependent Filtered Entity Not Updating:
dependent_property matches the entity’s property name exactly (case-sensitive).ajax_options to debug:
'ajax_options' => ['debug' => true] // Logs AJAX calls to console
Date Range Validation Failing:
DateRange constraint is applied to the form type, not the entity property.start_date/end_date as field names in the constraint.Custom Autocomplete Data:
Shtumi\UsefulBundle\Form\Type\AjaxAutocompleteType to modify data processing:
class CustomAutocompleteType extends AjaxAutocompleteType {
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults([
'custom_data' => null, // Add your custom option
]);
}
}
Add New DQL Functions:
MyCustomFunction) extending AbstractFunction and register it in the bundle’s Resources/config/doctrine.xml:
<doctrine-dql>
<function name="MY_CUSTOM_FUNC" class="App\Doctrine\DQL\MyCustomFunction"/>
</doctrine-dql>
SonataAdmin List Filters:
Shtumi\UsefulBundle\Form\Type\AjaxAutocompleteType and register it in SonataAdmin’s filter configuration.While this bundle is Symfony-focused, Laravel developers can adapt it via:
symfony/flex or symfony/console to integrate Symfony components.FormBuilder with Laravel’s Form facade (e.g., Form::macro() for custom types).doctrine/dbal.How can I help you explore Laravel packages today?