forked from genemu/GenemuFormBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request genemu#52 from kusmierz/2.0
Sync with 2.0 branch
- Loading branch information
Showing
10 changed files
with
417 additions
and
6 deletions.
There are no files selected for viewing
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,80 @@ | ||
<?php | ||
|
||
namespace Genemu\Bundle\FormBundle\Form\Core\Type; | ||
|
||
use Symfony\Component\Form\FormBuilder; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Component\Form\FormView; | ||
|
||
/** | ||
* A Form type that just renders the field as a p tag. This is useful for forms where certain field | ||
* need to be shown but not editable. | ||
* | ||
* @author Adam Kuśmierz <adam@kusmierz.be> | ||
*/ | ||
class PlainType extends AbstractType | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getDefaultOptions(array $options) | ||
{ | ||
$defaultOptions = array( | ||
'widget' => 'field', | ||
'configs' => array(), | ||
'read_only' => true, | ||
'attr' => array( | ||
'class' => $this->getName() | ||
) | ||
//'property_path' => false, | ||
); | ||
|
||
return array_replace_recursive($defaultOptions, $options); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildView(FormView $view, FormInterface $form) | ||
{ | ||
$value = $form->getClientData(); | ||
|
||
// set string representation | ||
if (true === $value) { | ||
$value = 'true'; | ||
} else if (false === $value) { | ||
$value = 'false'; | ||
} else if (null === $value) { | ||
$value = 'null'; | ||
} else if (is_array($value)) { | ||
$value = implode(', ', $value); | ||
} else if ($value instanceof \DateTime) { | ||
$value = $value->format('Y-m-d H:i:s'); | ||
} else if (is_object($value)) { | ||
if (method_exists($value, '__toString')) { | ||
$value = $value->__toString(); | ||
} else { | ||
$value = get_class($value); | ||
} | ||
} | ||
|
||
$view->set('value', (string) $value); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getParent(array $options) | ||
{ | ||
return 'field'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return 'genemu_plain'; | ||
} | ||
} |
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,174 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Olivier Chauvel <olivier@generation-multiple.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Genemu\Bundle\FormBundle\Form\JQuery\Type; | ||
|
||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\FormBuilder; | ||
use Symfony\Component\Form\FormView; | ||
use Symfony\Component\Form\FormInterface; | ||
|
||
use Genemu\Bundle\FormBundle\Form\Core\ChoiceList\AjaxArrayChoiceList; | ||
use Genemu\Bundle\FormBundle\Form\Core\DataTransformer\ChoiceToJsonTransformer; | ||
|
||
/** | ||
* @author Adam Kuśmierz <adam@kusmierz.be> | ||
*/ | ||
class TokeninputType extends AbstractType | ||
{ | ||
/** | ||
* Available options to set | ||
* | ||
* @var array | ||
*/ | ||
protected $_availableTokeninputOptions = array( | ||
'method', | ||
'queryParam', | ||
'searchDelay', | ||
'minChars', | ||
'propertyToSearch', | ||
'jsonContainer', | ||
'crossDomain', | ||
'prePopulate', | ||
'hintText', | ||
'noResultsText', | ||
'searchingText', | ||
'deleteText', | ||
'animateDropdown', | ||
'theme', | ||
'resultsFormatter', | ||
'tokenFormatter', | ||
'tokenLimit', | ||
'tokenDelimiter', | ||
'preventDuplicates', | ||
'tokenValue' | ||
); | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildForm(FormBuilder $builder, array $options) | ||
{ | ||
if (true === empty($options['choice_list'])) { | ||
$options['choice_list'] = new AjaxArrayChoiceList($options['choices'], $options['ajax']); | ||
} | ||
|
||
$builder | ||
->appendClientTransformer(new ChoiceToJsonTransformer( | ||
$options['choice_list'], | ||
$options['widget'], | ||
$options['multiple'], | ||
$options['ajax'] | ||
)) | ||
->setAttribute('choice_list', $options['choice_list']) | ||
->setAttribute('widget', $options['widget']) | ||
->setAttribute('route_name', $options['route_name']); | ||
|
||
foreach ($this->_availableTokeninputOptions as $option) { | ||
if (isset($options[$option])) { | ||
$builder->setAttribute($option, $options[$option]); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildView(FormView $view, FormInterface $form) | ||
{ | ||
$datas = json_decode($form->getClientData(), true); | ||
$value = ''; | ||
|
||
if (false === empty($datas)) { | ||
if (true === $form->getAttribute('multiple')) { | ||
foreach ($datas as $data) { | ||
$value .= $data['label'] . ', '; | ||
} | ||
} else { | ||
$value = $datas['label']; | ||
} | ||
} | ||
|
||
$view | ||
->set('tokeninput_value', $value) | ||
->set('route_name', $form->getAttribute('route_name')); | ||
|
||
foreach ($this->_availableTokeninputOptions as $option) { | ||
if ($form->hasAttribute($option)) { | ||
$view->set($option, $form->getAttribute($option)); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getDefaultOptions(array $options) | ||
{ | ||
$defaultOptions = array( | ||
'widget' => 'choice', | ||
'route_name' => null, | ||
'ajax' => false, | ||
'multiple' => true, | ||
'queryParam' => 'term', | ||
'preventDuplicates' => true, | ||
'tokenValue' => 'value', | ||
'propertyToSearch' => 'label', | ||
'theme' => 'facebook' | ||
); | ||
|
||
if (false === empty($options['route_name'])) { | ||
$options['ajax'] = true; | ||
} | ||
|
||
$options = array_replace($defaultOptions, $options); | ||
|
||
return $options; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getAllowedOptionValues(array $options) | ||
{ | ||
return array( | ||
'widget' => array( | ||
'choice', | ||
'language', | ||
'country', | ||
'locale', | ||
'entity', | ||
'document', | ||
'model', | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getParent(array $options) | ||
{ | ||
if (true === in_array($options['widget'], array('entity', 'document', 'model'), true)) { | ||
return 'genemu_ajax' . $options['widget']; | ||
} | ||
|
||
return $options['widget']; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return 'genemu_jquerytokeninput'; | ||
} | ||
} |
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
Oops, something went wrong.