public function dependentOn(array $previousFieldNames, callable $dependentStageDefineCallback, array $fieldNamesDefinedInStage = [])
    {
        if ($this->isDependent) {
            throw InvalidOperationException::format(
                'Invalid call to %s: cannot nest dependent form sections', __METHOD__
            );
        }

        $this->finishCurrentStage();

        if (!in_array(IObjectAction::OBJECT_FIELD_NAME, $previousFieldNames, true) && !$this->isCreateForm()) {
            $previousFieldNames[] = IObjectAction::OBJECT_FIELD_NAME;
        }

        $this->stages[] = new DependentFormStage(function (array $previousData) use ($dependentStageDefineCallback) {
            $this->isDependent = true;
            $objectInstance    = isset($previousData[IObjectAction::OBJECT_FIELD_NAME])
                ? $previousData[IObjectAction::OBJECT_FIELD_NAME]
                : null;

            if ($objectInstance) {
                /** @var TypedObject $objectInstance */
                $this->class                   = $objectInstance::definition();
                $this->currentEditedObjectType = get_class($objectInstance);
            }

            $dependentStageDefineCallback(
                $this,
                $previousData,
                $objectInstance
            );
            $this->isDependent = false;

            $form = $this->buildFormForCurrentStage();

            if ($objectInstance) {
                $form = $form->withInitialValuesFrom($objectInstance);
            }

            $this->exitStage();

            return $form;
        }, $fieldNamesDefinedInStage, in_array('*', $previousFieldNames, true) ? null : array_unique($previousFieldNames));
    }
public function dependentOn(array $previousFieldNames, callable $dependentStageDefineCallback, array $fieldNamesDefinedInStage = [])
    {
        if ($this->isDependent) {
            throw InvalidOperationException::format(
                'Invalid call to %s: cannot nest dependent form sections', __METHOD__
            );
        }

        $this->finishCurrentStage();

        if (!in_array(IObjectAction::OBJECT_FIELD_NAME, $previousFieldNames, true) && !$this->isCreateForm()) {
            $previousFieldNames[] = IObjectAction::OBJECT_FIELD_NAME;
        }

        $this->stages[] = new DependentFormStage(function (array $previousData) use ($dependentStageDefineCallback) {
            $this->isDependent = true;
            $objectInstance    = isset($previousData[IObjectAction::OBJECT_FIELD_NAME])
                ? $previousData[IObjectAction::OBJECT_FIELD_NAME]
                : null;

            if ($objectInstance) {
                /** @var TypedObject $objectInstance */
                $this->class                   = $objectInstance::definition();
                $this->currentEditedObjectType = get_class($objectInstance);
            }

            $dependentStageDefineCallback(
                $this,
                $previousData,
                $objectInstance
            );
            $this->isDependent = false;

            $form = $this->buildFormForCurrentStage();

            if ($objectInstance) {
                $form = $form->withInitialValuesFrom($objectInstance);
            }

            $this->exitStage();

            return $form;
        }, $fieldNamesDefinedInStage, in_array('*', $previousFieldNames, true) ? null : array_unique($previousFieldNames));
    }
public function dependentOn ( array $ previousFieldNames , callable $ dependentStageDefineCallback , array $ fieldNamesDefinedInStage = [ ] ) { if ( $ this -> isDependent ) { throw InvalidOperationException :: format ( 'Invalid call to %s: cannot nest dependent form sections' , __METHOD__ ) ; } $ this -> finishCurrentStage ( ) ; if ( ! in_array ( IObjectAction :: OBJECT_FIELD_NAME , $ previousFieldNames , true ) && ! $ this -> isCreateForm ( ) ) { $ previousFieldNames [ ] = IObjectAction :: OBJECT_FIELD_NAME ; } $ this -> stages [ ] = new DependentFormStage ( function ( array $ previousData ) use ( $ dependentStageDefineCallback ) { $ this -> isDependent = true ; $ objectInstance = isset ( $ previousData [ IObjectAction :: OBJECT_FIELD_NAME ] ) ? $ previousData [ IObjectAction :: OBJECT_FIELD_NAME ] : null ; if ( $ objectInstance ) { /** @var TypedObject $objectInstance */ $ this -> class = $ objectInstance :: definition ( ) ; $ this -> currentEditedObjectType = get_class ( $ objectInstance ) ; } $ dependentStageDefineCallback ( $ this , $ previousData , $ objectInstance ) ; $ this -> isDependent = false ; $ form = $ this -> buildFormForCurrentStage ( ) ; if ( $ objectInstance ) { $ form = $ form -> withInitialValuesFrom ( $ objectInstance ) ; } $ this -> exitStage ( ) ; return $ form ; } , $ fieldNamesDefinedInStage , in_array ( '*' , $ previousFieldNames , true ) ? null : array_unique ( $ previousFieldNames ) ) ; }
Defines a section of the form that is dependent on other fields.

The supplied callback will be passed the values for the dependent fields
as the second parameter and the object instance as the third parameter or
NULL if it is a create form.

Example:
<code>
$form->dependentOn(['name'], function (CrudFormDefinition $form, array $input, Person $object = null) {
if ($input['name'] === 'John') {
// ...
} else {
// ...
}
});
</code>

@param string[] $previousFieldNames
@param callable $dependentStageDefineCallback
@param string[] $fieldNamesDefinedInStage

@return void
@throws InvalidOperationException
Defines a section of the form that is dependent on other fields .