How to get previous vals in Yup validation - yup

I would like to validate a list of items(strings) and make sure there isn't a duplication(if there is, show error message) when user tries to modify the item. However, if the user try to modify it and finally keep the same value, we don't show the error message. In validation, I have something like below for each field in the array. In the function inside test below, I can only get current modified value as parameter - value, but not previous value so I cannot do validation with previous value. I would like to know if there is a way to get previous value(i.e. value before any change) for the value currently been modified? If there is only one value in the list, it is easy, but if there are multiples, it is hard to track.
Yup.object().shape({
rows: Yup.array(
Yup.lazy(() => (
myObject.test('field', 'error message', function (value) { ... } )
)
)
)
})

Related

Get value from Instance in Flutter

I am making a search request on the List with the Provider pattern.
List<Device> _devices = [
Device(one: 'Apple', two: 'iphone'),
Device(one: 'Samsung', two: 'Galaxy')
];
And Query is like this
List<Device> queryQuery(String value) {
return _devices
.where((device) => device.one.toLowerCase().contains(value.toLowerCase()))
.toList();
the result I expect to get is iphone when I passed the value Apple.
But the result on the screen that I got is [instance of ‘Device’]
when I code like this
child: Text('${deviceData.getDevice('Apple')}'
I do know I should be using some kind of key using two... but I have no idea :-(
You serialized the wrong object.
What you did end-up being similar to:
Text(Device(one: 'Apple', two: 'iphone').toString());
But you don't want to do Device.toString(). What you want instead is to pass Device.two to your Text.
As such your end result is:
Text('${chordData.chordExpand('Apple').two}')
By the look of [Instance of 'Device'], it seems the function is returning a list so it is a good idea to check if the list is empty or not. if it is not empty, one of the elements is still needed to be selected. I guess it should be Text('${chordData.chordExpand('Apple')[0].two}') in case the list is not empty.
To summarize, use something like this to handle the case when list is empty
// Inside your build method before returning the widget
var l = chordData.chordExpand('Apple'); // Returns a list of devices
String textToWrite; // Here we will store the text that needs to be written
if(l.isEmpty) textToWrite = 'No results'; // If the filter resulted in an empty list
else textToWrite = l[0].two; // l[0] is an instance of a device which has a property called two. You can select any instance from the list provided it exists
return <Your Widget>(
.....
Text(textToWrite),
.....
);

WIX -- Change text in repeater depending on the boolean value in the dataset

I'm creating a dynamic page in Wix which goes pretty well. Only struggling with one thing. I want a textfield in a repeater that is linked to a boolean in a database to display one text or another depending on the state of the boolean.
Because you mentioned you're on a dynamic page, I assume your repeater is connected to a dataset. This makes things a little more complicated because you have to wait for the dataset to load. If you would use onItemReady() to set your field values, the dataset would just overwrite them.
You want to do something like this:
$w.onReady(function () {
$w('#dataset1').onReady( () => {
$w('#repeater1').forEachItem( ($w, itemData, index) => {
if(itemData.boolField){
$w('#boolText').text = "Yes Ma'am!";
}
else {
$w('#boolText').text = "No way Jose!";
}
} );
} );
} );
Basically, you wait for the dataset to be ready, then loop through all the items in your repeater using the forEachItem() callback, and reset the value of the text field based on the value in the boolean field.

Protractor- ElementFinder returning unexpected values

I am writing a protractor test case to compare the name(s) of the displayed data is same as the searched name.
Even though my test case works fine, I am not able to understand what is happening. Because when i expect the name to compare, it compares as expected, but when i print the elementFinder's(rowData)(i have attached the output screen shot here) value in console.log, it show a huge list of some values which i am not able to understand?
PS: I am a newbie to protractor`
This is the testCase:
it('should show proper data for given last name', function () {
var searchValue='manning';
searchBox.sendKeys(searchValue);
search.click();
element.all(by.binding('row.loanNumber')).count().then(function(value) {
var loanCount = value;
for (var i = 0; i < loanCount; i++) {
var rowData = element.all(by.binding('row.borrowerName')).get(i).getText();
expect(rowData).toMatch(searchValue.toUpperCase());
console.log(rowData,'*****',searchValue.toUpperCase());
}
});
});`
And give me valuable suggestions about my style of code
rowData is a promise (not a value), so when you console.log it, you get the promise object
Protractor patches Jasmine to automatically resolve promises within the expect(), so that's how it knows to resolve the value and compare to the expected result.
If you want to console.log the value, you need to resolve the promise with .then() to get the value.
rowData.then(function(rowDataText) {
console.log(rowDataText);
)}
This is pretty much everyone's first question when they start using protractor. You will want to learn how promises work if you want a good understanding of how to manipulate them.

Lift - how to get default value of select using WiringUI

I have an issue with getting default value of select dropdown.
i have fruits val:
val fruits = List("apple", "banana", "other")
and i render a tr with:
<tr id={ theLine.guid }>
<td>
{
SHtml.ajaxSelect(fruits, Full(fruits(0)),
s => {
mutateLine(theLine.guid) {
l => Line(l.guid, l.name, s, l.note)
}
Noop
})
}
</td>
(...)
on page html is rendered correctly with option selected="selected", but when i try to save it to DB i get empty value of fruit. if i change option to 'other' and then i select it back to 'apple', it saves right value.
i add also a println function to addLine to see what values are in this vector, and there is empty value if i dont change fruit option, so i suppose that it is not problem when i process lines to save it to DB.
can you help me with this?
thanks
Gerard
Before you change your select option, you are not triggering the event that calls your function. The function is bound to onChange and that only gets fired when the value changes.
To fix, you could either: Start with an option like "Select a value". This would require the user to change the item, but is the only way to trigger the onchange.
If you don't want to do that, you could add a button and add your logic to a button click handler that would get called when submitted. Something like this should help - you'll need to bind it to your output, either inline as you provided, or via CSS Selectors:
var selected = Full(fruits(0))
SHtml.ajaxSelect(fruits, selected,
s => {
selected = Full(s)
Noop
})
SHtml.ajaxSubmit("Submit", () => {
mutateLine(theLine.guid) {
l => Line(l.guid, l.name, selected, l.note)
}
})

form validation with preg_match

I have created a form which gets user data about the city he leaves. The form stores data in a table called profile. I am using a function which inserts data into profile table. If there are already data, the function just updates the table. If the user does not insert data into city field and press submit, the table continues to keep the previous data, because nothing submitted.
In the form I would like to make a validation so that the user will inserts only a-z or space characters.
I have used the following code for validation:
if( empty($_POST) === false ){
if( empty($errors) === true ){
if( preg_match('/^[a-zA-Z ]+$/', $_POST['city']) ===0 ){
$errors[]='<font color="#963">City must only contains a-z and spaces</font>';
}
}
}
It works! But the problem now is that if the user leave the form empty and press submit the above code enforces him to place data( says: City must only contains a-z and spaces). How can I use the above code to validate the form only if the user enters data. And if not then to continue without any problem.
You can do this without even generating an error. Replace
if( preg_match('/^[a-zA-Z ]+$/', $_POST['city']) ===0 ){
With
$_POST['city'] = preg_replace("&[^a-zA-Z ]&","",$_POST['city']);
This will replace all non-lowercase letters and non-spaces with nothing before it's inserted into the database.
EDIT:
If you'd still like to generate an error, use
if( preg_match("&[^a-zA-Z ]&",$_POST['city']) ){
$errors[]='<font color="#963">City must only contains a-z and spaces</font>';
}
before the preg_replace.