ui-grid icon indicating copy to clipboard operation
ui-grid copied to clipboard

How to invalidate an editable cell

Open himsraw opened this issue 11 years ago • 8 comments

I am using the ui-grid for data entry. If user enters invalid data in an editable cell the user should not be allowed to move out of the cell. How can i restrict the user to stay on the cell and don't allow him to move out untill he enters a valid value? I am currently doing validation check in "afterCellEdit" event of grid cell. But this lets the user to move out of cell even after entering a invalid value. Any help would be welcome.

Thanks

himsraw avatar May 18 '15 09:05 himsraw

The best way to do this would be to write an editable directive that applies validation before providing the after cell edit event. It would also need to prevent any navigation away from the field, perhaps using the blur event.

The directive would need to be broadly similar to the existing input directive in the edit feature.

PaulL1 avatar May 18 '15 18:05 PaulL1

Hi @PaulL1 thanks for your suggestion. I have created a directive but i am not sure what needs to be done to keep the current cell in focus and not allow user to move out. Could you please help with this? I am pretty new to Angularjs as well.

image

.directive('validateRequiredCell', function(uiGridEditConstants) { return { restrict: 'A', scope: false, require: 'ngModel', link: function (scope, element, attrs, ngModel) { element.on('blur', function () { if (ngModel.$viewValue > 200) { scope.inputForm.$valid = false; } }); } }; });

himsraw avatar May 19 '15 06:05 himsraw

DOM's not really my thing. I suspect you can cancelDefault or preventPropagation, they aren't angular specific things but rather browser event handling. I think if it works how I imagine you'd change to:

(FYI you can format as code by doing ` three times on a blank line either side of your code)

  element.on('blur', function ( event) {

and then you may be able to do something like:

  event.cancel
  event.preventDefault

or some such.

PaulL1 avatar May 19 '15 06:05 PaulL1

@himsraw did you have any luck? Thanks.

c0bra avatar Jun 19 '15 18:06 c0bra

Not really. I am using the following in directive:

        element.bind('blur', function (evt) {
            if (validationService.valid == false && $.trim(validationService.valid) != '') {
                //alert(validationService.notificationMessage);
                element[0].focus();
                evt.stopImmediatePropagation();
                evt.preventDefault();
            }

        });

This locks the control on the invalid cell when user tries to move out using 'TAB' key but if user clicks on next cell (editable or uneditable) it allows the user to move out of invalid cell. So basically its working on TAB out but not mouse out. I don't know the reason why this is happening?

himsraw avatar Jun 22 '15 06:06 himsraw

some plunker? i need the same enhancement, i have a validation directive, but i dont know how to restrict my input or how to show to user that the input is wrong. Thank you all.

ghost avatar Sep 29 '15 13:09 ghost

Did someone manage to solve this? I need to leave the invalid cell in edit mode as well (and show a validation div with message near it, which is out of scope of this issue) but not prevent the user from moving out of it. FYI, my cell has a textbox which only accepts numbers and is invalid if empty value is entered. I was able to do it as himsraw pointed above (using stopImmesiatePropagation only) but in that case, if value is empty, TAB cancels edit but clicking elsewhere leaves the cell in invalid state (these two are inconsistent behaviors). What's worse, the latter somehow messes up the cell because after that it only accepts a single number (which fixes the validation error) but nothing more and it cannot be focused or written into anymore... I would be happy if someone could propose a working solution :|

MatejQ avatar Nov 09 '15 15:11 MatejQ

A proposal of validation is in #4152

imbalind avatar Nov 12 '15 22:11 imbalind