Ember-validation how to implement lazy validation - validation

I am using ember-cli:2.5.0 and ember-validations:v2.0.0-alpha.5
In my ember-component i have a validation which is running automatically for each change in a attribute but i want to run this validation only if i call "validate()" method in technical term call validation lazily.
Please find the below code samples,
import Ember from 'ember';
import EmberValidations, { validator } from 'ember-validations';
export default Ember.Component.extend(EmberValidations, {
didReceiveAttrs() {
this.set('newBook', this._bookModel().create());
},
_bookModel(data = {}) {
return Ember.Object.extend(EmberValidations, {
bookVersion: null,
isEditable: false,
validationActive: false,
validations: {
bookVersion: {
inline: validator(function() {
if(this.validationActive){ //Here this.validationActive always return undefined
var version = this.model.get('bookVersion') || "",
message = [];
if (Ember.isEmpty(bookVersion)) {
message.push("Book Version is mandatory!!!");
}
if (message.length > 0) {
return message.join(',');
}
}
})
}
}
}, data);
}
});
actions: {
this.get('newBook').set("validationActive",true);
this.get('newBook').validate().then(() => {
//Do the action
}
}
I want the above validation to run only calling "this.get('newBook').validate()". I am entirely new to ember so down-voter please put your comments before down-voting for others kindly let me know for any more code samples.
Your help should be appreciable.

The addon you are using for validations ("ember-validations") is a very popular one and its documentation is pretty well when compared to others. If you look at the documentation there is a part named "Conditional Validators" in documentation. You can use a boolean property to control when the validation is to be performed.
You can see an illustration of what I mean in the following twiddle. I have created a simple validation within the application controller for user's name. The name field must have a length of at least 5 and the validation is to be performed only if the condition validationActive is true. Initially; the condition is false; which means validator did not work and isValid property of Controller (which is inherited from EmberValidations Mixin) is true. If you toggle the property with the button provided; the validation will run (since the condition is now set to true; hence validation is triggered) and isValid will return to false. If you change the value; the validation result will change appropriately with respect to the value of user's name. If you toggle the condition once again to set it to false; then isValid will become true no matter what the valie of user's name is.
I hope this gives you an insight about how to control when your validations should work.
Here is what you should do after your edit: The field is undefined because you are trying to reach component's own validationActive field within inline validator. Please get validationActive as follows this.model.get('validationActive') and give a try. It should work.

Related

How to restrict SlickGrid to make a API call, while clicking or changing compound filters?

I have a SlickGrid Table, in which there are compound filters, currently when i try to change the compound filter (lets say from Equal To to Less Than), then it makes an API call.
I don't want to make an API call, how do i achieve this?
I searched in slickgrid docs, but couldn't find any property(if it is available).
Image
Please note that I'm the author of Angular-Slickgrid
So I looked at the problem you're having and it seems like a valid problem to look into, I agree that for some filters like the Compound Date Filter Operator we shouldn't query right away, that is after changing a the operator dropdown without providing a date. So, for that reason I am adding a new grid option skipCompoundOperatorFilterWithNullInput which will avoid triggering a filter change (it will also avoid querying the backend when implemented) when we first change the operator dropdown without providing a date being entered.
Note that this new option will only be available with Angular-Slickgrid v5.1.0+ (via this PR, now supports this and it will only be enabled by default on the Compound Date Filter (any other filters will have to explicitly enable this new flag either via grid option or via a column definition).
What if I cannot upgrade to 5.1.0? Are there any other ways of dealing with this?
Yes, it's just a bit more involving dealing with this though, it however requires a lot more work from your side. The information you need to know is that nearly every piece of code from Angular-Slickgrid and Slickgrid-Universal are protected TypeScript classes and functions which mean that you can simply use TypeScript to extends any of them. Let's take for example the CompoundDateFilter class, we could extend it this way to skip the callback triggering without a date provided (this._currentDate)
import { CompoundDateFilter, OperatorString } from '#slickgrid-universal/common';
export class CustomCompoundDateFilter extends CompoundDateFilter {
protected onTriggerEvent(e: Event | undefined) {
if (this._clearFilterTriggered) {
this.callback(e, { columnDef: this.columnDef, clearFilterTriggered: this._clearFilterTriggered, shouldTriggerQuery: this._shouldTriggerQuery });
this._filterElm.classList.remove('filled');
} else {
const selectedOperator = this._selectOperatorElm.value as OperatorString;
(this._currentValue) ? this._filterElm.classList.add('filled') : this._filterElm.classList.remove('filled');
// -- NEW CODE BELOW -- (to skip triggering callback on undefined date)
// when changing compound operator, we don't want to trigger the filter callback unless the date input is also provided
if (this._currentDate !== undefined) {
this.callback(e, { columnDef: this.columnDef, searchTerms: (this._currentValue ? [this._currentValue] : null), operator: selectedOperator || '', shouldTriggerQuery: this._shouldTriggerQuery });
}
}
this._clearFilterTriggered = false;
this._shouldTriggerQuery = true;
}
}
then use this new custom filter class in your column definitions
import { CustomCompoundDateFilter } from './custom-compoundDateFilter';
initGrid() {
this.columnDefinitions = [{
id: 'start', name: 'Start', field: 'start',
filterable: true, filter: { model: CustomCompoundDateFilter },
}];
}
and there you have it, below is a proof that it is working since I changed the operator and as you can see below this action is no longer resulting in 0 row returned. However if I had done the inverse, which is to input the date but without an operator, it would have execute the filtering because "no operator" is defaulting to the "equal to" operator.

Check for element being present then check if its displayed

I need to check if the pop up exists, if it does then I need to check if its displayed then perform certain action on it.
I have implemented the below. I was wanting to know if there is any better way of achieving this.
licenseUpdate.isPresent().then(function (item) {
if (item == true) {
licenseUpdate.isDisplayed().then(function (res) {
if (res == true){
licenseUpdate.click();
};
});
}
});
If you are using page object (you should) you can write something like this:
clickLicenseUpdate() {
const licenseUpdate = $(licenseUpdateCssSelector);
return licenseUpdate.isPresent()
.then((isPresent) => {
if (!isPresent) { return false; }
return licenseUpdate.isDisplayed();
})
.then((isDisplayed) => {
if (!isDisplayed) { return false; }
return licenseUpdate.click().then(() => true);
})
}
Note that if you are using and old JS version (you shouldn't) you need to replace arrow functions with traditional anonymous functions.
Some helpful links about Page Object Design Pattern:
PageObjects
Martin Fowler PageObject
Code explained (or at least, that is the plan):
Using $ to locate an element but you can use any strategy
supported by Protractor.
browser.findElement(by.className('license')) equivalent to
$('license'), browser.findElement(by.id('license')) equivalent to
$('#license'). Check Protractor documentation for more examples.
Once you a have found a web element that match your locator, you can
use isPresent method to determine whether the element is present on
the page. isPresent returns a promise that resolve to a boolean
value.
then always return a promise. You can return a primitive value from
onFulfilled callback and that value would be cast to a promise with
resolve with same value. That is what is done here: if (!isPresent) { return false; }; or you can return another promise
from onFulfilled callback and the promise returned by then will be resolved or rejected with same value of returned promise.
That is what is done here: return licenseUpdate.isDisplayed();. isDisplayed() also return a
promise that will resolve with whether this element is currently
visible on the page.
This can be a bit overwhelming if you are not
used to deal with promises. Check this out Promises/A+
Finally, if the element is present and is displayed, click the element with theclick method that, surprise, also return a
promise (WebDriverJS API is based on promises).
Note that if element is not present, isPresent is false in this
line if (!isPresent), returning false immediately bypass
licenseUpdate.isDisplayed() execution and resolve with a false value. In that
case isDisplayed value is false and again false is returned
immediately bypassing the licenseUpdate.click() execution.
Also note that clickLicenseUpdate return a promise that will
resolve to false if the element is not present or if is present but not
displayed. To keep clickLicenseUpdate returned value consistent, I recommend you to wait for licenseUpdate.click() and then return a boolean value as it is done here: return licenseUpdate.click().then(() => true); (using implicit return from arrow functions) because promise returned by click() resolve with a void value.
That is harmless but is considered a good practice maintain a consistent return value, always a boolean value, not sometime a boolean and others a void value.

grails 2.4.2 - timepoint of validation of domain object

I have got a domain class with some custom validators like the following:
class Domain {
String attribute1
OtherDomain attribute2
static constraints = {
attribute2 nullable: true, validator: {OtherDomain od, Domain d ->
if (od) {
log.debug "entering validation"
// certain validation here
}
}
}
For updating I have got a simple action in the corresponding DomainController:
#Transactional
def update(Domain domainInstance) {
log.debug "entering update()"
// rest of update
}
I'm wondering why in my debuglog I get the debug messages in the following order:
entering validation
entering update()
The problem is that the validation fails at this point (StackOverflowError). I know the reason for this error and I know what to do to circumvent this error (doing so within update action). However, I don't know why there is a validation before the programme even gets into the update() action. And I don't know how to prevent a validation at this point.
Do you have any suggestions?
The reason you see the "entering validation" message before "entering update()" is because you have declared a command object of type Domain. This means that Grails will bind any request parameters to domainInstance and then call validate() on it, before the action is executed. This allows you to write code like this:
#Transactional
def update(Domain domainInstance) {
// at this point request params have been bound and validate() has
// been executed, so any validation errors will be available in
// domainInstance.errors
if (domainInstance.hasErrors() {
// do something
}
log.debug "entering update()"
}

angular-schema-form - Custom validation message is not shown after form validation

In my form I have the following definition for a custom validation:
'staff[].title': {
key: 'staff[].title',
validationMessage: {
'needTitle': function() { console.log('DEBUG!'); return 'Need title';}
},
$validators: {
needTitle: function (value) {
return value ? true : false;
}
}
}
When I validate my form by $scope.$broadcast('schemaFormValidate'); the staff[].title field indicates there is an error which is great, but its error message is never shown. However, if I also broadcast the event scope.$broadcast('schemaForm.error.staff[].title,...) only then the error message appears. The trouble with this technique is that I have to broadcast the event once more with a true value, $scope.$broadcast('schemaForm.error.staff[].title,...,true) to set the field state back to normal.
Any ideas on how to do this with just $scope.$broadcast('schemaFormValidate');?
The annoying part with the current I do is that I have two broadcast two events to validate the form have my custom valuation messages shown!
Any help is greatly appreciated.

jQuery .toggle(showOrHide): implementation question

I'm having a button toggle whether a referenced div is visible or not. Originally, I was using the code:
$('#search-options-btn').click(function() {
if ($('#search-options').is('.hidden')) {
$('#search-options').removeClass('hidden');
} else {
$('#search-options').addClass('hidden');
}
});
However, in an attempt to find cleaner code, I came across the jQuery toggle() method, which according to the API has a method implementation
.toggle( showOrHide )
showOrHide: A Boolean indicating whether to show or hide the elements.
This description leads me to believe this is a shortcut implementation method for showing or hiding by passing the...identifier? showOrHide into the toggle() method.
Of course, attempting this:
$('#search-options-btn').click(function() {
$('#search-options').toggle(showOrHide);
});
yields an error in my firebug console:
showOrHide is not defined
[Break On This Error] $('#search-options').toggle(showOrHide);
I've also tried defining showOrHide as a boolean initialized to false; the error goes away, but the issue is not fixed.
According to the jQuery online API, this is supposed to be equivalent to
if ( showOrHide == true ) {
$('#foo').show();
} else if ( showOrHide == false ) {
$('#foo').hide();
}
unless I'm completely missing how this works. Can anyone fill me in on what I'm doing wrong here? I haven't been able to find a similar implementation.
you should just need toggle(), nothing else.
$('#search-options-btn').click(function() {
$('#search-options').toggle();
});
showOrHide is the internal name. You pass in a bool:
$('#...').showOrHide(true)
if you want the item to be visible, false if you want it hidden.
Its just toggle between class and not with boolean..
$('#search-options-btn').click(function() { $('#search-options').toggle('yourClassName'); });
If yourClassName is found, then it will remove the same, otherwise it will add it.

Resources