fix error when adding customfield choice

This commit is contained in:
Julien Fastré 2018-05-16 21:52:36 +02:00
parent a43dc3733f
commit cacf6d0f3a
3 changed files with 9 additions and 22 deletions

View File

@ -157,7 +157,7 @@ class CustomFieldChoice extends AbstractCustomField
->add(self::OTHER_VALUE_LABEL, TranslatableStringFormType::class, array( ->add(self::OTHER_VALUE_LABEL, TranslatableStringFormType::class, array(
'label' => 'Other value label (empty if use by default)')) 'label' => 'Other value label (empty if use by default)'))
->add(self::CHOICES, ChoicesType::class, array( ->add(self::CHOICES, ChoicesType::class, array(
'entry_type' => new ChoicesListType($this->defaultLocales), 'entry_type' => ChoicesListType::class,
'allow_add' => true 'allow_add' => true
)); ));

View File

@ -13,38 +13,24 @@ use Chill\MainBundle\Form\Type\TranslatableStringFormType;
class ChoicesListType extends AbstractType class ChoicesListType extends AbstractType
{ {
private $defaultLocales;
public function __construct($defaultLocales)
{
$this->defaultLocales = $defaultLocales;
}
/* (non-PHPdoc) /* (non-PHPdoc)
* @see \Symfony\Component\Form\AbstractType::buildForm() * @see \Symfony\Component\Form\AbstractType::buildForm()
*/ */
public function buildForm(FormBuilderInterface $builder, array $options) public function buildForm(FormBuilderInterface $builder, array $options)
{ {
$locales = $this->defaultLocales;
$builder->add('name', TranslatableStringFormType::class) $builder->add('name', TranslatableStringFormType::class)
->add('active', CheckboxType::class, array( ->add('active', CheckboxType::class, array(
'required' => false 'required' => false
)) ))
->add('slug', HiddenType::class, array( ->add('slug', HiddenType::class)
->addEventListener(FormEvents::SUBMIT, function(FormEvent $event) {
))
->addEventListener(FormEvents::SUBMIT, function(FormEvent $event) use ($locales){
$form = $event->getForm(); $form = $event->getForm();
$data = $event->getData(); $data = $event->getData();
$formData = $form->getData(); $formData = $form->getData();
if (NULL === $formData['slug']) { if (NULL === $formData['slug']) {
$slug = $form['name'][$locales[0]]->getData(); $slug = uniqid(rand(), true);
$slug = strtolower($slug);
$slug = preg_replace('/[^a-zA-Z0-9 -]/','', $slug); // only take alphanumerical characters, but keep the spaces and dashes too...
$slug = str_replace(' ','-', $slug); // replace spaces by dashes
$data['slug'] = $slug; $data['slug'] = $slug;
$event->setData($data); $event->setData($data);

View File

@ -52,9 +52,9 @@
{# we use javascrit to add an additional element. All functions are personnalized with the id ( = form.vars.id) #} {# we use javascrit to add an additional element. All functions are personnalized with the id ( = form.vars.id) #}
<script type="text/javascript"> <script type="text/javascript">
function addElementInDiv(div_id) { function addElementInDiv(div_id) {
var div = $('#' + div_id); var div = $('#' + div_id);console.log(div);
var prototype = div.data('prototype'); var prototype = div.data('prototype');console.log(prototype);
var index = div.data('index'); var index = div.data('index');console.log(index);
var add_element_link = $('#' + div_id + '_add_element_link'); var add_element_link = $('#' + div_id + '_add_element_link');
var new_fields = prototype.replace(/__name__label__/g, index); var new_fields = prototype.replace(/__name__label__/g, index);
var new_fields = prototype.replace(/__name__/g, index); var new_fields = prototype.replace(/__name__/g, index);
@ -68,7 +68,8 @@
var add_element_link = $('<a id="' + div_id + '_add_element_link"" href="#" class="sc-button bt-submit">{{ 'Add an element'|trans }}</a>'); var add_element_link = $('<a id="' + div_id + '_add_element_link"" href="#" class="sc-button bt-submit">{{ 'Add an element'|trans }}</a>');
var div = $('#' + div_id); var div = $('#' + div_id);
div.append(add_element_link); div.append(add_element_link);
div.data('index', div.find(':input').length / 5); div.data('index', div.find('td').length);
console.log(div.data('index'));
add_element_link.on('click', function (e) { add_element_link.on('click', function (e) {
e.preventDefault(); e.preventDefault();