├── FormBuilder.php ├── LICENSE ├── README.md ├── Resources └── views │ ├── Form │ ├── attributes.html.php │ ├── button_attributes.html.php │ ├── button_label.html.php │ ├── button_row.html.php │ ├── button_widget.html.php │ ├── checkbox_widget.html.php │ ├── choice_attributes.html.php │ ├── choice_options.html.php │ ├── choice_widget.html.php │ ├── choice_widget_collapsed.html.php │ ├── choice_widget_expanded.html.php │ ├── choice_widget_options.html.php │ ├── collection_widget.html.php │ ├── container_attributes.html.php │ ├── date_widget.html.php │ ├── datetime_widget.html.php │ ├── email_widget.html.php │ ├── form.html.php │ ├── form_enctype.html.php │ ├── form_end.html.php │ ├── form_errors.html.php │ ├── form_label.html.php │ ├── form_rest.html.php │ ├── form_row.html.php │ ├── form_rows.html.php │ ├── form_start.html.php │ ├── form_widget.html.php │ ├── form_widget_compound.html.php │ ├── form_widget_simple.html.php │ ├── hidden_row.html.php │ ├── hidden_widget.html.php │ ├── integer_widget.html.php │ ├── money_widget.html.php │ ├── number_widget.html.php │ ├── password_widget.html.php │ ├── percent_widget.html.php │ ├── radio_widget.html.php │ ├── range_widget.html.php │ ├── repeated_row.html.php │ ├── reset_widget.html.php │ ├── search_widget.html.php │ ├── submit_widget.html.php │ ├── textarea_widget.html.php │ ├── time_widget.html.php │ ├── url_widget.html.php │ ├── widget_attributes.html.php │ └── widget_container_attributes.html.php │ └── FormTable │ ├── button_row.html.php │ ├── form_row.html.php │ ├── form_widget_compound.html.php │ └── hidden_row.html.php ├── Templating ├── FormTemplating.php ├── Helper │ ├── FormHelper.php │ └── TranslatorHelper.php └── TemplateNameParser.php ├── Translation ├── DefaultTranslator.php └── TranslatorInterface.php └── composer.json /FormBuilder.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Symfony\Form\Standalone; 13 | 14 | use Symfony\Component\Form\FormExtensionInterface; 15 | use Symfony\Component\Form\FormTypeExtensionInterface; 16 | use Symfony\Component\Form\Forms; 17 | 18 | /** 19 | * Class FormTemplating 20 | * 21 | * @author Alexander Egurtsov 22 | */ 23 | class FormBuilder 24 | { 25 | /** 26 | * @var \Symfony\Component\Form\FormFactoryBuilderInterface 27 | */ 28 | protected $formFactoryBuilder; 29 | 30 | /** 31 | * @param $className 32 | * @param null $data 33 | * @param array $options 34 | * @return \Symfony\Component\Form\FormInterface 35 | */ 36 | public function create($className, $data = null, $options = []) 37 | { 38 | return $this->getFormFactory()->create($className, $data, $options); 39 | } 40 | 41 | /** 42 | * @return \Symfony\Component\Form\FormFactoryInterface 43 | */ 44 | public function getFormFactory() 45 | { 46 | return $this->getFormFactoryBuilder()->getFormFactory(); 47 | } 48 | 49 | /** 50 | * @return \Symfony\Component\Form\FormFactoryBuilderInterface 51 | */ 52 | public function getFormFactoryBuilder() 53 | { 54 | if (!$this->formFactoryBuilder) { 55 | $this->formFactoryBuilder = Forms::createFormFactoryBuilder(); 56 | } 57 | 58 | return $this->formFactoryBuilder; 59 | } 60 | 61 | /** 62 | * @param FormExtensionInterface $extension 63 | * @return $this 64 | */ 65 | public function addExtension(FormExtensionInterface $extension) 66 | { 67 | $this->getFormFactoryBuilder()->addExtension($extension); 68 | 69 | return $this; 70 | } 71 | 72 | /** 73 | * @param FormTypeExtensionInterface $typeExtension 74 | * @return $this 75 | */ 76 | public function addTypeExtension(FormTypeExtensionInterface $typeExtension) 77 | { 78 | $this->getFormFactoryBuilder()->addTypeExtension($typeExtension); 79 | return $this; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) Alexander Egurtsov 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is furnished 8 | to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Symfony Form Standalone 2 | 3 | This library allows to use [symfony forms](http://symfony.com/doc/current/forms.html) standalone without symfony framework and without pulling its dependencies. 4 | 5 | Current dependencies list: 6 | - symfony/form 7 | - symfony/templating 8 | 9 | Installation 10 | ----------- 11 | ```bash 12 | composer require lostedboy/symfony-form-standalone 13 | ``` 14 | Basic Usage 15 | ----------- 16 | Create form: 17 | ```php 18 | create($formType, $mappedEntity); 28 | 29 | ... 30 | 31 | // create templating handler 32 | $formTemplating = new FormTemplating(); 33 | 34 | // get templating engine 35 | $templating = $formTemplating->getTemplating(); 36 | 37 | // create form view 38 | $formView = $form->createView(); 39 | 40 | // render form partial 41 | echo $templating->render('form.html.php', array('form' => $formView)); 42 | ``` 43 | Render form (see: [Symfony Form Rendering](http://symfony.com/doc/current/form/form_customization.html)) 44 | ```php 45 | // form.html.php 46 | form($form) ?> 47 | ``` 48 | Render customization 49 | ------ 50 | ```php 51 | addYamlMappings([ 99 | // paths to validation files (optional) 100 | __DIR__ . '/config/validation/some_entity.yml' 101 | ]) 102 | ->getValidator(); 103 | 104 | // register extension 105 | $formBuilder->addExtension(new ValidatorExtension($validator); 106 | ``` 107 | 108 | License 109 | ======= 110 | 111 | This bundle is under MIT license 112 | -------------------------------------------------------------------------------- /Resources/views/Form/attributes.html.php: -------------------------------------------------------------------------------- 1 | block($form, 'widget_attributes') ?> 2 | -------------------------------------------------------------------------------- /Resources/views/Form/button_attributes.html.php: -------------------------------------------------------------------------------- 1 | id="escape($id) ?>" name="escape($full_name) ?>" disabled="disabled" 2 | $v): ?> 3 | 4 | escape($k), $view->escape(false !== $translation_domain ? $view['translator']->trans($v, array(), $translation_domain) : $v)) ?> 5 | 6 | escape($k), $view->escape($k)) ?> 7 | 8 | escape($k), $view->escape($v)) ?> 9 | 10 | 11 | -------------------------------------------------------------------------------- /Resources/views/Form/button_label.html.php: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lostedboy/symfony-form-standalone/59760fc35679689d877a231ae46ef66f759e6ac0/Resources/views/Form/button_label.html.php -------------------------------------------------------------------------------- /Resources/views/Form/button_row.html.php: -------------------------------------------------------------------------------- 1 |
2 | widget($form) ?> 3 |
4 | -------------------------------------------------------------------------------- /Resources/views/Form/button_widget.html.php: -------------------------------------------------------------------------------- 1 | $name, '%id%' => $id)) 3 | : $view['form']->humanize($name); } ?> 4 | 5 | -------------------------------------------------------------------------------- /Resources/views/Form/checkbox_widget.html.php: -------------------------------------------------------------------------------- 1 | block($form, 'widget_attributes') ?> 3 | 0): ?> value="escape($value) ?>" 4 | checked="checked" 5 | /> 6 | -------------------------------------------------------------------------------- /Resources/views/Form/choice_attributes.html.php: -------------------------------------------------------------------------------- 1 | disabled="disabled" 2 | $v): ?> 3 | 4 | escape($k), $view->escape($k)) ?> 5 | 6 | escape($k), $view->escape($v)) ?> 7 | 8 | 9 | -------------------------------------------------------------------------------- /Resources/views/Form/choice_options.html.php: -------------------------------------------------------------------------------- 1 | block($form, 'choice_widget_options') ?> 2 | -------------------------------------------------------------------------------- /Resources/views/Form/choice_widget.html.php: -------------------------------------------------------------------------------- 1 | 2 | block($form, 'choice_widget_expanded') ?> 3 | 4 | block($form, 'choice_widget_collapsed') ?> 5 | 6 | -------------------------------------------------------------------------------- /Resources/views/Form/choice_widget_collapsed.html.php: -------------------------------------------------------------------------------- 1 | 19 | -------------------------------------------------------------------------------- /Resources/views/Form/choice_widget_expanded.html.php: -------------------------------------------------------------------------------- 1 |
block($form, 'widget_container_attributes') ?>> 2 | 3 | widget($child) ?> 4 | label($child, null, array('translation_domain' => $choice_translation_domain)) ?> 5 | 6 |
7 | -------------------------------------------------------------------------------- /Resources/views/Form/choice_widget_options.html.php: -------------------------------------------------------------------------------- 1 | 4 | 5 | $choice): ?> 6 | 7 | 8 | block($form, 'choice_widget_options', array('choices' => $choice)) ?> 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /Resources/views/Form/collection_widget.html.php: -------------------------------------------------------------------------------- 1 | 2 | escape($view['form']->row($prototype)) ?> 3 | 4 | widget($form, array('attr' => $attr)) ?> 5 | -------------------------------------------------------------------------------- /Resources/views/Form/container_attributes.html.php: -------------------------------------------------------------------------------- 1 | block($form, 'widget_container_attributes') ?> 2 | -------------------------------------------------------------------------------- /Resources/views/Form/date_widget.html.php: -------------------------------------------------------------------------------- 1 | 2 | block($form, 'form_widget_simple'); ?> 3 | 4 |
block($form, 'widget_container_attributes') ?>> 5 | widget($form['year']), 7 | $view['form']->widget($form['month']), 8 | $view['form']->widget($form['day']), 9 | ), $date_pattern) ?> 10 |
11 | 12 | -------------------------------------------------------------------------------- /Resources/views/Form/datetime_widget.html.php: -------------------------------------------------------------------------------- 1 | 2 | block($form, 'form_widget_simple'); ?> 3 | 4 |
block($form, 'widget_container_attributes') ?>> 5 | widget($form['date']).' '.$view['form']->widget($form['time']) ?> 6 |
7 | 8 | -------------------------------------------------------------------------------- /Resources/views/Form/email_widget.html.php: -------------------------------------------------------------------------------- 1 | block($form, 'form_widget_simple', array('type' => isset($type) ? $type : 'email')) ?> 2 | -------------------------------------------------------------------------------- /Resources/views/Form/form.html.php: -------------------------------------------------------------------------------- 1 | start($form) ?> 2 | widget($form) ?> 3 | end($form) ?> 4 | -------------------------------------------------------------------------------- /Resources/views/Form/form_enctype.html.php: -------------------------------------------------------------------------------- 1 | vars['multipart']): ?>enctype="multipart/form-data" 2 | -------------------------------------------------------------------------------- /Resources/views/Form/form_end.html.php: -------------------------------------------------------------------------------- 1 | 2 | rest($form) ?> 3 | 4 | 5 | -------------------------------------------------------------------------------- /Resources/views/Form/form_errors.html.php: -------------------------------------------------------------------------------- 1 | 0): ?> 2 | 7 | 8 | -------------------------------------------------------------------------------- /Resources/views/Form/form_label.html.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | $name, '%id%' => $id)) 6 | : $view['form']->humanize($name); } ?> 7 | 8 | 9 | -------------------------------------------------------------------------------- /Resources/views/Form/form_rest.html.php: -------------------------------------------------------------------------------- 1 | 2 | isRendered()): ?> 3 | row($child) ?> 4 | 5 | 6 | -------------------------------------------------------------------------------- /Resources/views/Form/form_row.html.php: -------------------------------------------------------------------------------- 1 |
2 | label($form) ?> 3 | errors($form) ?> 4 | widget($form) ?> 5 |
6 | -------------------------------------------------------------------------------- /Resources/views/Form/form_rows.html.php: -------------------------------------------------------------------------------- 1 | 2 | row($child) ?> 3 | 4 | -------------------------------------------------------------------------------- /Resources/views/Form/form_start.html.php: -------------------------------------------------------------------------------- 1 | 2 | 3 |
action="" $v) { printf(' %s="%s"', $view->escape($k), $view->escape($v)); } ?> enctype="multipart/form-data"> 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Resources/views/Form/form_widget.html.php: -------------------------------------------------------------------------------- 1 | 2 | block($form, 'form_widget_compound')?> 3 | 4 | block($form, 'form_widget_simple')?> 5 | 6 | -------------------------------------------------------------------------------- /Resources/views/Form/form_widget_compound.html.php: -------------------------------------------------------------------------------- 1 |
block($form, 'widget_container_attributes') ?>> 2 | parent && $errors): ?> 3 | errors($form) ?> 4 | 5 | block($form, 'form_rows') ?> 6 | rest($form) ?> 7 |
8 | -------------------------------------------------------------------------------- /Resources/views/Form/form_widget_simple.html.php: -------------------------------------------------------------------------------- 1 | block($form, 'widget_attributes') ?> value="escape($value) ?>" /> 2 | -------------------------------------------------------------------------------- /Resources/views/Form/hidden_row.html.php: -------------------------------------------------------------------------------- 1 | widget($form) ?> 2 | -------------------------------------------------------------------------------- /Resources/views/Form/hidden_widget.html.php: -------------------------------------------------------------------------------- 1 | block($form, 'form_widget_simple', array('type' => isset($type) ? $type : 'hidden')) ?> 2 | -------------------------------------------------------------------------------- /Resources/views/Form/integer_widget.html.php: -------------------------------------------------------------------------------- 1 | block($form, 'form_widget_simple', array('type' => isset($type) ? $type : 'number')) ?> 2 | -------------------------------------------------------------------------------- /Resources/views/Form/money_widget.html.php: -------------------------------------------------------------------------------- 1 | block($form, 'form_widget_simple'), $money_pattern) ?> 2 | -------------------------------------------------------------------------------- /Resources/views/Form/number_widget.html.php: -------------------------------------------------------------------------------- 1 | block($form, 'form_widget_simple', array('type' => isset($type) ? $type : 'text')) ?> 2 | -------------------------------------------------------------------------------- /Resources/views/Form/password_widget.html.php: -------------------------------------------------------------------------------- 1 | block($form, 'form_widget_simple', array('type' => isset($type) ? $type : 'password')) ?> 2 | -------------------------------------------------------------------------------- /Resources/views/Form/percent_widget.html.php: -------------------------------------------------------------------------------- 1 | block($form, 'form_widget_simple', array('type' => isset($type) ? $type : 'text')) ?> % 2 | -------------------------------------------------------------------------------- /Resources/views/Form/radio_widget.html.php: -------------------------------------------------------------------------------- 1 | block($form, 'widget_attributes') ?> 3 | value="escape($value) ?>" 4 | checked="checked" 5 | /> 6 | -------------------------------------------------------------------------------- /Resources/views/Form/range_widget.html.php: -------------------------------------------------------------------------------- 1 | block($form, 'form_widget_simple', array('type' => isset($type) ? $type : 'range')); 2 | -------------------------------------------------------------------------------- /Resources/views/Form/repeated_row.html.php: -------------------------------------------------------------------------------- 1 | block($form, 'form_rows') ?> 2 | -------------------------------------------------------------------------------- /Resources/views/Form/reset_widget.html.php: -------------------------------------------------------------------------------- 1 | block($form, 'button_widget', array('type' => isset($type) ? $type : 'reset')) ?> 2 | -------------------------------------------------------------------------------- /Resources/views/Form/search_widget.html.php: -------------------------------------------------------------------------------- 1 | block($form, 'form_widget_simple', array('type' => isset($type) ? $type : 'search')) ?> 2 | -------------------------------------------------------------------------------- /Resources/views/Form/submit_widget.html.php: -------------------------------------------------------------------------------- 1 | block($form, 'button_widget', array('type' => isset($type) ? $type : 'submit')) ?> 2 | -------------------------------------------------------------------------------- /Resources/views/Form/textarea_widget.html.php: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Resources/views/Form/time_widget.html.php: -------------------------------------------------------------------------------- 1 | 2 | block($form, 'form_widget_simple'); ?> 3 | 4 | array('size' => 1)) : array() ?> 5 |
block($form, 'widget_container_attributes') ?>> 6 | widget($form['hour'], $vars); 10 | 11 | if ($with_minutes) { 12 | echo ':'; 13 | echo $view['form']->widget($form['minute'], $vars); 14 | } 15 | 16 | if ($with_seconds) { 17 | echo ':'; 18 | echo $view['form']->widget($form['second'], $vars); 19 | } 20 | ?> 21 |
22 | 23 | -------------------------------------------------------------------------------- /Resources/views/Form/url_widget.html.php: -------------------------------------------------------------------------------- 1 | block($form, 'form_widget_simple', array('type' => isset($type) ? $type : 'url')) ?> 2 | -------------------------------------------------------------------------------- /Resources/views/Form/widget_attributes.html.php: -------------------------------------------------------------------------------- 1 | id="escape($id) ?>" name="escape($full_name) ?>" disabled="disabled" 2 | required="required" 3 | $v): ?> 4 | 5 | escape($k), $view->escape(false !== $translation_domain ? $view['translator']->trans($v, array(), $translation_domain) : $v)) ?> 6 | 7 | escape($k), $view->escape($k)) ?> 8 | 9 | escape($k), $view->escape($v)) ?> 10 | 11 | 12 | -------------------------------------------------------------------------------- /Resources/views/Form/widget_container_attributes.html.php: -------------------------------------------------------------------------------- 1 | id="escape($id) ?>" 2 | $v): ?> 3 | 4 | escape($k), $view->escape(false !== $translation_domain ? $view['translator']->trans($v, array(), $translation_domain) : $v)) ?> 5 | 6 | escape($k), $view->escape($k)) ?> 7 | 8 | escape($k), $view->escape($v)) ?> 9 | 10 | 11 | -------------------------------------------------------------------------------- /Resources/views/FormTable/button_row.html.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | widget($form) ?> 5 | 6 | 7 | -------------------------------------------------------------------------------- /Resources/views/FormTable/form_row.html.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | label($form) ?> 4 | 5 | 6 | errors($form) ?> 7 | widget($form) ?> 8 | 9 | 10 | -------------------------------------------------------------------------------- /Resources/views/FormTable/form_widget_compound.html.php: -------------------------------------------------------------------------------- 1 | block($form, 'widget_container_attributes') ?>> 2 | parent && $errors): ?> 3 | 4 | 7 | 8 | 9 | block($form, 'form_rows') ?> 10 | rest($form) ?> 11 |
5 | errors($form) ?> 6 |
12 | -------------------------------------------------------------------------------- /Resources/views/FormTable/hidden_row.html.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | widget($form) ?> 4 | 5 | 6 | -------------------------------------------------------------------------------- /Templating/FormTemplating.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Symfony\Form\Standalone\Templating; 13 | 14 | use Symfony\Component\Templating\PhpEngine; 15 | use Symfony\Component\Templating\Loader\FilesystemLoader; 16 | use Symfony\Component\Form\Extension\Templating\TemplatingRendererEngine; 17 | use Symfony\Component\Form\FormRenderer; 18 | use Symfony\Form\Standalone\Templating\Helper\FormHelper; 19 | use Symfony\Form\Standalone\Templating\Helper\TranslatorHelper; 20 | use Symfony\Form\Standalone\Translation\DefaultTranslator; 21 | use Symfony\Form\Standalone\Translation\TranslatorInterface; 22 | 23 | /** 24 | * Class FormTemplating 25 | * 26 | * @author Alexander Egurtsov 27 | */ 28 | class FormTemplating 29 | { 30 | /** 31 | * @var array 32 | */ 33 | protected $themePaths = [ 34 | __DIR__ . '/../Resources/views/Form' 35 | ]; 36 | 37 | protected $loadPaths = [ 38 | __DIR__ . '/../Resources/views/Form' 39 | ]; 40 | 41 | /** 42 | * @var TranslatorHelper 43 | */ 44 | protected $translationHelper; 45 | 46 | /** 47 | * @var PhpEngine 48 | */ 49 | protected $templating; 50 | 51 | protected $translator; 52 | 53 | /** 54 | * FormTemplating constructor. 55 | * @param array $loadPaths 56 | * @param array $themePaths 57 | * @param TranslatorInterface $translator 58 | */ 59 | public function __construct( 60 | array $loadPaths = [], 61 | array $themePaths = [], 62 | TranslatorInterface $translator = null 63 | ) { 64 | $this->themePaths = array_merge($this->themePaths, $themePaths); 65 | $this->loadPaths = array_merge($this->loadPaths, $loadPaths); 66 | 67 | $this->setTranslator($translator); 68 | $this->createTranslatorHelper(); 69 | $this->createTemplating(); 70 | } 71 | 72 | protected function createTemplating() 73 | { 74 | $templating = new PhpEngine(new TemplateNameParser(), $this->getLoader()); 75 | 76 | $templating->setHelpers([ 77 | $this->getFormHelper($templating), 78 | $this->getTranslationHelper() 79 | ]); 80 | 81 | $this->templating = $templating; 82 | } 83 | 84 | /** 85 | * @return TranslatorHelper 86 | */ 87 | protected function createTranslatorHelper() 88 | { 89 | $this->translationHelper = new TranslatorHelper($this->getTranslator()); 90 | } 91 | 92 | /** 93 | * @param PhpEngine $templating 94 | * @return FormHelper 95 | */ 96 | protected function getFormHelper(PhpEngine $templating) 97 | { 98 | return new FormHelper(new FormRenderer($this->getRendererEngine($templating))); 99 | } 100 | 101 | /** 102 | * @return FilesystemLoader 103 | */ 104 | protected function getLoader() 105 | { 106 | return new FilesystemLoader(array_map(function ($path) { 107 | return $path . '/%name%'; 108 | }, $this->loadPaths)); 109 | } 110 | 111 | /** 112 | * @param PhpEngine $templating 113 | * @return TemplatingRendererEngine 114 | */ 115 | protected function getRendererEngine(PhpEngine $templating) 116 | { 117 | return new TemplatingRendererEngine($templating, $this->themePaths); 118 | } 119 | 120 | /** 121 | * @return TranslatorHelper 122 | */ 123 | protected function getTranslationHelper() 124 | { 125 | return $this->translationHelper; 126 | } 127 | 128 | /** 129 | * @return PhpEngine 130 | */ 131 | public function getTemplating() 132 | { 133 | return $this->templating; 134 | } 135 | 136 | /** 137 | * @return mixed 138 | */ 139 | public function getTranslator() 140 | { 141 | if (!$this->translator) { 142 | return new DefaultTranslator(); 143 | } 144 | 145 | return $this->translator; 146 | } 147 | 148 | /** 149 | * @param mixed $translator 150 | */ 151 | public function setTranslator($translator) 152 | { 153 | $this->translator = $translator; 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /Templating/Helper/FormHelper.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Symfony\Form\Standalone\Templating\Helper; 13 | 14 | use Symfony\Component\Templating\Helper\Helper; 15 | use Symfony\Component\Form\FormRendererInterface; 16 | use Symfony\Component\Form\FormView; 17 | 18 | /** 19 | * FormHelper provides helpers to help display forms. 20 | * 21 | * @author Fabien Potencier 22 | * @author Bernhard Schussek 23 | */ 24 | class FormHelper extends Helper 25 | { 26 | /** 27 | * @var FormRendererInterface 28 | */ 29 | private $renderer; 30 | 31 | /** 32 | * @param FormRendererInterface $renderer 33 | */ 34 | public function __construct(FormRendererInterface $renderer) 35 | { 36 | $this->renderer = $renderer; 37 | } 38 | 39 | /** 40 | * {@inheritdoc} 41 | */ 42 | public function getName() 43 | { 44 | return 'form'; 45 | } 46 | 47 | /** 48 | * Sets a theme for a given view. 49 | * 50 | * The theme format is ":". 51 | * 52 | * @param FormView $view A FormView instance 53 | * @param string|array $themes A theme or an array of theme 54 | */ 55 | public function setTheme(FormView $view, $themes) 56 | { 57 | $this->renderer->setTheme($view, $themes); 58 | } 59 | 60 | /** 61 | * Renders the HTML for a form. 62 | * 63 | * Example usage: 64 | * 65 | * form($form) ?> 66 | * 67 | * You can pass options during the call: 68 | * 69 | * form($form, array('attr' => array('class' => 'foo'))) ?> 70 | * 71 | * form($form, array('separator' => '+++++')) ?> 72 | * 73 | * This method is mainly intended for prototyping purposes. If you want to 74 | * control the layout of a form in a more fine-grained manner, you are 75 | * advised to use the other helper methods for rendering the parts of the 76 | * form individually. You can also create a custom form theme to adapt 77 | * the look of the form. 78 | * 79 | * @param FormView $view The view for which to render the form 80 | * @param array $variables Additional variables passed to the template 81 | * 82 | * @return string The HTML markup 83 | */ 84 | public function form(FormView $view, array $variables = array()) 85 | { 86 | return $this->renderer->renderBlock($view, 'form', $variables); 87 | } 88 | 89 | /** 90 | * Renders the form start tag. 91 | * 92 | * Example usage templates: 93 | * 94 | * start($form) ?>> 95 | * 96 | * @param FormView $view The view for which to render the start tag 97 | * @param array $variables Additional variables passed to the template 98 | * 99 | * @return string The HTML markup 100 | */ 101 | public function start(FormView $view, array $variables = array()) 102 | { 103 | return $this->renderer->renderBlock($view, 'form_start', $variables); 104 | } 105 | 106 | /** 107 | * Renders the form end tag. 108 | * 109 | * Example usage templates: 110 | * 111 | * end($form) ?>> 112 | * 113 | * @param FormView $view The view for which to render the end tag 114 | * @param array $variables Additional variables passed to the template 115 | * 116 | * @return string The HTML markup 117 | */ 118 | public function end(FormView $view, array $variables = array()) 119 | { 120 | return $this->renderer->renderBlock($view, 'form_end', $variables); 121 | } 122 | 123 | /** 124 | * Renders the HTML for a given view. 125 | * 126 | * Example usage: 127 | * 128 | * widget($form) ?> 129 | * 130 | * You can pass options during the call: 131 | * 132 | * widget($form, array('attr' => array('class' => 'foo'))) ?> 133 | * 134 | * widget($form, array('separator' => '+++++')) ?> 135 | * 136 | * @param FormView $view The view for which to render the widget 137 | * @param array $variables Additional variables passed to the template 138 | * 139 | * @return string The HTML markup 140 | */ 141 | public function widget(FormView $view, array $variables = array()) 142 | { 143 | return $this->renderer->searchAndRenderBlock($view, 'widget', $variables); 144 | } 145 | 146 | /** 147 | * Renders the entire form field "row". 148 | * 149 | * @param FormView $view The view for which to render the row 150 | * @param array $variables Additional variables passed to the template 151 | * 152 | * @return string The HTML markup 153 | */ 154 | public function row(FormView $view, array $variables = array()) 155 | { 156 | return $this->renderer->searchAndRenderBlock($view, 'row', $variables); 157 | } 158 | 159 | /** 160 | * Renders the label of the given view. 161 | * 162 | * @param FormView $view The view for which to render the label 163 | * @param string $label The label 164 | * @param array $variables Additional variables passed to the template 165 | * 166 | * @return string The HTML markup 167 | */ 168 | public function label(FormView $view, $label = null, array $variables = array()) 169 | { 170 | if (null !== $label) { 171 | $variables += array('label' => $label); 172 | } 173 | 174 | return $this->renderer->searchAndRenderBlock($view, 'label', $variables); 175 | } 176 | 177 | /** 178 | * Renders the errors of the given view. 179 | * 180 | * @param FormView $view The view to render the errors for 181 | * 182 | * @return string The HTML markup 183 | */ 184 | public function errors(FormView $view) 185 | { 186 | return $this->renderer->searchAndRenderBlock($view, 'errors'); 187 | } 188 | 189 | /** 190 | * Renders views which have not already been rendered. 191 | * 192 | * @param FormView $view The parent view 193 | * @param array $variables An array of variables 194 | * 195 | * @return string The HTML markup 196 | */ 197 | public function rest(FormView $view, array $variables = array()) 198 | { 199 | return $this->renderer->searchAndRenderBlock($view, 'rest', $variables); 200 | } 201 | 202 | /** 203 | * Renders a block of the template. 204 | * 205 | * @param FormView $view The view for determining the used themes 206 | * @param string $blockName The name of the block to render 207 | * @param array $variables The variable to pass to the template 208 | * 209 | * @return string The HTML markup 210 | */ 211 | public function block(FormView $view, $blockName, array $variables = array()) 212 | { 213 | return $this->renderer->renderBlock($view, $blockName, $variables); 214 | } 215 | 216 | /** 217 | * Returns a CSRF token. 218 | * 219 | * Use this helper for CSRF protection without the overhead of creating a 220 | * form. 221 | * 222 | * 223 | * echo $view['form']->csrfToken('rm_user_'.$user->getId()); 224 | * 225 | * 226 | * Check the token in your action using the same CSRF token id. 227 | * 228 | * 229 | * $csrfProvider = $this->get('security.csrf.token_generator'); 230 | * if (!$csrfProvider->isCsrfTokenValid('rm_user_'.$user->getId(), $token)) { 231 | * throw new \RuntimeException('CSRF attack detected.'); 232 | * } 233 | * 234 | * 235 | * @param string $tokenId The CSRF token id of the protected action 236 | * 237 | * @return string A CSRF token 238 | * 239 | * @throws \BadMethodCallException When no CSRF provider was injected in the constructor. 240 | */ 241 | public function csrfToken($tokenId) 242 | { 243 | return $this->renderer->renderCsrfToken($tokenId); 244 | } 245 | 246 | public function humanize($text) 247 | { 248 | return $this->renderer->humanize($text); 249 | } 250 | } 251 | -------------------------------------------------------------------------------- /Templating/Helper/TranslatorHelper.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Symfony\Form\Standalone\Templating\Helper; 13 | 14 | use Symfony\Component\Templating\Helper\Helper; 15 | use Symfony\Form\Standalone\Translation\TranslatorInterface; 16 | 17 | /** 18 | * Class FormTemplating 19 | * 20 | * @author Alexander Egurtsov 21 | */ 22 | class TranslatorHelper extends Helper 23 | { 24 | /** 25 | * @var TranslatorInterface 26 | */ 27 | protected $translator; 28 | 29 | /** 30 | * Constructor. 31 | * 32 | * @param TranslatorInterface $translator A TranslatorInterface instance 33 | */ 34 | public function __construct(TranslatorInterface $translator) 35 | { 36 | $this->translator = $translator; 37 | } 38 | 39 | /** 40 | * @see TranslatorInterface::trans() 41 | */ 42 | public function trans($id, array $parameters = array(), $domain = 'messages', $locale = null) 43 | { 44 | return $this->translator->trans($id, $parameters, $domain, $locale); 45 | } 46 | 47 | /** 48 | * @see TranslatorInterface::transChoice() 49 | */ 50 | public function transChoice($id, $number, array $parameters = array(), $domain = 'messages', $locale = null) 51 | { 52 | return $this->translator->transChoice($id, $number, $parameters, $domain, $locale); 53 | } 54 | 55 | /** 56 | * {@inheritdoc} 57 | */ 58 | public function getName() 59 | { 60 | return 'translator'; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Templating/TemplateNameParser.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Symfony\Form\Standalone\Templating; 13 | 14 | use Symfony\Component\Templating\TemplateNameParserInterface; 15 | use Symfony\Component\Templating\TemplateReference; 16 | use Symfony\Component\Templating\TemplateReferenceInterface; 17 | 18 | /** 19 | * Class TemplateNameParser 20 | * 21 | * @author Alexander Egurtsov 22 | */ 23 | class TemplateNameParser implements TemplateNameParserInterface 24 | { 25 | /** 26 | * {@inheritdoc} 27 | * 28 | * @api 29 | */ 30 | public function parse($name) 31 | { 32 | if ($name instanceof TemplateReferenceInterface) { 33 | return $name; 34 | } 35 | 36 | $formattedPath = str_replace(':', ':', $name, $count); 37 | 38 | if ($count >= 2) { 39 | $pathList = explode(':', $name); 40 | $formattedPath = ''; 41 | 42 | for ($i = 0; $i < sizeof($pathList); $i++) { 43 | if ($i == 0) { 44 | $formattedPath = $pathList[$i] . ':'; 45 | } else { 46 | if ($i !== sizeof($pathList) - 1) { 47 | $formattedPath .= $pathList[$i] . '/'; 48 | } else { 49 | $formattedPath .= $pathList[$i]; 50 | } 51 | } 52 | } 53 | 54 | } else { 55 | $formattedPath = str_replace(':', '/', $formattedPath); 56 | } 57 | 58 | $engine = null; 59 | 60 | if (false !== $pos = strrpos($formattedPath, '.')) { 61 | $engine = substr($formattedPath, $pos + 1); 62 | } 63 | 64 | return new TemplateReference($formattedPath, $engine); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /Translation/DefaultTranslator.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Symfony\Form\Standalone\Translation; 13 | 14 | /** 15 | * Class DefaultTranslator 16 | * 17 | * @author Alexander Egurtsov 18 | */ 19 | class DefaultTranslator implements TranslatorInterface 20 | { 21 | protected $locale; 22 | 23 | /** 24 | * @inheritdoc 25 | */ 26 | public function trans($id, array $parameters = array(), $domain = null, $locale = null) 27 | { 28 | return $id; 29 | } 30 | 31 | /** 32 | * @inheritdoc 33 | */ 34 | public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null) 35 | { 36 | return $id; 37 | } 38 | 39 | /** 40 | * @inheritdoc 41 | */ 42 | public function setLocale($locale) 43 | { 44 | $this->locale = $locale; 45 | } 46 | 47 | /** 48 | * @inheritdoc 49 | */ 50 | public function getLocale() 51 | { 52 | return $this->locale; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Translation/TranslatorInterface.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Symfony\Form\Standalone\Translation; 13 | 14 | /** 15 | * TranslatorInterface. 16 | * 17 | * @author Fabien Potencier 18 | */ 19 | interface TranslatorInterface 20 | { 21 | /** 22 | * Translates the given message. 23 | * 24 | * @param string $id The message id (may also be an object that can be cast to string) 25 | * @param array $parameters An array of parameters for the message 26 | * @param string|null $domain The domain for the message or null to use the default 27 | * @param string|null $locale The locale or null to use the default 28 | * 29 | * @return string The translated string 30 | * 31 | */ 32 | public function trans($id, array $parameters = array(), $domain = null, $locale = null); 33 | 34 | /** 35 | * Translates the given choice message by choosing a translation according to a number. 36 | * 37 | * @param string $id The message id (may also be an object that can be cast to string) 38 | * @param int $number The number to use to find the indice of the message 39 | * @param array $parameters An array of parameters for the message 40 | * @param string|null $domain The domain for the message or null to use the default 41 | * @param string|null $locale The locale or null to use the default 42 | * 43 | * @return string The translated string 44 | * 45 | */ 46 | public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null); 47 | 48 | /** 49 | * Sets the current locale. 50 | * 51 | * @param string $locale The locale 52 | * 53 | */ 54 | public function setLocale($locale); 55 | 56 | /** 57 | * Returns the current locale. 58 | * 59 | * @return string The locale 60 | */ 61 | public function getLocale(); 62 | } 63 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "lostedboy/symfony-form-standalone", 3 | "homepage": "https://github.com/lostedboy/symfony-form-standalone", 4 | "description": "Symfony forms wrapper for standalone usage", 5 | "authors": [ 6 | { 7 | "name": "Alexander Egurtsov", 8 | "email": "egurtsov@gmail.com", 9 | "homepage": "https://github.com/lostedboy" 10 | } 11 | ], 12 | "license": "MIT", 13 | "autoload": { 14 | "psr-4": { 15 | "Symfony\\Form\\Standalone\\": "/" 16 | } 17 | }, 18 | "require": { 19 | "symfony/form": "^3.2 || ^4.0", 20 | "symfony/templating": "^3.2 || ^4.0" 21 | } 22 | } 23 | 24 | --------------------------------------------------------------------------------