When defining a form element you have the option of specifying after_element_html
. what you put there will be shown after the element itself. Here is an example:
$fieldset->addField('some_field', 'text', array(
'label' => Mage::helper('core')->__('Some label'),
'name' => 'some_name',
'after_element_html' => '<button type="button" onclick="alert(\'Stop clicking me!!\')">Do not click</button>'
));
I think you can start from here. you can use the onclick
on the button to do an ajax call (or something like that) and upload your image.
Any rule-model could be used this way:
$ruleModel->validate($validationModel);
But your rule model must extend the \Magento\Rule\Model\AbstractModel
class and object which you will validate must have desired data (subtotal in your case, so I suppose it will be instance of \Magento\Quote\Model\Quote\Address
).
So, an example will be looking like this:
$address = $block->getAddress(); // or $quote->getAddress();
$rules = $block->getRules(); // load all availabel rules from DB
foreach ($rules as $rule) {
if ($rule->validate($address)) {
echo __('Pass conditions of the rule %1', $rule->getId());
} else {
echo __('Missed rule %1', $rule->getId());
}
}
The $rule->validate()
will call this code:
/**
* Validate rule conditions to determine if rule can run
*
* @param \Magento\Framework\DataObject $object
* @return bool
*/
public function validate(\Magento\Framework\DataObject $object)
{
return $this->getConditions()->validate($object);
}
Best Answer
To add a conditional field to the default model (and further utilize it for validating purposes), you will need to create a new model (or modify the existing one) that contains these fields.
Now, we have a table with the model description and we need to complete the model itself, and include into it the appropriate resource model and collection.
The model will be called 'Rule':
As you can see, our model inherits from the
Magento\Rule\Model\AbstractModel
model that already has all the required methods.Right in the Constructor, we'll add condition factories that allow us to work with them and create multiple methods. This should give us the understanding of how the model works.
Note that we are using the default condition models from the Magento SalesRule (
\Magento\SalesRule\Model\Rule\Condition
) module. If you need to expand the conditions, you can add your own clasees and/or rewrite them completely or inherit from the base available classes. In can be useful when you want to add a special condition that is not included in the default conditions. For example, Subtotal With Discount.....
Next, let's switch to the interface in the admin panel. We need the Controller with a set of actions (such as Save, Add, Edit, Grid Display, Conditions reload) and a layout with blocks.
Let's start with the Controller itself. First, declare the common Controller:
Here, we need to call out our models' factory in the Constructor. That is done to make them (and some auxiliary classes, like a register and a logger) publicy available.
The
_initRule
method is responsible for the current rule initialization or creating of a new and empty one with the ability of adding it to the register. The_initAction()
method loads a layout and makes the modules' menu available for actions (also, it adds breadcumbs). The_isAllowed()
method checks if the current admin has an access to the Controller.At the next step, we are going to add the default actions:
....
Editing:
This is how to add new conditions:
This class is responsible for loading the conditions that have been chosen in the interface (all the conditions can't be loaded at once).
....
Next, we need to create all the required blocks and layout.
Now, let's get down to creating and editing new rules. Let's create the main container block for editing:
When done, we should add the controllers title and the
save
andedit current model
buttons in the Constructor. Also, here you should add the main text for of the block.This is a form itself:
and tabs:
We'll have the two tabs: Ceneral model's information and Conditions.
Conditions:
To view more details, please follow this link.