1. Go to this page and download the library: Download magdev/console-form 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/ */
magdev / console-form example snippets
$application = new \Symfony\Component\Console\Application('Project-Demo', '0.0.1');
$application->getHelperSet()
->set(new \Droath\ConsoleForm\FormHelper());
...
namespace Droath\Project\Form;
use Droath\ConsoleForm\Field\BooleanField;
use Droath\ConsoleForm\Field\SelectField;
use Droath\ConsoleForm\Field\TextField;
use Droath\ConsoleForm\Form;
use Droath\ConsoleForm\FormInterface;
/**
* Define project setup form.
*/
class ProjectSetup implements FormInterface
{
/**
* {@inheritdoc}
*/
public function getName()
{
return 'project.form.setup';
}
/**
* {@inheritdoc}
*/
public function buildForm()
{
return (new Form())
->addField(new TextField('name', 'What is your name?'))
->addField((new SelectField('gender', 'What is your gender?'))
->setOptions(['male', 'female', 'other']))
->addField(new BooleanField('send_newletter', 'Email me the weekly newsletter'));
}
}
namespace Droath\Project\Command;
use Droath\ConsoleForm\Field\BooleanField;
use Droath\ConsoleForm\Field\SelectField;
use Droath\ConsoleForm\Field\TextField;
use Droath\ConsoleForm\Form;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class Initialize extends Command
{
/**
* {@inheritdoc}
*/
public function configure()
{
$this
->setName('init')
->setDescription('Initialize project config.');
}
/**
* {@inheritdoc}
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$form = $this->getHelper('form')
->getForm($input, $output);
$form->addFields([
(new TextField('project', 'Project name'))
->setDefault('Demo Project'),
(new SelectField('version', 'Project Version'))
->setOptions(['7.x', '8.x'])
->setDefault('8.x'),
]);
$results = $form
->process()
->getResults();
var_dump($results)
}
...
namespace Droath\Project\Command;
use Droath\ConsoleForm\Field\BooleanField;
use Droath\ConsoleForm\Field\SelectField;
use Droath\ConsoleForm\Field\TextField;
use Droath\ConsoleForm\Form;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class Initialize extends Command
{
/**
* {@inheritdoc}
*/
public function configure()
{
$this
->setName('init')
->setDescription('Initialize project config.');
}
/**
* {@inheritdoc}
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$form = $this->getHelper('form')
->getFormByName('project.form.setup', $input, $output);
$results = $form
->process()
->getResults();
var_dump($results)
}
...
...
$form
->addFields([
(new TextField('project', 'Project name'))
->setDefault('Demo Project'),
(new SelectField('version', 'Project Version'))
->setOptions(['7.x', '8.x'])
->setDefault('8.x'),
])
->save(function($results) {
// Save results to filesystem or remote source.
// Don't need to call process() as it's done inside the save method.
});
...
$form->addFields([
(new TextField('name', 'Project Name')),
(new FieldGroup('environments'))
->addFields([
(new TextField('ssh_label', 'SSH Label'))
->setRequired(false),
(new TextField('ssh_host', 'SSH Host'))
->setRequired(false),
(new Textfield('ssh_uri', 'SSH URI'))
->setRequired(false)
])
->setLoopUntil(function($result) {
if (!isset($result['ssh_label'])) {
return false;
}
return true;
})
]);
$results = $form
->process()
->getResults();
...
$form
->addFields([
(new TextField('project', 'Project name'))
->setDefault('Demo Project'),
(new SelectField('version', 'Project Version'))
->setOptions(['7.x', '8.x'])
->setDefault('8.x'),
(new TextField('another_option', 'More options for 7.x version'))
->setCondition('version', '7.x'),
]);
$results = $form
->process()
->getResults();
...
$form
->addFields([
(new BooleanField('questions', 'Ask questions?'))
->setSubform(function ($subform, $value) {
if ($value === true) {
$subform->addFields([
(new TextField('how_old', 'How old are you?')),
(new TextField('location', 'Where do you live?')),
]);
}
}),
]);
$results = $form
->process()
->getResults();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.