Download the PHP package droath/console-form without Composer
On this page you can find all versions of the php package droath/console-form. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package console-form
Console Form
A simple form solution when using the Symfony console component.
There are two ways forms can be created, one is using the standalone method which usually takes place in the command execute method.
Or by taking advantage of the auto discovery feature so you can reuse forms throughout the project, which keeps the form logic decoupled from the command it's executed on.
Getting Started
First, you'll need to download the console form library using composer:
Set the \Droath\ConsoleForm\FormHelper as a helper class within the console application HelperSet:
Auto Discovery
If you decide to use the auto discovery feature you'll need to use the \Droath\ConsoleForm\FormDiscovery class:
The \Droath\ConsoleForm\FormDiscovery::discover() method takes two arguments, the first argument is either a single directory or an array or directories. These directories are searched for form classes that have implemented the \Droath\ConsoleForm\FormInterface interface. The second argument is the class namespace.
Here is an example of what the form class needs to implement so it's found by the auto discovery feature:
Form Basics
Interacting with the form helper within the command class. Either you can create a form using the standalone method or retrieve the form using the name that was defined (auto discovery needs to be implemented).
First, I'll show you the standalone method on which you can create a form:
If using auto discovery you can simply just retrieve the form by name:
Form Save
Many of times forms need to save their results to a filesystem. The form save() method displays a confirmation message asking the user to save the results (which is configurable). If input is true, then the save callable is invoked; otherwise it's disregarded.
Form Field Groups
There might come a time when you need to group fields together. One of the advantages would be having the ability to collect multiple inputs for a grouping of fields. As you can see in the example below, the setLoopUntil()
provides the result array that was last given, these values can help determine if you should stop collecting data. Return false
to stop field group iteration, otherwise set to true
.
Form Fields
There are three basic field types which are:
- Text
- Select
- Boolean
All field types derive from the base field class, so most fields have similar methods and features.
Below are some examples of the most useful field methods:
Condition
The setCondition() method allows a field to be shown based on what value was previously inputted by the user. This method can be set multiple times if you require more conditions to be met for a given field.
The "More options for 7.x version" text field will only be shown if the 7.x version was selected in the previous question.
Field Callback
The setFieldCallback() method requires a single callback function, which can be any valid PHP callable. The callback will be invoked during the form process lifecycle. The field callback receives two arguments, the first argument is the field instance. The second is an array of results for all previous questions.
Now you can set additional methods for a given field instance based on the results for a previous question(s). As you can see in the example below the select options are added based on the results retrieved from the "Project Name" text field.
Subform
The setSubform() method requires a callback function to be set, which can be an anonymous function or any valid PHP callable. The subform function is invoked during the original form process lifecycle.
The subform callable is given two arguments, the first argument is the subform instance and the second is the inputed value. The callable doesn't need to return any data. Also, the original form process takes care of processing the subform.
All versions of console-form with dependencies
symfony/console Version ^2.8 || ^3.0
symfony/finder Version ^2.8 || ^3.0 || ^4.0