I have a date and time picker set up like this sample. I now want to make these required as well as disable the submit button when they are not filled in (I know how to do this). Default the fields will be blank. I tried with adding the "required" property onto the input field but that keeps marking the field as invalid. Is it possible to achieve this another way and if so how?
We recommend you use ngModel, instead of value, to bind the date and time picker templates' inputs. ngModel does not just give you two-way data binding, but also tells you if the user touched the control, if the value changed, or if the value became invalid. The code should be similar to the following:
<input igxInput name="dateInput" [(ngModel)]="displayData" required />
<input igxInput name="timeInput" [(ngModel)]="displayTime" required/>
Please, review this approach and let us know if you need further assistance on this matter.
I tried your suggestion, but that didn't work, since displayData is passed down from the igxDatePickerTemplate, but we need to bind to the date property in the ts file. So I experimented a bit more and came to the above linked stackblitz demo. The only problem remaining is that the first time picking a date, the field remains marked as invalid. This leads me to believe either I am still doing something wrong, or this could be a bug. Can you have a look and help me?
I reviewed our code and it seems we don't evaluate the igxInput value when it gets changed outside the input's 'input' event. This is why if one types in the date picker, the invalid style would be changed to valid, but when we change the value via the calendar, the invalid style remains. We will log this issue in github and it will be assigned to a developer in a future sprint to review my investigation and confirm my findings or to offer a fix, or other resolution. In the meantime, if using ngModel does not work for you, I can suggest you manually dispatch the input event of the input native element as follows:
<igx-date-picker name="date" [(ngModel)]="date" (onClose)="submit()">
@ViewChild(IgxInputDirective) datePickerInput: IgxInputDirective;
Please, let us know if you need further assistance on the matter.
Thank you Galina, this workaround solves the issue for now. However I am still having trouble with the required state of the field. I have made a new demo to demonstrate the problem: https://datepickerdemo.stackblitz.io. As you can see as soon as you enter a name the save button becomes enabled, allowing you to bypass the date requirement. Is this another possible bug and would you know a workaround for it?
If I understood correctly, you need to include the validation of the date & time pickers' values in the form validation but without binding them to the model. If that's the case I would recommend simply extending the submit button's disabled condition as shown in this sample (I used the first stackblitz sample you sent us, as the latest one seems to have some configuration error and I can't see it).