I made form using form builder
$builder
->add('name', null, [
'attr' => ['autofocus' => true],
'label' => 'label.name',
])
->add('city', EntityType::class, [
// looks for choices from this entity
'class' => City::class,
// uses the City.name property as the visible option string
'choice_label' => 'name',
'placeholder' => 'choose.city'
])
->add('university', EntityType::class, [
// looks for choices from this entity
'class' => University::class,
// uses the University.name property as the visible option string
'choice_label' => 'name',
])
->add('courses', EntityType::class, [
// looks for choices from this entity
'class' => Course::class,
// uses the University.name property as the visible option string
'choice_label' => 'name',
'multiple' => true,
])
;
Now my form looks like this
I want for dropdown choices to be integers and not blank.
For single dropdowns all I need to add is: #Assert\NotBlank(message="student.university_not_blank")
How can I set that those values on single dropdown should be integers?
How can I validate multiple choice dropdown not to be blank and its values to be integers?
Related
I have a form field which displays a bunch of checkboxes, the field is an EntityType. When the form renders on the front-end, a bunch of the checkboxes, not all, are already checked.
I'd like all options to be unchecked when the form renders but I can't figure out why specific ones are already checked. Below is what I currently have:
->add('example', EntityType::class, [
'class' => Example::class,
'multiple' => true,
'required' => false,
'by_reference' => false,
'expanded' => true,
'query_builder' => function (EntityRepository $repository) use ($client) {
return $repository
->createQueryBuilder('ex')
->join('ex.quote', 'e', Expr\Join::WITH, 'e.client = :client')
->setParameter('client', $client->getId());
},
]);
I have tried adding things like:
'choice_attr' => [
'checked' => false,
]
Any help would be great.
I am calling entity to provide drop down options. I set a place holder value. I tried setting the data value, but regardless placeholder value is alway tag with selected.
My PostFormType:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', TextType::class, array( 'attr' => array(
'class' => 'form-control'
)))
->add('price', TextType::class, array( 'attr' => array(
'class' => 'form-control'
)))
->add('artist', EntityType::class, [
'data'=>2,
'placeholder'=>'Choose Artist',
'class'=>Artist::class,
'choice_label'=>'artist',
'query_builder'=> function (EntityRepository $er) {
return $er->createQueryBuilder('artist')
->orderBy('artist.artist', 'ASC');
},
'empty_data' => null,
'attr' => array(
'class' => 'form-control'
)
])
->add('userId', HiddenType::class )
->add('description', TextareaType::class, array( 'attr' => array(
'class' => 'form-control'
)))
->add('purchaseDate','date')
->add('id',HiddenType::class)
;
}
You shouldn't configure data for property artist if it has to be modified by user in the form.
In case you want to set the default value of a new Entity. Even if you do, the change from UI form will not affect as the data attribute will reset the value to 2 as provided after submit.
You can always use Constructor of the relevant Entity to set any default value.
But, as artist is a referenced property. You should do it in Controller before you build the form.
$artist = $em->getRepository("AppBundle:Artist")->find(2);
$entity->setArtist($artist)
// Load the form here. The default value automatically set in the form
Hope this helps!
I'm trying to display a form with a "select" where is the "options" from a query.
$results = $conn->query(" select gsm, name from Contact");
$row = $results->fetchAll();
and after that: I trying to use this
foreach($row as $lign)
$centretechnique[$lign['gsm']] = $lign['name'];
and in formbuilder
$form->add('centretechnique', 'choice', array('required' => false, 'error_bubbling' => true, "empty_value" => "Choisir ", 'choices' => $centretechnique));
I want to display a list of select option with value=gsm and the displayed value is the name
this method llow me to display , but the problem that if I have duplicate option, only on option will be displayed.
for example, the result of the request is
array (size=2)
0 =>
array (size=2)
'gsm' => string '628436515' (length=9)
'name' => string 'name1' (length=7)
1 =>
array (size=2)
'gsm' => string '628436515' (length=9)
'name' => string 'name 2' (length=4)
just one option will be displayed.
any help please
If you want to populate choices form type from the database, then you should be using the form entity type.
Something like this:
$form->add(
'centretechnique',
'entity',
array(
'class' => 'MyBundle:Contact',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('c')
->orderBy('c.name', 'ASC');
},
'choice_label' => 'name',
'choice_value' => 'gsm',
'multiple' => false,
'expanded' => false,
'required' => false,
)
)
By building your choices you do :
$centretechnique[$lign['gsm']] = $lign['name'];
So your code does overwrite the first choice with the second, since you use the same key twice ...
Sorry for my english.
I need to create a drop down list (select option) with values taken from an Entity.
I must also add options (I use select2 to do this).
When I send the form it doesn't work. How can I configure the field type to make it work?
I need the possibilities to adding multiple persons
My current code is
...
->add('person', 'entity', array(
'attr' => array(
'class' => 'tags'
),
'class' => 'AppBundle:Person',
'data_class' => null,
'label' => 'Persons',
'mapped' => false,
'multiple' => true,
'required' => false
));
Select2
$('select.tags').select2({
language: 'it',
tags: true,
tokenSeparators: [','],
width: '100%'
});
Error message
The offset "0" does not exist.
is it possible to update an option field after adding it ?
$builder
->add('examens', 'entity', array(
'class' => 'TelegrammeExamenBundle:ExamExamen',
'property' => 'libelle',
'required' => true,
'empty_value' => 'Sélectionnez un examen',
//'data' => $this->em->getReference("TelegrammeExamenBundle:ExamExamen", 510),
'data' => null,
'query_builder' => function(ExamenRepository $r) {
return $r->getSelectList();
},
'attr' => array('class' => 'bg_white_filet_gris')
))
;
how modify field option ??? (setOption don't exist)
if (...) $builder->get('examens')->setOption('property', 'test');
You can simply ->add() it again. As the API documentation suggests for the add method:
Adds or replaces a child to the form
http://api.symfony.com/2.8/Symfony/Component/Form/FormInterface.html#method_add
This can be used to modify form elements for example in a FormEvent.
Alternatively the FormBuilder provides a setAttribute() method which can be used as follows:
$builder->get('examens')->setAttribute('property', 'test');