-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #239 add a custom form field type example (yceruto)
This PR was merged into the master branch. Discussion ---------- add a custom form field type example **The first purpose** of this PR is to provide a practical example of how to create a custom form field type. **The second purpose** is to improve the user interface to the field "Published At" on edit a Post, using a JavaScript date library like bootstrap-datetimepicker for parsing, validating, manipulating, and formatting dates. http://eonasdan.github.io/bootstrap-datetimepicker. >**Important:** Is not the purpose of this PR create a custom form field type to support all options and features of the datetimepicker plugin. References: * http://symfony.com/doc/current/cookbook/create_custom_field_type.html * http://symfony.com/doc/current/best_practices/forms.html#custom-form-field-types Main Topics: * Defining the custom form Field Type. * Creating your form Field Type as a Service. * Creating a Template for the custom form field Type * Configuring the custom form template. * Initializing datetime picker JavaScript plugin. * Using the form Field Type into PostType form. Others Topics (refactor): * Moving the initialization of the highligth JavaScript plugin for `main.js` file. **Preview:** ![datetimepicker](https://cloud.githubusercontent.com/assets/2028198/10723013/d1930664-7b8c-11e5-9409-ec8b369a1c2f.png) Commits ------- d8376ea add a custom form field type example
- Loading branch information
Showing
13 changed files
with
283 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{# | ||
Each field type is rendered by a template fragment, which is determined | ||
by the value of your getName() method and the suffix _widget. | ||
See http://symfony.com/doc/current/cookbook/form/create_custom_field_type.html#creating-a-template-for-the-field | ||
#} | ||
|
||
{% block app_datetimepicker_widget %} | ||
{% spaceless %} | ||
<div class="input-group date" data-toggle="datetimepicker"> | ||
{{ block('datetime_widget') }} | ||
<span class="input-group-addon"> | ||
<span class="fa fa-calendar"></span> | ||
</span> | ||
</div> | ||
{% endspaceless %} | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace AppBundle\Form\Type; | ||
|
||
use AppBundle\Utils\MomentFormatConverter; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Component\Form\FormView; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
/** | ||
* Defines the custom form field type used to manipulate datetime values across | ||
* Bootstrap Date\Time Picker javascript plugin. | ||
* See http://symfony.com/doc/current/cookbook/create_custom_field_type.html | ||
* | ||
* @author Yonel Ceruto <yonelceruto@gmail.com> | ||
*/ | ||
class DateTimePickerType extends AbstractType | ||
{ | ||
/** | ||
* @var MomentFormatConverter | ||
*/ | ||
private $formatConverter; | ||
|
||
public function __construct() | ||
{ | ||
$this->formatConverter = new MomentFormatConverter(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildView(FormView $view, FormInterface $form, array $options) | ||
{ | ||
$view->vars['attr']['data-date-format'] = $this->formatConverter->convert($options['format']);; | ||
$view->vars['attr']['data-date-locale'] = \Locale::getDefault(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function configureOptions(OptionsResolver $resolver) | ||
{ | ||
$resolver->setDefaults(array( | ||
'widget' => 'single_text', | ||
)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getParent() | ||
{ | ||
return 'datetime'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return 'app_datetimepicker'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace AppBundle\Utils; | ||
|
||
/** | ||
* This class is used to convert PHP date format to moment.js format | ||
* | ||
* @author Yonel Ceruto <yonelceruto@gmail.com> | ||
*/ | ||
class MomentFormatConverter | ||
{ | ||
/** | ||
* This defines the mapping between PHP ICU date format (key) and moment.js date format (value) | ||
* For ICU formats see http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax | ||
* For Moment formats see http://momentjs.com/docs/#/displaying/format/ | ||
* | ||
* @var array | ||
*/ | ||
private static $formatConvertRules = array( | ||
// year | ||
'yyyy' => 'YYYY', 'yy' => 'YY', 'y' => 'YYYY', | ||
// day | ||
'dd' => 'DD', 'd' => 'D', | ||
// day of week | ||
'EE' => 'ddd', 'EEEEEE' => 'dd', | ||
// timezone | ||
'ZZZZZ' => 'Z', 'ZZZ' => 'ZZ', | ||
// letter 'T' | ||
'\'T\'' => 'T', | ||
); | ||
|
||
/** | ||
* Returns associated moment.js format. | ||
* | ||
* @param string $format PHP Date format | ||
* | ||
* @return string | ||
*/ | ||
public function convert($format) | ||
{ | ||
return strtr($format, self::$formatConvertRules); | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.