So the easy but not recommended way is to put your element under app/code/local/Varien/Data/Form/Element/. As Magento is simply using the order of include_paths and will first look into the local folder, before it goes to the lib folder.
But the wiring to Varien_Data_Form_Element is just a fallback which is defined in lib\Varien\Data\Form\Abstract.php.
public function addField($elementId, $type, $config, $after=false)
{
if (isset($this->_types[$type])) {
$className = $this->_types[$type];
}
else {
$className = 'Varien_Data_Form_Element_'.ucfirst(strtolower($type));
}
So the question is where can you defined that _types array? There is an addType method in the same class which can be called. When you search for that method you might stumble over an iteration of _getAdditionalElementTypes on the form block in app\code\core\Mage\Adminhtml\Block\Widget\Form.php
protected function _addElementTypes(Varien_Data_Form_Abstract $baseElement)
{
$types = $this->_getAdditionalElementTypes();
foreach ($types as $code => $className) {
$baseElement->addType($code, $className);
}
}
The default implementation of _getAdditionalElementTypes just returns an empty array, but there is already some working implementation in the Magento Core in Mage_Adminhtml_Block_Sales_Order_Create_Form_Abstract.
So that example looks like:
protected function _getAdditionalFormElementTypes()
{
return array(
'file' => Mage::getConfig()->getBlockClassName('adminhtml/customer_form_element_file'),
'image' => Mage::getConfig()->getBlockClassName('adminhtml/customer_form_element_image'),
'boolean' => Mage::getConfig()->getBlockClassName('adminhtml/customer_form_element_boolean'),
);
}
And even in your form which you need to extend: Mage_Adminhtml_Block_System_Config_Form
protected function _getAdditionalElementTypes()
{
return array(
'export' => Mage::getConfig()->getBlockClassName('adminhtml/system_config_form_field_export'),
'import' => Mage::getConfig()->getBlockClassName('adminhtml/system_config_form_field_import'),
'allowspecific' => Mage::getConfig()
->getBlockClassName('adminhtml/system_config_form_field_select_allowspecific'),
'image' => Mage::getConfig()->getBlockClassName('adminhtml/system_config_form_field_image'),
'file' => Mage::getConfig()->getBlockClassName('adminhtml/system_config_form_field_file')
);
}
Based on that you should be able to implement your own _getAdditonalFormElementTypes by rewriting the System_Config_Form which can then handle new frontend types in your config.
€: Thinking about the question of Fabian it should also be possible by using an observer. The addType method on the form element is public, so you can call that and add custom types from and observer. You need to listen to an event in the form object and in the observer add your custom types. Just make sure this happens before _initFields, which is called from initForm. So a good point can be the prepare_layout events which are called before the initForm method (see Mage_Adminhtml_Block_System_Config_Edit->initForm and Mage_Core_Block_Abstract->setLayout)
As far as I know there is no way to define this in system.xml
. You would have to manipulate the generation of the form.
Also check this thread on StackOverflow stating that password managers (and also Chrome starting from v34) ignore this attribute: https://stackoverflow.com/questions/3868299/is-autocomplete-off-compatible-with-all-modern-browsers
The most important quotes:
Password managers now ignore the autocomplete attribute for password fields in the major browsers as of:
- IE11
- Firefox 30
- Chrome 34
- Safari seems to have an opt-in option to ignore them
It should still work fine for disabling autocomplete on form fields, but no longer affects the password manager.
And:
As of Chrome v34, autocomplete="off" is now ignored by default.
This somewhat debatable feature can be disabled in the flags configuration by visiting chrome://flags
http://news.softpedia.com/news/Chrome-34-Seeks-to-Save-All-Your-Passwords-436693.shtml
So I'd suggest to accept that users have to care about it themselves.
Best Answer
As far as I know, if we want to add more custom validations. We should validate with
beforeSave()
. For example, take a look the cookie validation:vendor/magento/module-cookie/Model/Config/Backend/Lifetime.php
.We can add more Js validation for the user interface input.
A Sample to add Js validation for system xml:
In your module, create file the layout -
adminhtml_system_config_edit.xml
app/code/Vendor/Module/view/adminhtml/layout/adminhtml_system_config_edit.xml
Remember to add your custom validation in the template.