How do I show a field based on the selection of another field - laravel-backpack

I have a form field, when I select a car, I want to get the type of car or the result of the model.
$this->crud->addField([
'name' => 'car_id',
'label' => 'Car',
'type' => 'select2_from_ajax',
'attribute' => 'name',
'model' => Car::class,
'data_source' => url('admin/api/cars'),
'placeholder' => 'Search and select a car',
'minimum_input_length' => 2,
]);
And how do I use the result to determine which other fields to show in the form?

You can do that in Backpack v5 using the included CrudField JS Library. To hide/show other fields depending on card_id, you should:
Step 1. In your setupCreateOperation() and/or setupUpdateOperation() load a new JS file:
Widget::add()->type('script')->content('assets/js/admin/forms/product.js');
Step 2. Create that JS file. Inside it, you can now easily select and get the value of Backpack fields. To show/hide other fields depending on car_id:
crud.field('car_id').onChange(function(field) {
// eg. show "car_model" if "car_id" is 1
crud.field('car_model').show(field.value == 1);
}).change();
For more things you can do with the CrudField JS Library, check out its docs.

Related

How can I sort a views field created in hook_views_data_alter?

In a custom drupal 9 module I define a new field for a view within hook_views_data_alter(&$data).
$data['node']['node_views_mydata'] = array(
'title' => t('Node Views Mydata'),
'field' => array(
'title' => t('Node views mydata'),
'help' => t('Shows some data in views'),
'id' => 'node_views_mydata',
'sort' => [
'node_views_mydata' => 'default',
],
)
);
I have also defined a field plugin processing text data for this field, and can insert and output the field in views.
Now I would like to make the field sortable. But I can't do that. I always get the error "unknown column". In fact, node_views_data is not a "real" node field, but only created on the fly via the hook.
Is there nevertheless a way to sort by this column?

SugarCRM Filter From Related Module

I am trying to make a custom filter based on another module in sugarCRM 8.0.1.
I have a module Ev_Registrations that has one Ev_Event and one Contact, the tables are:
ev_registrations
ev_registrations_contacts_c
ev_registrations_ev_events_c
How do I make a dropdown for event or contact on the list page as highlighted below?
I've tried making a custom filter, and managed to get it showing up in the list, however, it doesn't seem to be actually filtering by the event name.
$viewdefs['EV_Registrations']['base']['filter']['basic']['filters'][] = array(
'id' => 'filterRegistrationByEvent',
'name' => 'LBL_FILTER_REGISTRATION_BY_EVENT',
'filter_definition' => array(
array(
'ev_registrations_ev_events_c.ev_events.name' => '2019 Foo Bar Event',
),
),
'editable' => false,
'is_template' => false,
);

Is it possible to hide some options from choice list in twig in from symfony select type form element?

I have a form and making a select list element as follows-
$builder->add('location', 'choice', array(
'empty_value' => 'Select',
'choices' => array(0 => 'Brazil', 1 => 'USA', 2 => 'Europe'),
'label' => 'select your choice',
));
Now I don't want the second option to come in dropdown, I would set it in js on basis of some other conditions so dropdown should show only two options i.e. Brazil and Europe, Is it possible to do so in symfony version 2.3?
The only idea I come across is to create your own form_theme and then filter a thing you want to hide.
http://symfony.com/doc/current/form/form_customization.html#what-are-form-themes

CakePHP FormHelper multiple select does not remember settings

In my form I have a input field with multiple checkboxes.
This works as expected.
When for some reason (eg an obligated field in the form is empty upon submit) the form after submit is not posted, all other fields maintain there input except the field below, all checkboxes become unchecked.
What can I do to make this field remember it's settings?
$options = array(
'1' => 'one',
'2' => 'two',
'3' => 'three'
);
echo $this->Form->input('checkboxes',array(
'type' => 'select',
'multiple' => 'checkbox',
'options' => $options,
'default' => array(1,2,3)
));
FormHelper uses the $this->request->data to fill the fields. You need to keep (or fill) your data in request data array and the cakephp will maintain your data.

Load a Symfony sfWidgetFormDoctrineChoice select with a limited set of results

I am developing a form in a Symfony application where a user must indicate a country, a region and an optional island using HTML select elements.
I have three models: Country, Region and Island; and Symfony has auto-generated three widgets in the form using the sfWidgetFormDoctrineChoice widget:
...
'country_id' => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Country'), 'add_empty' => false)),
'region_id' => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Region'), 'add_empty' => false)),
'island_id' => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Island'), 'add_empty' => true)),
...
Since the country list is large, and so the region list, I've been thinking on filtering the options available in Region and Island according to the value selected in Country.
Doing this after the HTML document is ready is easy with jQuery's change method and a simple AJAX request. But I wonder if there's a way of doing this directly from Symfony, perhaps in form configuration, to have a default combined selection.
Any suggestions?
Thanks!
After playing around with sfDependentSelectPlugin, I ended up assigning custom queries to initialize the HTML select elements:
$countryId = $this->getObject()->getCountry()->getTable()->getDefaultCountryId();
$regionId = $this->getObject()->getRegion()->getTable()->getDefaultRegionId($countryId);
$islandId = $this->getObject()->getIsland()->getTable()->getDefaultIslandId($regionId);
$this->widgetSchema->setDefault('country_id', $countryId);
$this->setWidget('region_id', new sfWidgetFormDoctrineChoice(array(
'model' => $this->getRelatedModelName('Region'),
'query' => $this->getObject()->getRegion()->getTable()->getRegionsQuery($countryId),
'default' => $regionId,
)));
$this->setWidget('island_id', new sfWidgetFormDoctrineChoice(array(
'model' => $this->getRelatedModelName('Island'),
'query' => $this->getObject()->getIsland()->getTable()->getIslandsQuery($regionId),
'add_empty' => '---',
'default' => $islandId,
)));
And then updating the options available with AJAX requests using jQuery. The good thing is that the actions that handle the AJAX requests use the same query methods above to return a new set of results.