Created
June 8, 2017 09:40
-
-
Save evgeniy1204/4f1891e91532b2f4338100f1b7474dbd to your computer and use it in GitHub Desktop.
Duplicate label
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
{% form_theme form ':form/base:fields.html.twig' %} | |
{{ form_start(form, { 'action': action, 'method': 'POST', 'attr': {'novalidate': 'novalidate'} }) }} | |
{{ form_row(form.position) }} | |
{{ form_rest(form) }} | |
{{ form_end(form) }} |
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
<?php | |
namespace RDW\Bundle\JobBundle\Form\Type; | |
use RDW\Bundle\AppBundle\Form\EventListener\BuildPhotoUploadFormListener; | |
use RDW\Bundle\AppBundle\Form\EventListener\BuildContactPersonPhotoUploadFormListener; | |
use RDW\Bundle\AppBundle\Form\DataTransformer\FormattedNumberTransformer; | |
use RDW\Bundle\AppBundle\Form\Type\ProfessionalAreaCollectionPopupType; | |
use RDW\Bundle\AppBundle\Form\Type\PositionAutocompleteType; | |
use RDW\Bundle\JobBundle\Entity\Job; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
/** | |
* Class BaseJobType | |
* | |
* @package RDW\Bundle\JobBundle\Form\Type | |
*/ | |
class BaseJobType extends AbstractType | |
{ | |
/** | |
* {@inheritDoc} | |
*/ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
if (! $options['disabled']) { | |
$builder->addEventSubscriber(new BuildPhotoUploadFormListener()); | |
$builder->addEventSubscriber(new BuildContactPersonPhotoUploadFormListener()); | |
} | |
$builder | |
->add('position', PositionAutocompleteType::class, ['required' => true]) | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getName() | |
{ | |
return ''; | |
} | |
} |
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
{% block position_autocomplete_widget %} | |
{% if is_url_match_manage(app.request) %} | |
{% spaceless %} | |
{{ form_label(form, label|default(null), { 'label_attr' : { 'class' : 'label_1' } }) }} | |
{{ form_widget(form, { 'attr': { 'class': 'input ' ~ form.vars.attr.class, 'data-placeholder': form.vars.attr.placeholder|trans, 'data-ajax-url': url('rdw_search.search.filter_values', { 'filter': 'all_positions' }), 'data-find-pa-url': url('rdw_search.search.find_pa', { 'filter': 'all_positions' }), "data-init-url": url('rdw_search.search.init_values') } }) }} | |
{{ form_errors(form) }} | |
{% endspaceless %} | |
{% else %} | |
{% spaceless %} | |
{% set errorClass = errors|length > 0 ? 'error' : '' %} | |
<li {% if errors|length > 0 %}class="error"{% endif %}> | |
{{ form_label(form, label|default(null), { 'label_attr' : { 'class' : 'label_1' } }) }} | |
{{ form_widget(form, { 'attr': { 'class': 'input ' ~ form.vars.attr.class ~ ' ' ~ errorClass, 'data-placeholder': form.vars.attr.placeholder|trans, 'data-ajax-url': url('rdw_search.search.filter_values', { 'filter': 'all_positions' }), 'data-find-pa-url': url('rdw_search.search.find_pa', { 'filter': 'all_positions' }), "data-init-url": url('rdw_search.search.init_values') } }) }} | |
{{ form_errors(form) }} | |
</li> | |
{% endspaceless %} | |
{% endif %} | |
{% 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
<?php | |
namespace RDW\Bundle\AppBundle\Form\DataTransformer; | |
use RDW\Bundle\JobBundle\Entity\Position; | |
/** | |
* Class PositionAutocompleteTransformer | |
* | |
* @package RDW\Bundle\AppBundle\Form\DataTransformer | |
*/ | |
class PositionAutocompleteTransformer extends SingleEntityTransformer | |
{ | |
/** | |
* Transforms an object ($position) to a string (title). | |
* | |
* @param Position $position | |
* @return string | |
*/ | |
public function transform($position) | |
{ | |
if (!$position) { | |
return null; | |
} | |
return $position->getTitle(); | |
} | |
/** | |
* Transforms a string to an object. | |
* | |
* @param string $id | |
* | |
* @return object|null | |
*/ | |
public function reverseTransform($id) | |
{ | |
if (!$id) { | |
return null; | |
} | |
$found = null; | |
if (is_numeric($id)) { | |
$found = $this->om->getRepository($this->repositoryName)->find($id); | |
} | |
if (!$found && is_string($id)) { | |
$found = $this->om->getRepository($this->repositoryName)->findOneBy(['title' => $id]); | |
if(!$found){ | |
$found = new Position(); | |
$found->setTitle($id); | |
$this->om->persist($found); | |
$this->om->flush($found); | |
} | |
} | |
return $found; | |
} | |
} |
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
<?php | |
namespace RDW\Bundle\AppBundle\Form\Type; | |
use Doctrine\Common\Persistence\ObjectManager; | |
use Symfony\Component\Form\AbstractType; | |
use RDW\Bundle\AppBundle\Form\DataTransformer\PositionAutocompleteTransformer; | |
use Symfony\Component\Form\Extension\Core\Type\TextType; | |
use Symfony\Component\Form\Extension\Core\Type\HiddenType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
/** | |
* Class PositionAutocompleteType | |
* | |
* @package RDW\Bundle\AppBundle\Form\Type | |
*/ | |
class PositionAutocompleteType extends AbstractType | |
{ | |
/** | |
* @var ObjectManager | |
*/ | |
protected $om; | |
/** | |
* @var string | |
*/ | |
protected $filter; | |
/** | |
* @var string | |
*/ | |
protected $repositoryName; | |
/** | |
* @param ObjectManager $om | |
*/ | |
public function __construct(ObjectManager $om) | |
{ | |
$this->om = $om; | |
} | |
/** | |
* @param string $filter | |
*/ | |
public function setFilter($filter) | |
{ | |
$this->filter = $filter; | |
} | |
/** | |
* @param string $repositoryName | |
*/ | |
public function setRepositoryName($repositoryName) | |
{ | |
$this->repositoryName = $repositoryName; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
parent::buildForm($builder, $options); | |
$builder->addModelTransformer(new PositionAutocompleteTransformer($this->om, $this->repositoryName)); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function setDefaultOptions(OptionsResolverInterface $resolver) | |
{ | |
$resolver->setDefaults([ | |
'mapped' => true, | |
'error_bubbling' => false, | |
'required' => false, | |
'label' => ucfirst($this->filter), | |
'attr' => [ | |
'placeholder' => 'Choose an option', | |
'class' => 'position-autocomplete', | |
'data-filter' => $this->filter, | |
], | |
]); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function getName() | |
{ | |
return 'position_autocomplete'; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function getParent() | |
{ | |
return TextType::class; | |
} | |
} |
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
<?php | |
namespace RDW\Bundle\AppBundle\Form\DataTransformer; | |
use Doctrine\Common\Persistence\ObjectManager; | |
use Symfony\Component\Form\DataTransformerInterface; | |
/** | |
* Class SingleEntityTransformer | |
* | |
* @package RDW\Bundle\AppBundle\Form\DataTransformer | |
*/ | |
class SingleEntityTransformer implements DataTransformerInterface | |
{ | |
/** | |
* @var ObjectManager | |
*/ | |
protected $om; | |
/** | |
* @var string | |
*/ | |
protected $repositoryName; | |
/** | |
* @param ObjectManager $om | |
* @param string $repositoryName | |
*/ | |
public function __construct(ObjectManager $om, $repositoryName) | |
{ | |
$this->om = $om; | |
$this->repositoryName = $repositoryName; | |
} | |
/** | |
* Transforms an object to a string. | |
* | |
* @param object|null $item | |
* | |
* @return string | |
*/ | |
public function transform($item) | |
{ | |
if (null === $item) { | |
return ""; | |
} | |
return $item->getId(); | |
} | |
/** | |
* Transforms a string to an object. | |
* | |
* @param string $id | |
* | |
* @return object|null | |
*/ | |
public function reverseTransform($id) | |
{ | |
if (! $id) { | |
return null; | |
} | |
return $this->om->getRepository($this->repositoryName)->find($id); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment