I'm using Sonata 3 in my project. I faced with the problem with autocomplete field.
protected function configureFormFields(FormMapper $form)
{
$form
->add(
'contactPerson',
ModelAutocompleteType::class,
[
'label' => 'Contact Person',
'property' => 'name',
'model_manager' => $this->modelManager,
'class' => ContactPerson::class,
'btn_add' => 'Add new',
'callback' => [CustomerCompanyFilters::class, 'filterContactPersons'],
]
)
}
I added 'Add new' button and can create a new entity in popup, but it is not set automatically as a field value, I need to search it again. Is it possible to set field value just after pressing 'Create' button popup?
Related
When I submit the form. The value chosen from the dropdown list is successfully saved in the database.
The issue is when form is refresh in the edit mode, the value chosen for the brand is not retained.
Here is my configureFormFields:
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('image', 'text')
->add('brand', EntityType::class, array(
'class' => Brand::class,
'choice_label' => 'slug',
))
->add('Page', ChoiceType::class, array(
'choices' => array(
'Homepage' => 'Homepage',
'HomeProduit' => 'Home Produit',
'HomeRubrique' => 'Home Rubrique',
'PageContent' => 'Page Content'
)))
;
}
I solved the issue.
The problem was that there was no relationship between the 2 tables (relationship added and everything works fine). Hope that can help someone some day.
I am working on a Symfony2 CRM which implements Doctrine ORM for the forms. One of the forms on the CRM is for creating customer addresses, and has all the standard text fields such as street, city, postcode etc. but also a dropdown for country and county. The problem is, if I retrieve all counties from the database the list is huge and not only takes ages to load but it's very difficult for my client to find the county since it's for every country in the world.
What I'd like is for them to select the country first and then the county field is auto populated based on the selected country. However, I am unsure how to do this in Doctrine.
Here is my buildForm function in the form type (noting that here, Zone means County):
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('company' , 'text', array('required' => false ));
$builder->add('firstname' , 'text', array('required' => true, 'label' => 'First name' ));
$builder->add('lastname' , 'text', array('required' => true, 'label' => 'Last name' ));
$builder->add('address1' , 'text');
$builder->add('address2' , 'text', array('required' => false ));
$builder->add('city' , 'text');
$builder->add('country' , 'entity',
array(
'class' => 'AppBundle:Oc49Country',
'property' => 'name',
'empty_value' => 'Choose an option',
));
$builder->add('zone' , 'entity',
array(
'class' => 'AppBundle:Oc49Zone',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('z')
->orderBy('z.name', 'ASC');
},
'empty_value' => '-- Please select --',
'required' => true,
'label' => 'County/State'
));
$builder->add('postcode' , 'text');
$builder->add('save', 'submit', array(
'attr' => array(
'class' => 'btn btn-primary'
),
));
}
Is there a way of adding an additional parameter to the query builder in order to filter the county list, on the fly?
Since you want to change the County select list after the page is already rendered by symfony, you'll have to use ajax in some way to update just that county select list.
I would use an jquery/ajax call to populate the County select list based on the selected country. From your twig template, add some a jquery function along these lines:
$('#country_select).on('change', function() {
url: '/path/to/controller/to/get/countyList',
method: 'GET'
}).done(function() {
// update options
});
This question will help with details. Obviously my code is rough, but hopefully this helps.
I'm trying to add custom fields to the user registration form. I need to save 2 of them into the Drupal User's database and 1 into my own separate database.
I'm using hook_form_alter to add the fields and then hook_form_submit to save the fields.
How do I submit the first/last name fields into drupal's user table and then save the SSN into my own? I have a function to switch between the 2 databases already but am just not sure how to use hook_submit_form to save some in one database and some in another?
function myModule_form_alter(&$form, &$form_state, $form_id){
if($form_id == 'user_registration_form'){
$form['f_name'] = array(
'#type' => 'textfield',
'#title' => t('First Name'),
'#required' => TRUE,
'#attributes' => array('class' => array('fname'))
);
$form['l_name'] = array(
'#type' => 'textfield',
'title' => t('Last Name'),
'#required' => TRUE,
'#attributes' => array('class' => array('lname'))
);
$form['ssn'] = array(
'#type' => 'textfield',
'#title' => t('Social Security Number')
'#maxlength' => 11,
'#required' => TRUE,
'#attributes' => array('class' => array('ssn'), 'placeholder' => '999-99-9999')
);
Would it be something like this?
function myModule_form_submit($form, &$form_state){
//Array of information to save into drupal user's table
$edit = array(
'f_name' => $form_state['values']['f_name'],
'l_name' => $form_state['values']['l_name'],
'second_email' => $form_state['values']['second_email'],
);
user_save(drupal_anonymous_user(), $edit);
drupal_set_message(t('The form has been submitted.'));
}
I'm not sure if I can use drupal's user_save() function and pass it this new information to save or if I have to do something in the .install file to add these new columns and then target them in the form_submit?
In what field will you save the custom field data? You should create a custom table for those fields and save data in there. When you load the user, you should also load those details from that table. There is no way you can directly load those custom field data when user object is loaded.
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');
I use drupal 7, and Entity API to develope a module. I have an entity to record client information. I wish to use image_field to let client upload their logo. So I have this function:
function silver_client_enable()
{
field_cache_clear();
field_associate_fields("silver_client");
if(field_info_field('logo'))
return;
$field = array(
'field_name' => 'logo',
'cadinality' => 1,
'type' => 'image',
);
field_create_field($field);
$instance = array(
'field_name' => 'logo',
'entity_type' => 'silver_client',
'bundle' => 'silver_client',
'label' => 'Logo',
'description' => 'Logo',
'display' => array(
'default' => array('label' => 'hidden')
),
'settings' => array(
'file_directory' => '/logo',
),
'widget' => array(
'type' => 'image_image',
),
);
field_create_instance($instance);
}
In the entity creation/edit form, I use :
field_attach_form('silver_client', $client, $form, $form_state);
to attch the field.
When I called up this form, the image upload field was corrected displayed. An i can use it to uplod file to serve.
In the form submit function, I save the entity as:
entity_save('silver_client', $client);
However, after I press the save button, the entity table is correctly saved. Field table is not. Both field_data_logo and field_revision_logo are empty.
I believer Entity API looks after the retrieving and saving of attached fields. Can someone tell me what is wrong with my code? Thank you.
You have to write the values back into your entity:
field_attach_submit('silver_client', $client, $form, $form_state);
entity_save('silver_client', $client);
http://api.drupal.org/api/drupal/modules!field!field.attach.inc/function/field_attach_submit/7
And you should validate the field values:
field_attach_validate('silver_client', $client, $form, $form_state);
http://api.drupal.org/api/drupal/modules!field!field.attach.inc/function/field_attach_validate/7
Furthermore if you don't want to declare your entity and fields by yourself you might checkout the EntityConstructionKit : http://drupal.org/project/eck which allows to export entity structures with Features just like Views.