I have created a formElement="checbkox" with prefer toogle. It works well in loading data from database (0 or 1), it changes to No or Yes manually, but, by default when the page is loaded is not set to Yes (It loaded always in NO) altough the value in DOM is 1.
<input type="checkbox" class="admin__actions-switch-checkbox" data-bind="attr: {id: uid, name: inputName}, value: value, disable: disabled, hasFocus: focused, simpleChecked: checked" id="DKISJNG" name="product[is_cut_resistant]" value="1">
The code in product_form.xml is:
<field name="is_cut_resistant" component="Magento_Catalog/js/components/visible-on-option/yesno" formElement="checkbox">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="valuesForOptions" xsi:type="array">
<item name="boolean" xsi:type="string">boolean</item>
</item>
</item>
</argument>
<settings>
<dataType>boolean</dataType>
<label translate="true">Cut Resistant</label>
<dataScope>is_cut_resistant</dataScope>
</settings>
<formElements>
<checkbox>
<settings>
<valueMap>
<map name="false" xsi:type="number">0</map>
<map name="true" xsi:type="number">1</map>
</valueMap>
<prefer>toggle</prefer>
</settings>
</checkbox>
</formElements>
</field>
Update your code with default argument.
<item name="default" xsi:type="number">1</item>
Now The code in product_form.xml is:
<field name="is_cut_resistant" component="Magento_Catalog/js/components/visible-on-option/yesno" formElement="checkbox"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="valuesForOptions" xsi:type="array"> <item name="boolean" xsi:type="string">boolean</item> </item> <item name="default" xsi:type="number">1</item> </item> </argument> <settings> <dataType>boolean</dataType> <label translate="true">Cut Resistant</label> <dataScope>is_cut_resistant</dataScope> </settings> <formElements> <checkbox> <settings> <valueMap> <map name="false" xsi:type="number">0</map> <map name="true" xsi:type="number">1</map> </valueMap> <prefer>toggle</prefer> </settings> </checkbox> </formElements> </field>
Hope it helps!
if issue solved,Click Kudos & Accept as Solution
It still not working.
Toogle appears on when a value is not retrieved from database, but when the '1' comes from database it is set to No.
The code of the plugin for getting the informatio for database is this:
public function afterGetData(Subject $subject, $result)
{
foreach ($result as $productId => &$productData) {
if (is_int($productId)) {
$extensionObject = $this->extensionAttributeValueRepository->getValueByProductCodeAttribute((int)$productId, 'personal_catalog_only');
$productData['product']['personal_catalog_only'] = $extensionObject->getValue();
$extensionObjectCutResistant = $this->extensionAttributeValueRepository->getValueByProductCodeAttribute((int)$productId, 'is_cut_resistant');
$productData['product']['is_cut_resistant'] = $extensionObjectCutResistant->getValue();
}
}
return $result;
}
Were you ever able to resolve this?
Hi,
Is this issue resolved ?
I am also facing same issue but in my custom admin form where i am showing check boxes value from DB is 1 also when we inspect checkbox input value = "1" but toggle is off. We are using Magento 2.4.2
How can resolve any suggestion or solution ?
Thanks