I am working in Wix Studio and have a form that loads data from a collection into a datePicker. The element is set to show today’s date but it is overwritten on page load to show what is in the collection using Velo to set the value…as that is the only way to get .value setting to work in Velo.
If there is no value in the collection, it leaves the date as today’s date - but I would like this to be blank in this situation. Is there a way to do that? There is nothing obvious in the API. A null value displays today’s date.
If I modify the display element to show nothing as the initial value, then you completely lose the ability to write to the element in Velo…it remains blank, regardless of what you assign to it…so that’s not an option.
Have you tried changing the settings on the date element? You can set it to “show nothing”.
If you connect the date element via a dataset, it will pull the date that is saved in the database. If there is no date, then it will display no date when the settings are set to “show nothing” when the dataset settings are set to “read and write”.
As long as the field is not marked as “required”, then it should remain blank.
Hi Nayeli. I have tried changing the element…that is actually the cause of my problems…which led to the question. The element doesn’t behave quite as I’d expect when you try to write a new value to it.
If I set it “show nothing”, I can’t write a new value to it.
This image shows it happening. Three date pickers, all set to show my birthday (9th June) but only the middle works.
The code is simplified for this example, rather than introducing the complexity of the collection and arrays that I am building.
$w.onReady(function () {
let myBirthday = new Date(2023, 5, 9);
$w('#datePicker1').value = myBirthday
$w('#datePicker2').value = myBirthday
$w('#datePicker3').value = myBirthday
});
I am totally confused. If you have an idea for a code workaround, that would be fantastic. I can’t be the first person to try to figure this out but the only reliable workaround I can think of is using a button with an alphanumeric representation of the date as it’s label, and using it to bring up a lightbox with a just a datapicker in it, for editing a date, and storing the value back to the label afterwards. Seems mad though.
I am building a form that allows a user to edit existing data, before updating it in Contacts (this is also why I am not using/cannot use a dataset). I need to be able to keep blank fields blank, if the user doesn’t populate one that starts out that way.
That would not be how you save a new value inside of the database. You can set it to display whatever date you want for the placeholder ---- -but if you want it to save a new value via code, you have to set field value for that.
If it’s a value that no one has to actually “see”, then you can skip displaying the Date Picker altogether. You can use a simple Text element to display any date you’d like ---- and set field value to insert a new value for that field.
My actual use case is I pull data from Contacts. There are several date fields, all using date pickers. Some have values in Contacts and some don’t…which are populated changes from contact to contact. All must be editable.
I want to load them if they’re there in the array I build from Contacts. If they’re not in there, I need to allow the user to pick a date still, if they choose to do so, before saving.
If I knew which date pickers would be unpopulated on page load, it would be easy, I’d just use set the initial load for those as blank, but which of them are populated/empty is completely random.
Maybe I put two datepickers in for each date and show one if there’s a date to populate and the other if it’s to be blank…that’s not too much work.
Really would be nice to be able to write a value, that showed up, to a date picker that was initially loaded as empty though…
I’ll manage…sometimes it’s good to have someone to bounce things off…so thanks for that.
I’m sorry. My pregnancy brain understood you did not connect the element to a dataset. But it decided to skip the part where you aren’t even using a dataset on the page at all to pull data from it.
I don’t know what API you are using. But as long as it lets you read and write, you will have to set the value via code.
So whether it’s set field value, or append, or update, etc. All the APIs will tell you how to set the value.
What is consistent though, is you cannot set an element to equal a value then tell your code to equal that element. You just need to tell your code what the value is directly.
Lol…didn’t know you were pregnant. Congratulations.
I am not having any trouble writing the changed Contact back…that bit is easy, once I can figure out what the user has changed, without misleading them on what is actually in the contact record in the first place. I think I have this now though.
Am I right in thinking that this is the only input element where you can’t write a value to it and then read back what you wrote? I can’t think of another one where this is the case…
So I think all elements work the same. You can set the value and placeholder, but I don’t think any element will ever return the value you set it to be. (With or without a dataset being involved in the mix.)
As far as I remember Wix teaching me, always set the value via code never retrieve from an element you assigned a value to (because it may return no value or the value of the original element’s placeholder value such as a video or image). The only time you can retrieve an actual value is when a person physically interacted with the element and added the value themselves.
They also taught me, set element to Read Only if you don’t desire any interaction but one can also avoid using an input element as Read Only by simply using regular text elements to display data that does not need to be updated.
And when using a dataset, try not to mix Read Only elements with Read and Write elements to avoid errors in saving.