Configure select2 API to hide toggle - jquery-select2

How do I hide select all on select2?
https://select2.github.io/options.html
http://demos.krajee.com/widget-details/select2
'showToggleAll' => false, // does not work

you might want to find out why it does not work sense the demo version works ok but if you need to hide it...
$(".s2-togall-button, .s2-togall-select").css("display", "none")
run after the select2 has loaded seem to work for me.

Correct use of that option is like next:
echo $form->field($model, 'attribute')->widget(
Select2::classname(),
[
'data' => $arrayValues,
'theme' => Select2::THEME_BOOTSTRAP,
'options' => [
'class' => 'form-control',
'prompt'=> 'Select up to 3 values',
'multiple' => true,
],
'pluginOptions' => [
'tags' => false,
'maximumSelectionLength' => 3,
'allowClear' => false,
//'tokenSeparators' => [',', ' '],
'closeOnSelect' => true,
],
'showToggleAll' => false,
]
);
I hope will be useful

Related

Shopware 6 Plugin - text snippet not deleted on uninstall plugin - order custom field label

We created custom fields within our plugin for orders and for products.
Shopware creates text snippets for the custom fields labels.
These should be removed when uninstalling the plugin.
It works for the products custom field.
...
'customFields' => [
[
'name' => 'product_custom_field_name_dummy',
'type' => CustomFieldTypes::BOOL,
'config' => [
'type' => 'checkbox',
'componentName' => 'sw-field',
'customFieldType' => 'checkbox',
'label' => [
self::GER_ISO => 'Label GER',
self::EN_ISO => 'Label EN',
Defaults::LANGUAGE_SYSTEM => 'Label EN',
]
],
]
],
'relations' => [
[
'entityName' => ProductDefinition::ENTITY_NAME,
],
],
...
But not for the orders custom fields.
...
'customFields' => [
[
'name' => 'order_custom_field_name_dummy_one',
'type' => CustomFieldTypes::TEXT,
'config' => [
'customFieldType' => CustomFieldTypes::TEXT,
'label' => [
self::GER_ISO => 'Order Label GER',
self::EN_ISO => 'Order Label EN',
Defaults::LANGUAGE_SYSTEM => 'Order Label EN',
]
],
],
[
'name' => 'order_custom_field_name_dummy_two',
'type' => CustomFieldTypes::SELECT,
'config' => [
'customFieldType' => CustomFieldTypes::SELECT,
'componentName' => 'sw-single-select',
'label' => [
self::GER_ISO => 'Order Label GER 2',
self::EN_ISO => 'Order Label EN 2',
Defaults::LANGUAGE_SYSTEM => 'Order Label EN 2',
],
'options' => [
...
]
]
],
[
'name' => 'order_custom_field_name_dummy_three',
'type' => CustomFieldTypes::DATETIME,
'config' => [
'customFieldType' => CustomFieldTypes::DATETIME,
'label' => [
self::GER_ISO => 'Order Label GER 3',
self::EN_ISO => 'Order Label EN 3',
Defaults::LANGUAGE_SYSTEM => 'Order Label EN 3',
]
],
],
[
'name' => 'order_custom_field_name_dummy_four',
'type' => CustomFieldTypes::SELECT,
'config' => [
'customFieldType' => CustomFieldTypes::SELECT,
'componentName' => 'sw-single-select',
'label' => [
self::GER_ISO => 'Order Label GER 4',
self::EN_ISO => 'Order Label EN 4',
Defaults::LANGUAGE_SYSTEM => 'Order Label EN 4',
],
'options' => [
...
]
],
],
],
'relations' => [
[
'entityName' => OrderDefinition::ENTITY_NAME,
],
],
...
Is this a problem Shopware has with order custom fields or did we possibly make a mistake when creating the order custom fields?
Edit:
The custom fields are created on install method und removed on uninstall method inside the plugin via the CustomFieldSetRepository.
Edit:
This is how we delete the custom fields on uninstall:
public function uninstallCustomFieldSet() {
$customFieldSet = $this->getCustomFieldSet(self::CUSTOM_FIELD_SET_NAME);
if ($customFieldSet instanceof CustomFieldSetEntity) {
$this->customFieldSetRepository->delete([['id' => $customFieldSet->getId()]], $this->context);
}
}
protected function getCustomFieldSet(string $customFieldSetName): ?CustomFieldSetEntity {
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', $customFieldSetName));
$criteria->addAssociation('customFields');
$criteria->addAssociation('relations');
$customFieldSet = $this->customFieldSetRepository->search($criteria, $this->context)->first();
if ($customFieldSet instanceof CustomFieldSetEntity) {
return $customFieldSet;
} else {
return null;
}
}
The author mentions that Shopware 6 confirms that it's a bug.
I am creating an answer to this question even though it was already answered in the comments so that it can be accepted & this question resolved.

How to get the selected option of a radion button element in Zend Framework 2?

In a Fieldset I have an Element\Radio foo and Element\Text bar.
public function init()
{
$this->add(
[
'type' => 'radio',
'name' => 'foo',
'options' => [
'label' => _('foo'),
'value_options' => [
[
'value' => 'a',
'label' => 'a',
'selected' => true
],
[
'value' => 'b',
'label' => 'b'
]
]
]
...
]);
$this->add(
[
'name' => 'bar',
'type' => 'text',
'options' => [
'label' => 'bar',
...
],
...
]);
}
The validation of the field bar is depending on the selected foo option. It's easy to implement, if I can get the selected value of foo:
public function getInputFilterSpecification()
{
return [
'bar' => [
'required' => $this->get('foo')->getCheckedValue() === 'a',
...
],
];
}
But there is no method Radio#getCheckedValue(). Well, I can iterate over the $this->get('foo')->getOptions()['value_options'], but is it really the only way?
How to get (in the Fieldset#getInputFilterSpecification()) the selected option of a Zend\Form\Element\Radio?
The selected option gets POSTed to the server along with everything else from the HTML form and is all of this is available in validators through the $context array.
You can create a conditionally required field by using a callback validator and the $context array like this:
public function getInputFilterSpecification() {
return [
'bar' => [
'required' => false,
'allow_empty' => true,
'continue_if_empty' => true,
'required' => true,
'validators' => [
[
'name' => 'Callback',
'options' => [
'callback' => function ($value, $context) {
return $context['foo'] === 'a'
},
'messages' => [
\Zend\Validator\Callback::INVALID_VALUE => 'This value is required when selecting "a".'
]
]
]
]
],
];
}
That would check if 'foo' is equal to 'a', i.e. option 'a' is selected and return true when it is, which marks the input as valid, and false when it's not, marking the input invalid.

zf2 form disable select option(s)

Is it possible to disable the options in a select element?
I have a form with a select element that by default has a lot of options available. During form creation, depending on information retrieved from the database, i would like to disable certain options.
Some research came up with
$form->get('selectElement')->setAttribute("disabled", array(0, 1, 2));
...which should disable the first 3 options, but unfortunately does not.
You must use the setAttribute() method to set the attributes of your select element, not its options. For this, you should use setValueOptions():
$myOptions = $form->get('selectElement')->getValueOptions();
foreach ([0, 1, 2] as $value) {
$myOptions [$value]['disabled'] = true ;
}
$form->get('selectElement')->setValueOptions($myOptions);
$myOptionsmust be an array of options:
[
[
'label' => 'My first option',
'disabled' => false,
'value' => 1
],
[
'label' => '2nd option',
'disabled' => false,
'value' => 2
],
[
'label' => '3rd option disabled',
'disabled' => true,
'value' => 3
],
]

From Backend to Frontend Yii2 Advanced App

I'm trying to link some controllers from frontend to backend. After some hours I don't where could be the problem.
Backend
file: main.php
'urlManager' => [
'enablePrettyUrl' => false,
'showScriptName' => false,
'baseUrl' => '/backend/web',
],
'urlManagerFrontEnd' => [
'class' => 'yii\web\urlManager',
'baseUrl' => '/frontend/web',
'enablePrettyUrl' => false,
'showScriptName' => false,
]
file: SiteController.php
public function actionIndex()
{
// User's variable
$user = \common\models\User::findIdentity(Yii::$app->user->id);
if($user->role != self::USER_ADMIN){
return $this->redirect(Url::to(Yii::$app->urlManagerFrontEnd->createUrl(['/site/index'])));
}
return $this->render('index');
}
Using this
Url::to(Yii::$app->urlManagerFrontEnd->createUrl(['/site/index']))
Returns me
/advanced/backend/web/index.php?r=site%2Findex
Any advice?
Your code is correct. urlManagerFrontEnd should return url based on baseUrl /frontend/web.
Try change baseUrl to http://yourdomain/
I did little googling and found this link.
for reference I am posting same here
I read around in the UrlManager.php and found the following:
$baseUrl = $this->showScriptName || !$this->enablePrettyUrl ? $this->getScriptUrl() : $this->getBaseUrl();
So this means when showScriptName= true and enablePrettyUrl=false $baseUrl = getScriptUrl() otherwise $baseUrl = getBaseUrl()
So it just work with prettyUrl=true and the showScriptName = false. When we set prettyUrl on true it takes $baseUrl = getBaseUrl()
Changing it to the following it resolves our problem =).
/*$baseUrl = $this->showScriptName || !$this->enablePrettyUrl ? $this->getScriptUrl() : $this->getBaseUrl();*/
$baseUrl = !$this->showScriptName || $this->enablePrettyUrl ? $this->getScriptUrl() : $this->getBaseUrl();
Now you have to set prettyurl=false and the other on true et voila
I tried this on a fresh template and then applied code you mentioned in question and got the same error as you got.
But then after the fix I did according to this post I get correct path.
This link is also helpful.
in your frontend config add this to the top to define 2 variables.
use \yii\web\Request;
$baseUrl = str_replace('/frontend/web', '/frontend/web', (new Request)->getBaseUrl());
$backEndBaseUrl = str_replace('/frontend/web', '/backend/web', (new Request)->getBaseUrl());
And set these variables as the baseUrl parameters in the components
'components' => [
'urlManager' => [
'class' => 'yii\web\urlManager',
'enablePrettyUrl' => false,
'showScriptName' => false,
//'baseUrl' => '/frontend/web',
'baseUrl'=> $baseUrl,
],
'urlManagerBackEnd' => [
'class' => 'yii\web\urlManager',
'enablePrettyUrl' => false,
'showScriptName' => false,
//'baseUrl' => '/backend/web',
'baseUrl' => $backEndBaseUrl,
],
then you can have links from the frontend to the backend by e.g.
$backendUrl= Yii::$app->urlManagerBackEnd->createUrl('//');
echo yii\helpers\Html::a('link to backend', $backendUrl);
to have the same from the backend to the frontend add this to the backend config:
use \yii\web\Request;
$baseUrl = str_replace('/backend/web', '/backend/web', (new Request)->getBaseUrl());
$frontEndBaseUrl = str_replace('/backend/web', '/frontend/web', (new Request)->getBaseUrl());
and in the components:
'urlManager' => [
'class' => 'yii\web\urlManager',
'enablePrettyUrl' => false,
'showScriptName' => false,
'baseUrl'=> $baseUrl,
],
'urlManagerFrontEnd' => [
'class' => 'yii\web\urlManager',
'enablePrettyUrl' => false,
'showScriptName' => false,
//'baseUrl' => '/backend/web',
'baseUrl' => $frontEndBaseUrl,
],
and to create links use:
$frontendUrl= Yii::$app->urlManagerFrontEnd->createUrl('//');
echo yii\helpers\Html::a('link to frontend', $frontendUrl);
forgot you can of course also link to specific pages e.g. from backend to frontend site/about:
$frontendUrl= Yii::$app->urlManagerFrontEnd->createUrl('/site/about');
echo yii\helpers\Html::a('link to frontend site about', $frontendUrl);
BTW. if you have removed the /web behavior by some htaccess you should also remove it in the variables.
use this code. it will redirect you to front end
return $this->redirect(Yii::$app->urlManager->createUrl('./../../frontend/web/'));
Use below one:
Url::to(Yii::$app->urlManagerBackEnd->createUrl('index.php/'/site/index'), true);

Yii2 : How to change URL in Action Column Grid View?

I want to change url in action view, from view to viewdaily, viewweekly and viewmonthly, what should i do?
[
'class' => 'yii\grid\ActionColumn',
],
The simplest way is not create/use ActionColumn, instead you can use raw-column and show there what you want:
[
'attribute' => 'some_title',
'format' => 'raw',
'value' => function ($model) {
return 'your Text';
},
],

Resources