New row in slickgrid, Validator does not fire - slickgrid

I have the requiredFieldValidator configured for one of my columns and it works as expected for existing rows. When I tab thru, or click to begin editing a new row, the validator does not fire when moving off the column. When move off the new row (with no data in the column) so that it commits, and then move back to the row, now the validator works...but ONLY if I put some data in the editor, and then delete it.
In other words, for new rows, the requiredFieldValidator does not do anything until I actually type something in the column. If I leave a column blank it never fires, defeating the purpose of it. Interestingly, the example they provide (http://mleibman.github.com/SlickGrid/examples/example3-editing.html) exhibits the same problem.

This checks for both values being "". I believe this should return true since a validator could be a "required" validator.
this.isValueChanged = function () {
// this needs to validate if both are empty since a validator might be a "required" validator
if ($input.val() == "" && defaultValue == "")
return true;
else
return (!($input.val() == "" && defaultValue == "")) && ($input.val() != defaultValue);
};
Preferred method would be to create a custom editor so you aren't mucking with the core library.

I concur, just encountered this behavior myself.
Here is the solution.
Change this.isValueChanged function contents to the following.
if(($input.val().length > 0) && ($input.val() != defaultValue))
return true;
else if(($input.val() == defaultValue)&&($input.val().length > 0))
return true;
else if(($input.val().length == 0) && ((undefined === defaultValue )||(defaultValue == "")))
return true;
else
return false;
For some reason defaultValue is not set during the first interraction, my guess if you initialize it to "" you would not need to check for undefined.
At any rate defaultValue holds the previous value of the cell, so when it is empty and it is the first cell being edited it is undefined, after that it is set to "" for all other empty cells.
The problem is this function needs to tell the grid that the value has changed in order for the cell editor to validate the input. In the provided implementation the expression always returns false because
$input.val() == "" evaluates to true and then the expression
!($input.val() == "" && defaultValue == null)) evaluates to false
This means defaultValue is continuously undefined.
Hope that makes sense and my code helps you out.

Had the same problem and I believe that ideally defaultValue should be set to undefined or null, when it wasn't initialized. So I've modified TextEditor in a following way:
function TextEditor(args) {
var $input;
var defaultValue;
var scope = this;
this.loadValue = function (item) {
defaultValue = item[args.column.field] || null;
$input.val(defaultValue);
$input[0].defaultValue = defaultValue;
$input.select();
};
this.isValueChanged = function () {
return $input.val() != defaultValue;
};
....
setting default defaultValue to null and simplifying isValueChanged.

There is an error in the first else if of Roman, the return is false. Otherwise, any change in cell returns true even if the value has not been changed.
I tried this and it works.
this.isValueChanged = function () {
var attuale = $input.val();
if((attuale.length > 0) && (attuale != defaultValue))
return true;
else if((attuale == defaultValue)&&(attuale.length > 0))
return false;
else if((attuale.length == 0) && ((undefined === defaultValue )||(defaultValue == "")))
return true;
else
return false;
};

Related

AssertionError on ArrayList with same output

I know that this topic has been asked by many times and I search for all possible solutions but unfortunately nothing solves my problem.
Here's my test case:
#Test
public void whenFindAllBy_thenReturnListofViewPlanDetailDto() {
java.sql.Date startDate = new java.sql.Date(new Date().getTime());
java.sql.Date endDate = new java.sql.Date(new Date().getTime());
Plan planA = new Plan();
planA.setName("Plan A - 2018");
entityManager.persist(planA);
entityManager.flush();
Module moduleA = new Module();
moduleA.setName("CSS");
moduleA.setDescription("CSS is a cornerstone technology of the World Wide Web, alongside HTML and JavaScript.");
entityManager.persist(moduleA);
Module moduleB = new Module();
moduleB.setName("HTML");
moduleB.setDescription("Hypertext Markup Language is the standard markup language for creating web pages and web applications.");
entityManager.persist(moduleB);
PlanDetail planDetailA = new PlanDetail();
planDetailA.setInstructor("Mozilla Firefox Foundation");
planDetailA.setStartDate(startDate);
planDetailA.setEndDate(endDate);
planDetailA.setModule(moduleA);
planDetailA.setPlan(planA);
entityManager.persist(planDetailA);
PlanDetail planDetailB = new PlanDetail();
planDetailB.setInstructor("W3 Schools");
planDetailB.setStartDate(startDate);
planDetailB.setEndDate(endDate);
planDetailB.setModule(moduleB);
planDetailB.setPlan(planA);
entityManager.persist(planDetailB);
entityManager.flush();
List<ViewPlanDetailDto> plandetails = new ArrayList<>();
plandetails.add(new ViewPlanDetailDto(planDetailA.getId(), planDetailA.getModule().getName(), planDetailA.getModule().getDescription(), planDetailA.getInstructor(), planDetailA.getStartDate(), planDetailA.getEndDate()));
plandetails.add(new ViewPlanDetailDto(planDetailB.getId(), planDetailB.getModule().getName(), planDetailB.getModule().getDescription(), planDetailB.getInstructor(), planDetailB.getStartDate(), planDetailB.getEndDate()));
assertEquals(planRepository.findAllBy(planA.getId()), plandetails);
}
Stacktrace:
java.lang.AssertionError: expected: java.util.ArrayList<[ViewPlanDetailDto(detailId=1, name=CSS, description=CSS is a cornerstone technology of the World Wide Web, alongside HTML and JavaScript., instructor=Mozilla Firefox Foundation, startDate=2018-07-06, endDate=2018-07-06), ViewPlanDetailDto(detailId=2, name=HTML, description=Hypertext Markup Language is the standard markup language for creating web pages and web applications., instructor=W3 Schools, startDate=2018-07-06, endDate=2018-07-06)]> but was: java.util.ArrayList<[ViewPlanDetailDto(detailId=1, name=CSS, description=CSS is a cornerstone technology of the World Wide Web, alongside HTML and JavaScript., instructor=Mozilla Firefox Foundation, startDate=2018-07-06, endDate=2018-07-06), ViewPlanDetailDto(detailId=2, name=HTML, description=Hypertext Markup Language is the standard markup language for creating web pages and web applications., instructor=W3 Schools, startDate=2018-07-06, endDate=2018-07-06)]>
What I try:
Override equals on PlanDetail, ViewPlanDetailDto, Plan
but it all failed.
Equals and Hashcode overrides:
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof ViewPlanDetailDto))
return false;
ViewPlanDetailDto other = (ViewPlanDetailDto) obj;
if (description == null) {
if (other.description != null)
return false;
} else if (!description.equals(other.description))
return false;
if (detailId == null) {
if (other.detailId != null)
return false;
} else if (!detailId.equals(other.detailId))
return false;
if (endDate == null) {
if (other.endDate != null)
return false;
} else if (!endDate.equals(other.endDate))
return false;
if (instructor == null) {
if (other.instructor != null)
return false;
} else if (!instructor.equals(other.instructor))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (startDate == null) {
if (other.startDate != null)
return false;
} else if (!startDate.equals(other.startDate))
return false;
return true;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((description == null) ? 0 : description.hashCode());
result = prime * result + ((detailId == null) ? 0 : detailId.hashCode());
result = prime * result + ((endDate == null) ? 0 : endDate.hashCode());
result = prime * result + ((instructor == null) ? 0 : instructor.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((startDate == null) ? 0 : startDate.hashCode());
return result;
}
When I try to assert it it always fails even though the output is identical.
Based on IntelliJ's comparison failure, it highlighted on the trailing space on the expected part which I don't get how it ended having a trailing space.
You probably override equals() incorrectly .
To understand and correct your issue, you should start by the base : unit testing your equals() method (and by the way think of overriding hashCode() to be consistent with the equals() contract).
Whatever, overriding equals() by specifying all instance fields of the class to do some assertions in an unit test is generally something that you can avoid and that you have to if it gives an undesirable behavior to equals().
equals() has a semantic defined in Object.equals() :
Indicates whether some other object is "equal to" this one.
You should stick to that.
Generally I use a unit testing matcher library such as Harmcrest or AssertJ to perform assertions on the object's field in a non intrusive while being simple and clear.
With AssertJ, your assertion could look like :
Assertions.assertThat(planRepository.findAllBy(planA.getId()))
// assert result size
.hasSize(2)
// extract a field to assert
.extracting(ViewPlanDetailDto::getPlanDetail)
// extract still finer fields to assert
.extracting(PlanDetail::getId, p -> p.getModule().getName(), p -> p.getModule().geDescription(), ... other fields to assert)
// declare values expected
.containsExactly(Tuple.tuple(planDetailA.getId(), "CSS", "CSS is a cornerstone technology of the World Wide Web, alongside HTML and JavaScript.",
planDetailB.getId(), "HTML", "Hypertext Markup Language is the standard markup language for creating web pages and web applications.",
... other expected tuples ));

Does C# 6 Elvis operator (null propagation) short circuit

Why this c# code throws a null exception?
bool boolResult = SomeClass?.NullableProperty.ItsOkProperty ?? false;
IsnĀ“t elvis operator supposed to stop evaluation (short circuit) once the NullableProperty evaluates to null?
In my understanding the line of code above is a shortcut for:
bool boolResult
if(SomeClass != null)
if(SomeClass.NullableProperty != null)
boolResult = SomeClass.NullableProperty.ItsOkProperty;
else
boolResult = false;
else
boolResult = false;
Did I assume wrong?
EDIT: Now I understand why I get it wrong, The line of code actually translates to something similar to:
bool boolResult
if(SomeClass != null)
boolResult = SomeClass.NullableProperty.ItsOkProperty;
else
boolResult = false;
And throws because NullableProperty is null...
You need to chain, since the NRE is on the second reference:
bool boolResult = SomeClass?.NullableProperty?.ItsOkProperty ?? false;

Code to validate radio buttons?

Can any of you supply me with some code that will validate a set of radio buttons, show a confirm box, displaying the selected option to the user, and then proceed or stop based on what the user selects?
Thanks,
James
var Option Value = document.getElementById('ID').checked;
var Option Value = document.getElementById('ID').checked;
var Option Value = document.getElementById('ID').checked;
if(Option Value == false && Option Value == false && Option Value == false) {
msg += "You must select an option";
document.getElementById('ID').style.color="red";
return false;
}
var selected;
if(Option Value == true) {
selected = "Option Value";
}
if(Option Value == true) {
selected = "Option Value";
}
if(Option Value == true) {
selected = "Option Value";
}
if(Option Value == true || Option Value == true || Option Value == true) {
var confirmed = confirm("Are you sure you have selected the correct option?" + " You have selected: " + selected)
}
if(confirmed == false) {
result = false;
}
Hope it helps!

How to set a Javascript variable from my View using a ViewBag property

I would like to set a JavaScript variable in my view (inside a script block) based on the existences of a ViewBag property from the controller... like:
var doAboutTab = #(ViewBag.DoAboutTab != null)
This generates a JavaScript error of:
var doAboutTab = False
'False is not defined'
#if(ViewBag.DoAboutTab != null)
{
var doAboutTab ="something";
}else
{
var doAboutTab ="something_else";
}
or
var doAboutTab = '#ViewBag.DoAboutTab' !== '';
The false keyword is lower case in javascript.
var doAboutTab = #((ViewBag.DoAboutTab != null).ToString().ToLower());
ToString() method on bool in C# returns it as "False".Javascript expects "false", you should make it lowercase
var doAboutTab = #((ViewBag.DoAboutTab != null).ToString().ToLower())

Axapta Validation Override Always Executes Twice

In most cases, validation methods I've overridden execute twice each time the parent field is changed. Everything still works, but the InfoLog displays double messages every time.
Is there any way to prevent this?
Thanks
public boolean validate()
{
boolean ret;
int exlowValue;
int lowValue;
int highValue;
int exhighValue;
str errorMessage;
;
ret = super();
//Make sure a numeric value was entered
if (ABC_RegExValidator::validateMe("integer", int2str (ABC_Checks_checkExtremeLow.value())))
{
//get the form values
exlowValue = ABC_Checks_checkExtremeLow.value();
lowValue = str2int(ABC_Checks_checkLow.valueStr());
highValue = str2int(ABC_Checks_checkHigh.valueStr());
exhighValue = str2int(ABC_Checks_checkExtremeHigh.valueStr());
//Extreme Low must be 0 or less than all others
if (exlowValue != 0)
{
//A non-zero value was entered; it must be less than all other fields
if ((exlowValue >= lowValue && lowValue > 0) || (exlowValue >= highValue && highValue > 0) || (exlowValue >= exhighValue && exhighValue > 0))
{
//Return an error
ret = checkfailed(strFmt("#ABC197", int2str(exlowValue)));
}
else
{
//Not greater than any other value
//Success!
ret = true;
} //Greater than all others?
}
else
{
//No errors
ret = true;
} // 0?
}
else
{
//Regular expression failed
//Return an error
ret = checkfailed("#ABC192");
} //Regular expression
return ret;
}
Your description of the problem is not really clear. One can override the valite method on a form control, the validate method on a form datasource and the validatefield method on the table. That's my knowledge of version 3.0.
And how do you mean the "parent field"? I presume the table field?
If I put info messages in each of these methods they only execute once when I modify a value. That's the case in 3.0. I don't know which version you're using.
Maybe you could be more precise about which validation method you're testing?

Resources