Writing an asset extension
Asset extensions allows to create custom functionalities and look and feel to a certain asset type in WSO2 Enterprise Store. With this example, I am going to create a new asset type and add a custom validation to the asset creation page.
Download WSO2ES product distribution from wso2.com and extract it to your local drive. Start the server and fire up browser with the admin console url.
https://:9443/carbon/
Navigate to Extensions > Configure > Artifact Types and click the "Add new Artifact" link.
Download WSO2ES product distribution from wso2.com and extract it to your local drive. Start the server and fire up browser with the admin console url.
https://
Navigate to Extensions > Configure > Artifact Types and click the "Add new Artifact" link.
This will load the UI with a new default artifact.
Notice that it has following attributes in it's root node.
shortName="applications" singularLabel="Enterprise Application" pluralLabel="Enterprise Applications"
"applications" is the name of new asset type we are going to add.
In our example we are going to do a custom validation to the asset creation page. Let's add the fields we are going to validate.
Our intention is to provide 4 check-boxes and display an error message if at least one checkbox is not selected. Basically if user does not check any of the four checkboxes we provide will display an error message.
<table name="Tier Availability">
<field type="checkbox">
<name>Unlimited</name>
</field>
<field type="checkbox">
<name>Gold</name>
</field>
<field type="checkbox">
<name>Silver</name>
</field>
<field type="checkbox">
<name>Bronze</name>
</field>
</table>
Restart the server.
Now when we go to the publisher app, the new content type is available.
Now click the Add button to add a new asset of "Enterprise Applications".
You will be able to notice the new section we added on the rxt is available in this page.
We are going to add a custom validation to this four checkboxes where it will validate if at least one of the checkboxes is checked and if not show an error message bellow the checkboxes.
We can't do any changes to the core web application. But it's possible to add custom behavior via ES extensions. Since this is an asset specific customization which need to apply only to the new "application" asset type we need to create an "asset extension". There is one other extension type call "app extension" where it applies to all the asset types across the whole web application.
Navigate to "repository/deployment/server/jaggeryapps/publisher/extensions/assets" folder. Create a new folder and name it "applications". Note that the "applications" is the value of "shortName" attribute we gave on rxt creation. The complete path to the new folder is "repository/deployment/server/jaggeryapps/publisher/extensions/assets/applications".
Now with this folder we can override the default files in the core application. We need to add a client side javascript to the add asset page and with that file we need to initialize the client side event registrations where it will validate the checkboxes.
Note the folder structure in "repository/deployment/server/jaggeryapps/publisher/" ( say [A] ). We can override the files from above to "repository/deployment/server/jaggeryapps/publisher/extensions/assets/applications" ( say [B] ).
Copy [A]/themes/default/helpers/create_asset.js to [B]/themes/default/helpers/create_asset.js.
Add a new client side script of your choice to the list of js in [B]/themes/default/helpers/create_asset.js. I added a file call 'custom-checkbox-validate.js'. The content of [B]/themes/default/helpers/create_asset.js will be as follows.
var resources = function (page, meta) {
return {
js:['libs/jquery.form.min.js','publisher-utils.js','create_asset.js','jquery.cookie.js','date_picker/datepicker.js','select2.min.js','tags/tags-common.js','tags/tags-init-create-asset.js','custom-checkbox-validate.js'],
css:['bootstrap-select.min.css','datepick/smoothness.datepick.css','date_picker/datepicker/base.css', 'date_picker/datepicker/clean.css','select2.min.css'],
code: ['publisher.assets.hbs']
};
};
Now create the new file [B]/themes/default/js/custom-checkbox-validate.js
Put a alert('') in the above file and see if you are getting a browser alert message.
If you are not getting the alert message following might be probable causes.
- You haven't restart the server after the extension creation
- The asset type is not macing with the folder name.
- The folder structure in the extension is not aligning with the core application folder structure.
Update the custom-checkbox-validate.js with the following content. The script bellow will validate at least one checkbox is checked from the four checkboxes.
$(document).ready(function () {
validator.addCustomMethod(function () {
//Get the 4 checkboxes jQuery objects.
var bronze = $('#tierAvailability_bronze');
var unlimited = $('#tierAvailability_unlimited');
var gold = $('#tierAvailability_gold');
var silver = $('#tierAvailability_silver');
var errorMessage = "You need to check at least one from the tires";
/*
* Custom event handler to the four checkboxes.
* error by default is shown after the input element.
*/
var checkboxClickCustomHandler = function () {
if (silver.is(':checked') || gold.is(':checked') || unlimited.is(':checked') || bronze.is(':checked')) {
bronze.removeClass("error-field");
bronze.next().remove();
} else {
validator.showErrors(bronze, errorMessage);
}
};
//Register event hanlder for the the for checkboxes
bronze.click(checkboxClickCustomHandler);
unlimited.click(checkboxClickCustomHandler);
gold.click(checkboxClickCustomHandler);
silver.click(checkboxClickCustomHandler);
// Do the custom validation where it checks at least one checkbox is checked. Return type is a json of format {message:"",element:
Comments
This post helped quite a bit with getting validation working for our new LC transition inputs UI.