Any good validator examples?
Writing a custom screen and want to validate vendor item numbers and prices on input.
These are currently XLineEdit widgets. Suspect QValidators might be useful - any tips
or examples on how to use in scripts?
lmazzucchellli,
QValidators let you describe a pattern that input must follow. A validator will help with the price input but not vendor item number input.
First: prices
The application core defines a set of validators that you can use in scripts. Pick the one that matches your need. In this case, prices, you want the QDoubleValidator returned by GUIClient::priceVal(). This validator is used throughout the core application wherever a price can be entered. To use it, you'll need to write something like this:
var _priceField = mywindow.findChild("_priceField");
_priceField.setValidator(mainwindow.priceVal());
This will restrict input in _priceField to the proper precision. Search for Validator on the GUIClient class page of the xTuple ERP Programmer Reference for the complete set of pre-defined validators.
We have partially exposed the QDoubleValidator class to scripting. If none of the pre-defined validators meets your need, you can define your own validators for doubles (that is, numeric values with a decimal point). You'll have to use the properties of the QDoubleValidator to control its behavior. See Qt's QDoubleValidator docs for the list of properties. Here's an example that lets you enter any number between 0 and 1, to a precision of 5 decimal places:
var _smallNumberField = mywindow.findChild("_smallNumberField");
var _smallNumberVal = new QDoubleValidator(mywindow);
_smallNumberVal.bottom = 0;
_smallNumberVal.top = 1;
_smallNumberVal.decimals = 5;
Second: vendor item numbers
This isn't a pattern-matching case so a QValidator is the wrong tool for the job. I'll guess that picking a vendor item number from a list might work for you. If so, consider using an XComboBox instead of an XLineEdit for the vendor item number. Here's one way to do so:
var _vend = mywindow.findChild("_vend"); // a VendorCluster
var _venditem = mywindow.findChild("_venditem"); // an XComboBox
...
function sHandleVendorChange()
{
if (_vend.id() < 0)
_venditem.clear(); // if no vendor is currently selected, clear the list
else
{
// list all vendor item numbers for which the selected vendor is an item source
var params = new Object;
params.vend_id = _vend.id();
_venditem.populate("SELECT itemsrc_id, itemsrc_vend_item_number, itemsrc_vend_item_number"
+ " FROM itemsrc"
+ " WHERE (itemsrc_vend_id=<? value('vend_id') ?>"
+ " ORDER BY itemsrc_vend_item_number;",
params);
}
}
...
// refresh the list of vendor item numbers when a vendor is selected
_vend["newId(int)"].connect(sHandleVendorChange);
...
If you do this, then you may have to use the _venditem.id() as a parameter to an SQL query elsewhere in your script to get the information you really want, which might be your item number or the internal item_id or ...
Gil
In my script, the code snippet in the second example throws an exception
and reports "ReferenceError: QValidator is not defined."
That would seem at odds with your claim of visibility.
What am I missing?
In my script, the code snippet in the second example throws an exception
and reports "ReferenceError: QDoubleValidator is not defined."
That would seem at odds with your claim of visibility.
What am I missing?
In my script, the code snippet in the second example throws an exception
and reports "ReferenceError: QDoubleValidator is not defined."
That would seem at odds with your claim of visibility.
What am I missing?
In my script, the code snippet in the second example throws an exception
and reports "ReferenceError: QDoubleValidator is not defined."
That would seem at odds with your claim of visibility.
Also, if I write "_newLeadTime.setValidator(mainwindow.orderVal());
(orderVal appears in http://www.xtuple.org/sites/default/files/dev/350/html/class_g_u_i_clien...)
I get an uncaught exception "cannot call orderVal(): unknown return type "QIntValidator"
What am I missing?
What version of the xTuple ERP app are you running (version #, not edition)?
Gil
Just what I needed - thanks!
L
L,
You aren't missing anything. I didn't test sufficiently before posting the code snippets.
On the other hand, you should still be able to use the pre-defined priceVal(), which was one of the problems mentioned in the original post. I tested the following with both 3.5.1 and 3.6beta (coming soon):
_smallNumberField.setValidator(mainwindow.priceVal()); // this works var _val = new QDoubleValidator(); // this throws an exception
QIntValidators haven't been exposed to scripting even though the GUIClient::orderVal() method has, so the script engine doesn't know what to do with the validator returned by mainwindow.orderVal().
Please enter two issues in Mantis, one for allowing the creation of new QDoubleValidators and the other for making QIntValidators generally available to scripts.
Gil
Trying to do a validation on the Tracking Number field on the shipOrder screen. Tried the following based on the example above:
var _trackField = mywindow.findChild("_tracknum");
_trackField.setValidator(mainwindow.qtyVal());
I'm getting this error:
Uncaught exception at shipOrder:6: TypeError: Result of expression '_trackField.setValidator' [undefined] is not a function.
Dustin,
The _tracknum on the shipOrder screen is an XComboBox. Unfortunately, setValidator() has not been exposed to scripting for XComboBoxes.
Gil







