PHP code example of ucscode / easyadmin-dependency-field-resolver

1. Go to this page and download the library: Download ucscode/easyadmin-dependency-field-resolver library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ucscode / easyadmin-dependency-field-resolver example snippets


use Ucscode\EasyAdmin\DependencyFieldResolver\Service\DependencyFieldResolver;

class UserCrudController extends AbstractCrudController
{
    public function __construct(
        private DependencyFieldResolver $resolver
    ) {}

    public function configureFields(string $pageName): iterable
    {
        return $this->resolver
            ->configureFields(function(): iterable {
                // Do exactly the same thing you would do in `configureFields()` of your crud controller
                // You can return an array or use `yield` to return a Generator
                // However, you should only return *INDEPENDENT* Fields
                yield TextField::new('username');

                yield ChoiceField::new('type')
                    ->setChoices([
                        'Individual' => 'individual',
                        'Organization' => 'org',
                    ]);
            })
            ->dependsOn('type', function(array $values): iterable {
                // This Closure will only run if 'type' is not null
                if ($values['type'] === 'org') {
                    yield TextField::new('companyName');
                    yield AssociationField::new('industry');
                }
            })
            ->dependsOn(['type', 'username'], function(array $values) use ($pageName): iterable {
                // This Closure will only run if both 'type' and 'username' are not null
                if ($values['username'] == 'joe' && $values['type'] == 'org') {
                    yield TextField::new('website');
                    return;
                }
                
                yield ChoiceField::new(...);
            })
            ->resolve();
    }
}


use Ucscode\EasyAdmin\DependencyFieldResolver\Event\DependencyChangedEvent;

class DependencySubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            DependencyChangedEvent::class => 'onDependencyChange',
        ];
    }

    public function onDependencyChange(DependencyChangedEvent $event): void
    {
        $data = $event->getPostData();

        // If the type changes, we might want to force clear the company name
        if ($data->get('type') === 'individual') {
            $data->set('companyName', null);
        }
    }
}


public function onDependencyChange(DependencyChangedEvent $event): void
{
    $data = $event->getPostData(); // The ResolverPostData DTO

    // If 'country' changed, clear 'state' so old data doesn't persist
    if ($data->has('country')) {
        $data->set('state', null);
    }
}


->configureFields(function() {
    yield CountryField::new('country');
})
->dependsOn('country', function(array $values) {
    yield ChoiceField::new('state')
        // Use false to ensure the form can always submit for state-tracking
        ->setRequired(false) 
        ->setFormTypeOptions([
            'constraints' => [
                // Use a constraint to ensure the data is valid upon final save
                new NotBlank([
                    'message' => 'Please select a state for ' . $values['country'],
                ])
            ]
        ]);
})