PHPackages                             martin1982/mfconditionalfieldsbundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. martin1982/mfconditionalfieldsbundle

ActiveSymfony-bundle[Utility &amp; Helpers](/categories/utility)

martin1982/mfconditionalfieldsbundle
====================================

Symfony MF Conditional Fields bundle

1.1.3(1y ago)01.3k[2 issues](https://github.com/Martin1982/mfconditionalfieldsbundle/issues)MITPHPPHP &gt;=8.2

Since Jul 23Pushed 1y ago1 watchersCompare

[ Source](https://github.com/Martin1982/mfconditionalfieldsbundle)[ Packagist](https://packagist.org/packages/martin1982/mfconditionalfieldsbundle)[ RSS](/packages/martin1982-mfconditionalfieldsbundle/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (4)Versions (7)Used By (0)

Prerequisites
=============

[](#prerequisites)

Make sure you load the [mf-conditional fields JS library](https://github.com/bomsn/mf-conditional-fields) in your project on the pages where you use conditional fields. You can either use script or module, currently the script is executed when `mfConditionalFields` is available in the global namespace.

If you use a bundler (like Webpack), make sure to implement form initialization.

Example;

```
import mfConditionalFields from "mf-conditional-fields";

let initialized = [];

document.addEventListener('DOMContentLoaded', () => {
    document.querySelectorAll('[data-conditional-rules]').forEach((element) => {
        let form = element.closest('form');
        let formId = form.getAttribute('id');

        if (initialized.includes(formId)) {
            return;
        }

        initialized.push(formId);

        mfConditionalFields('#' + formId, {
            debug: process.env.NODE_ENV === 'development',
        });
    });
});
```

Installation
============

[](#installation)

Make sure Composer is installed globally, as explained in the [installation chapter](https://getcomposer.org/doc/00-intro.md)of the Composer documentation.

Applications that use Symfony Flex
----------------------------------

[](#applications-that-use-symfony-flex)

Open a command console, enter your project directory and execute:

```
composer require martin1982/mfconditionalfieldsbundle
```

Applications that don't use Symfony Flex
----------------------------------------

[](#applications-that-dont-use-symfony-flex)

### Step 1: Download the Bundle

[](#step-1-download-the-bundle)

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

```
composer require martin1982/mfconditionalfieldsbundle
```

### Step 2: Enable the Bundle

[](#step-2-enable-the-bundle)

Then, enable the bundle by adding it to the list of registered bundles in the `config/bundles.php` file of your project:

```
// config/bundles.php

return [
    // ...
    Martin1982\MfConditionalFieldsBundle\MfConditionalFieldsBundle::class => ['all' => true],
];
```

### Step 3: Enable the form theme

[](#step-3-enable-the-form-theme)

When using Twig you can initialize a form with conditional fields using a `form_theme` setting in your twig config:

```
twig:
    form_themes: ['@MfConditionalFields/conditional_field.html.twig']
```

Usage
=====

[](#usage)

On your FormType class implement the `ConditionalRulesInterface` for easy access to all options. When adding a field using the FormBuilder you can make a field dependent by providing the `conditional_options` option.

The following options are available:

NameTypeDescriptioncontainerStringThe container for the given actionactionStringThe action that needs to be performed when the rules applylogicStringOR when only one condition needs to be met, AND when all need to be metrulesArrayArray of rules to check, with at least 1 ruleThe rules consist of these options:

NameTypeDescriptionnameStringField name to checkoperatorStringOperator used to check fieldvalueStringExpected value for the rule to applyExample
=======

[](#example)

In this example the code from the Symfony documentation is used to select if someone is attending. In addition, it'll show a reason text element when a user selects 'Maybe' as an option.

```
