La idea es que si pulso tanto los operadores de id 25 y 26 se cambie el inputtype del input value. ¿Cómo hago con este js?
function changevaluefield(e){
if (e.value == 25 || e.value == 26){
if (document.getElementById(e.id.replace("operator","value")).type != "file"){
document.getElementById(e.id.replace("operator","value")).classList.remove("form-control");
document.getElementById(e.id.replace("operator","value")).classList.add("custom-file-input");
document.getElementById(e.id.replace("operator","value")).type = "file";
}
}else{
if (document.getElementById(e.id.replace("operator","value")).type != "text"){
document.getElementById(e.id.replace("operator","value")).classList.remove("custom-file-input");
document.getElementById(e.id.replace("operator","value")).classList.add("form-control");
document.getElementById(e.id.replace("operator","value")).type = "text";
}
}
}
La idea es de cierto modo hacer esto dentro del php que he creado donde tengo un formulario personalizado y hacerlo mediante eventos, este es el formulario actual:
<?php
namespace App\Form\Type;
class FiltercampaignvalueType extends AbstractType
{
private $campaign;
private $filtergroup;
private $class;
public function buildForm(FormBuilderInterface $builder, array $options)
{
$this->campaign = $options['attr']['campaign'];
$this->filtergroup = $options['attr']['filtergroup'];
$this->class = 'row';
$builder->add('fieldcampaign', EntityType::class, array(
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('u')
->where('u.campaign = :campaign')
->setParameter('campaign', $this->campaign)
->orderBy('u.name', 'ASC');
},
'class' => Fieldcampaign::class,
'choice_label' => 'name',
'choice_value' => 'id',
'required' => true,
'label' => false,
'placeholder' => 'Choose a field',
'attr' => array(
'class' => 'fieldcampaign_select',
'onchange' => 'return preseclet(this);'
)
)
);
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$data = $event->getData();
$form = $event->getForm();
if (!$data || null === $data->getId()) {
$form->add('operator', EntityType::class, array(
'class' => Operator::class,
'choice_label' => 'name',
'choice_value' => 'id',
'required' => true,
'label' => false,
'placeholder' => 'Choose an operator'
)
);
}
});
$formModifier = function (FormInterface $form, Fieldtype $type = null) {
$form->add('operator', EntityType::class, array(
'class' => Operator::class,
'query_builder' => function (EntityRepository $er) use ($type) {
return $er->createQueryBuilder('u')
->where('u.fieldtype = :fieldtype')
->setParameter('fieldtype', $type);
//->orderBy('u.name', 'ASC');
},
'choice_label' => 'name',
'choice_value' => 'id',
'required' => true,
'label' => false,
'placeholder' => 'Choose an operator',
'attr' => array(
'class' => 'valuefield',
'onchange' => 'return changevaluefield(this);'
)
)
);
};
$builder->add('value', TextType::class, array (
'label' => 'Value',
'required' => false,
'label' => false,
'attr' => array(
'placeholder' => 'Insert value')
)
);
$builder->add('filtercampaigngroup', EntityType::class, array(
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('u')
->where('u.id = :id')
->setParameter('id', $this->filtergroup)
->orderBy('u.name', 'ASC');
},
'class' => Filtercampaigngroup::class,
'label' => false,
'choice_label' => 'name',
'choice_value' => 'id',
'required' => true,
'attr' => array(
'readonly' => true,
)
)
);
$value="";
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($formModifier) {
$data = $event->getData();
if($data !== null) {
if($data->getOperator() !== null) {
if ($data->getOperator()->getId() == 26 || $data->getOperator()->getId() == 25){
$value="file";
/* $builder->add('value', TextType::class, array (
'label' => 'Value',
'required' => false,
'label' => false,
'attr' => array(
'placeholder' => 'Insert value')
)
);*/
}else{
$value = "number";
}
$formModifier($event->getForm(), $data->getOperator()->getFieldtype());
} else {
$formModifier($event->getForm(), null);
}
} else {
$formModifier($event->getForm(), $data);
}
}
);
if ($value == "file"){
$builder->add('value', FileType::class, array (
'label' => 'Value',
'required' => false,
'label' => false,
'attr' => array(
'placeholder' => 'Insert File')
)
);
}else{
$builder->add('value', TextType::class, array (
'label' => 'Value',
'required' => false,
'label' => false,
'attr' => array(
'placeholder' => 'Insert value')
)
);
}
$builder->get('fieldcampaign')->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use ($formModifier) {
$type = $event->getForm()->getData()->getType();
$formModifier($event->getForm()->getParent(), $type);
}
);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Filtercampaign::class,
'csrf_protection' => false,
]);
}
}
Aparte de lo que he puesto me gustaría también poder hacer que persista la información cuando hago los cambios tanto de el filtercampaign como de operator si es el mismo tipo el value. Ahora mismo busco el poder hacer lo que hace el javascript pero mediante eventos de formulario de symfony: -Cambiar el type del input value a File cuando el input operator tiene id 25 o 26 y que vuelva a ser Text si es distinto a 25 y 26 y a poder ser que cuando cambio fieldcampaign no recargue todo el formulario dejando vacio el input value por ejemplo.