PDA

View Full Version : Property Grid - interdependent cell validation



JoshS
18 Jan 2011, 4:39 AM
Hello,

tl;dr: Is there a way to:


Show validation errors on fields that aren't being edited in a property grid (red line) and
A way to trigger validation for the entire grid and/or loop over all the cells and fire validation for each one?


I have a property grid that looks something like this:

Name - TheFieldName
Show Label - T/F
Label - The label text.

The grid is populated with data from a store of Ext.data.Records. The validation function for each field can be defined elsewhere in the application, and is stored as a string in the record.

Currently validation is basic per cell checking, i.e.



[...]
if (label.length < 5) {
return 'Not long enough.';
} else {
return true;
}
[...]
What I would like is the ability for each field to become invalid dependent on another cell's value, e.g.



[...]
if (!showLabel && label.length > 0) {
return 'Label shouldn't be defined because show label is set to false!';
} else {
return true;
}
[...]
Currently I pass a function that evals a function call to another piece of non-extjs code for variable access. It also evals the validation function stored in the record (in here we have the variables it needs access to, since these functions could be rather complex in the future), and runs that. validatePropertyValue returns true or an error string as ext expects, and has access to the grid/store/records, etc.

(Is it possible to store functions in a record object rather than a string?)



editor.field.validator = function(value){
var valid = true;
valid = eval("path.to.function.validatePropertyValue(value, property, store);");
return valid;
}
Now then, finally to the problem I am having using the code above:



[...]
if (!showLabel && label.length > 0) {
return 'Label shouldn't be defined because show label is set to false!';
} else {
return true;
}
[...]
The above code is set as the validation function for 'label'. If I set the 'showLabel' combo box to 'false' and have text in the label field, then the label field doesn't show any sign of being invalid unless I click on it (i.e. am actively editing it).

Is there a way to:
1. Show validation errors on fields that aren't being edited in a property grid (red line) and
2. A way to trigger validation for the entire grid and/or loop over all the cells and fire validation for each one if 1. is possible? I think there are several ways to do this - firing events, .validate(), .validateValue() and several others, but of course I don't know if they work as I can't see the validation failing without having to click on them.

Thanks!

Foster
18 Jan 2011, 5:50 AM
I havent done so with property grids in my app, but my general approach has been:
on save click, call:


if (!this.getForm().isValid()) {
... //do whatever Ext.Msg.alert(), etc you want for invalid forms
}


To get the error messages out, which I have set on my fields, I've been looping through the items calling


TextField.getActiveError()

so that I can show one message on the screen which lists all of the errors

Foster
18 Jan 2011, 6:03 AM
Also have you looked at this Property & function from the API for PropertyGrid. This may help solved your first two questions about doing validation on fields that arent being edited

forceValidation (http://dev.sencha.com/deploy/dev/docs/source/EditorGrid.html#cfg-Ext.grid.EditorGridPanel-forceValidation) : BooleanTrue to force validation even if the value is unmodified (defaults to false)



validateedit (http://dev.sencha.com/deploy/dev/docs/source/EditorGrid.html#event-Ext.grid.EditorGridPanel-validateedit) : ( Object e ) Fires after a cell is edited, but before the value is set in the record. Return false to cancel the change. The edit ...
Fires after a cell is edited, but before the value is set in the record. Return false to cancel the change.