Skip to content

Instantly share code, notes, and snippets.

@strawherotk
Last active August 6, 2018 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save strawherotk/2be6443685daa698e305f4f60187fbea to your computer and use it in GitHub Desktop.
Save strawherotk/2be6443685daa698e305f4f60187fbea to your computer and use it in GitHub Desktop.
demo - float validation
sap.ui.define([
"sap/ui/core/mvc/Controller"
], function (Controller) {
"use strict";
return Controller.extend("barcodescan.controller.barcode", {
onInit: function () {
var oViewModel = new sap.ui.model.json.JSONModel({
number: 0
}, true);
this.getView().setModel(oViewModel, "viewModel");
},
scanSuccess: function (oEvent) {
var sText = oEvent.getParameters().text;
this.getView().byId("barcodeResult").setText(sText);
},
scanFail: function (oEvent) {
var sText = "Scan Failed";
this.getView().byId("barcodeResult").setText(sText);
},
textFormatter: function (sValue) {
console.log(sValue);
return sValue;
},
onInputLiveChange: function (oEvt) {
var oControl = oEvent.getSource();
this.validateFloatInput(oControl);
},
onInputChange: function (oEvent) {
var oControl = oEvent.getSource();
this.validateFloatInput(oControl);
},
validateFloatInput: function (oControl) {
var oBinding = oControl.getBinding("value");
var oValue = oControl.getValue();
try {
var oParsedValue = oBinding.getType().parseValue(oValue, oBinding.sInternalType); // throw error if cannot parse value
if (oParsedValue) {
oControl.setValueState(sap.ui.core.ValueState.None);
} else {
oControl.setValueState(sap.ui.core.ValueState.Error);
}
} catch (ex) {
oControl.setValueState(sap.ui.core.ValueState.Error);
}
}
});
});
<mvc:View controllerName="barcodescan.controller.barcode" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:mvc="sap.ui.core.mvc"
displayBlock="true" xmlns="sap.m" xmlns:ndc="sap.ndc">
<App>
<pages>
<Page title="{i18n>title}">
<content>
<FlexBox height="100px" alignItems="Center" justifyContent="Center">
<ndc:BarcodeScannerButton scanSuccess="scanSuccess" scanFail="scanFail" width="10em"></ndc:BarcodeScannerButton>
</FlexBox>
<FlexBox alignItems="Center" justifyContent="Center">
<Text id="barcodeResult"/>
</FlexBox>
<FlexBox alignItems="Center" justifyContent="Center">
<Label text="Input with type 'String': "/>
<Input></Input>
</FlexBox>
<FlexBox alignItems="Center" justifyContent="Center">
<Label text="Input with type 'Number': "/>
<Input type="Number"></Input>
</FlexBox>
<FlexBox alignItems="Center" justifyContent="Center">
<Label text="bound to view's JSON model: "/>
<Input value="{path: 'viewModel>/number', type:'sap.ui.model.type.Float'}" liveChange="onInputLiveChange" change="onInputChange"></Input>
</FlexBox>
</content>
</Page>
</pages>
</App>
</mvc:View>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment