How to validate Angular Material Datepicker? - angular-material

I have the following code:
<form [formGroup]="meetingFormGroup">
<!-- Date Input -->
<mat-form-field>
<input
matInput
[min]="minDate"
[max]="maxDate"
[matDatepicker]="picker"
placeholder="Choose a date"
formControlName="date"
required
>
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker></mat-datepicker>
<mat-error *ngIf="meetingFormGroup.controls['date'].hasError('required')"
>Please choose a date.</mat-error
>
<mat-error *ngIf="?????"
>Entered date is too small.</mat-error
>
</mat-form-field>
</form>
Now on the Angular Material Website at the 'Date Validation' section I read the following:
Each validation property has a different error that can be checked:
A value that violates the min property will have a matDatepickerMin error.
A value that violates the max property will have a matDatepickerMax error.
A value that violates the matDatepickerFilter property will have a matDatepickerFilter error.
So my question is, how can I validate these errors in my mat-error tags. What do I need to write in the *ngIf expression field?
I am sorry for my bad English and hope you understand my problem and maybe can provide a solution.
Thanks!

From the Angular Material website (here):
A value that violates the min property will have a matDatepickerMin error.
A value that violates the max property will have a matDatepickerMax error.
A value that violates the matDatepickerFilter property will have a matDatepickerFilter error.
So, you'll use:
For min - meetingFormGroup.controls['date'].hasError('matDatepickerMin')
For max - meetingFormGroup.controls['date'].hasError('matDatepickerMax')
For filter - meetingFormGroup.controls['date'].hasError('matDatepickerFilter').

Related

Interpolation not working properly inside mat-form-field for mat-error

I am validating username with custom validation in angular material
Following is my code
<mat-form-field>
<input id="Username" required name="Username" [(ngModel)]="employee.Username" type="text" matInput
#Username="ngModel" (focusout)="ValidateUsername($event.target.value)" placeholder="Username">
<mat-error *ngIf="isNotValidUsername">Username already exist</mat-error>
<mat-error *ngIf="Username.errors?.required && Username.touched">Required</mat-error>
</mat-form-field>
Problem is "Username already exist" error not showing.
If i write this error line code outside mat-form-field then it worked. And also if Required error fired then it also show "Username already exist" error. But it alone inside mat-form-field tag not working. What i am doing wrong? How can i show it?
mat-error won't show errors if the control is valid. You can use the error state matcher to set an invalid state to the input. The following post includes how it is implemented in a password validation form group.
https://stackblitz.com/angular/brleoyyorkap?file=app%2Finput-error-state-matcher-example.ts

ngModel is not working with value attribute in ngx-material-timepicker with input in Angular7?

I have used ngx-material-timepicker for implementing the time filter.in our case time picker is open properly but its not working in the following scenarios:
Case 1 :
1.initially i have set selected time in the input and it working properly now this time i am not using with [(ngModel)]
<input [ngxTimepicker]="defaultValue" [value]="'05:11 pm'" (change)="selectChanged($event)">
<ngx-material-timepicker #defaultValue></ngx-material-timepicker><br>
Case 2 :
2.Now we have add [(ngModel)] for get the selected value that is selected fro the picker but now this time out initially set value is not showing in the input but if i remove the [(ngModel)] its working:
<input [ngxTimepicker]="defaultValue" [(ngModel)]="date1" [value]="'05:11 pm'" (ngModelChange)="selectChanged($event)" >
<ngx-material-timepicker #defaultValue></ngx-material-timepicker><br>
I have also post above issue in the github but get any response,please tell me anyone how to fix above issue.
I've got the same problem but in my case my inputs are into form and i forget about the name propriety.
i just added the name="date" in the input tag
<mat-form-field class="toggle-example" style="width: 100%">
<input matInput [(ngModel)]="time" [ngxTimepicker]="toggleTimepicker" name="time" [format]="24" [disableClick]="true" readonly>
<ngx-material-timepicker-toggle matSuffix [for]="toggleTimepicker"></ngx-material-timepicker-toggle>
<ngx-material-timepicker #toggleTimepicker></ngx-material-timepicker>
</mat-form-field>
I have fixed issue via below code:
<input [ngxTimepicker]="defaultValue" [(ngModel)]="date1 == undefined ? defaultValue : date1" [value]="'05:11 pm'" (ngModelChange)="selectChanged($event)" >
I think the onchange that you were asking is to set the time
You can use [timeset]="(setTime())" inside your ngx-time-picker tag
And in your ts file write the set time method.
I used this step and succeeded
<mat-form-field>
<input aria-label="default time" matInput [ngxTimepicker]="defaultValue" [value]="'9:00 am'" readonly>
<ngx-material-timepicker (timeSet)="setTime($event)" #defaultValue>
</ngx-material-timepicker>

Thymeleaf - Exception evaluating SpringEL expression on object variable

I am sticking a value into a hidden input with Thymeleaf and keep getting an error that says Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "receiptInquirySearchForm.cardNumber?:''" (template: "results.html" - line 14, col 44)
I have tried putting the ? after receiptInquirySearchForm, after cardNumber, and after both. I keep getting that same error on that line.
Here is line 14:
<input type="hidden" name="cardNumber" data-th-value="${receiptInquirySearchForm.cardNumber?}" />
Now I know receiptInquirySearchForm is a valid non-null object because I have several other hidden inputs that do not throw errors.
<input type="hidden" name="tokenId" data-th-value="${receiptInquirySearchForm.tokenId}" />
<input type="hidden" name="accountNumber" data-th-value="${receiptInquirySearchForm.accountNumber}" />
<input type="hidden" name="sku" data-th-value="${receiptInquirySearchForm.sku}" />
When I change the data-th-value from cardNumber to tokenId, it gets past that block of hidden inputs so every other line works fine.
UPDATE
I found another more descriptive error message down below.
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'cardNumber' cannot be found on object of type '...web.form.ReceiptInquirySearchForm' - maybe not public or not valid?
How can I check for that in the code? I know sometimes it will be there, but apparently in this instance it is not.
They were doing this in Velocity like this:
<input type="hidden" name="cardNumber" value="$!receiptInquirySearchForm.cardNumber" />
The exclamation correctly handled the possible missing or null cardNumber.
Just as #Metroids pointed out, you are probably missing getters/setters for the field cardNumber especially a getter for it. If you have a getter for it, check that the getter follows the POJO convention and is public like so;
public int getCardNumber()
{
return cardNumber;
}
If the spelling is not like so getCardNumber(), even though you can call the method in the controller to get the value, thymeleaf cannot do so because it relies on POJO convention to be able to call variable properties. I hope this helps.

Syntax error with parseJSON during unobtrusive validation

My MVC app is generating the following HTML which causes a Javascript syntax error upon submission (I'm not typing anything into the two text boxes). Here's the generated HTML and the submit handler:
<form action="/UrIntake/Save" id="UrIntakeForm" method="post">
<input data-val="true" data-val-length="The field LastName must be a string with a maximum length of 50." data-val-length-max="50" data-val-required="The LastName field is required." id="FormSubmitter_LastName" name="FormSubmitter.LastName" type="text" value="" />
<input data-val="true" data-val-length="The field FirstName must be a string with a maximum length of 50." data-val-length-max="50" data-val-required="The FirstName field is required." id="FormSubmitter_FirstName" name="FormSubmitter.FirstName" type="text" value="" />
<div id="SubmissionButtons" class="right">
<input type="button" onclick="SubmitForm()" value="Submit" />
<input type="button" onclick="CancelForm()" value="Cancel" />
</div>
</form>
function SubmitForm() {
$("#UrIntakeForm").valid();
.
.
.
This is the jQuery code where the syntax error is occurring (v1.9.0). "data" is undefined and the "return" line is where the error occurs:
parseJSON: function( data ) {
// Attempt to parse using the native JSON parser first
if ( window.JSON && window.JSON.parse ) {
return window.JSON.parse( data );
}
Presumably, I don't have to enter anything into the text boxes (and should then get the "field is required" message). Is this what's causing the error? That doesn't make sense, but I don't see what else it could be.
Cause
This is an issue with jquery.validate.unobtrusive.js in your ASP.NET.MVC package.
As of jQuery 1.9, the behavior of parseJSON() has changed and an undefined value would be considered a malformed JSON, resulting in the error you've specified. See the jQuery 1.9 Core Upgrade Guide for more information.
Solution
Use the jQuery Migrate plugin, which among other things adds backward-compatibility to the jQuery parseJSON() utility.
EDIT
According to the official announcement in this thread on Microsoft Connect, the issue has been resolved in the latest release of the framework.
Naturally, as Andreas Larsen noted in the comments, make sure to clear any relevant cache, server-side and client-side, after upgrading to the new release.
I also had this issue. The problem was that $.parseJSON(undefined) causes an exception to be thrown, and that the unobtrusive validation was making that call. As stated in the accepted answer, this has since been fixed.
You can download the Microsoft version of this script which will properly validate without causing an exception from this link: http://ajax.aspnetcdn.com/ajax/mvc/5.1/jquery.validate.unobtrusive.min.js

Grails check checkbox with value same as variable

Hi I have this value that I passed to the gsp
${d.causalOrganism}
I have this three checkboxes and I want one of them to be checked based on the above value. How can I do so? I tried but doesn't work.
<label for="causalOrganism">Causal Organism:</label>
Fungi<input type="checkbox" value="Fungi" name="causalOrganism" id="causalOrganism1" onclick="checkOrganism(this.id)"/>
Bacteria<input type="checkbox" value="Bacteria" name="causalOrganism" id="causalOrganism2" onclick="checkOrganism(this.id)"/>
Virus<input type="checkbox" value="Virus" name="causalOrganism" id="causalOrganism3" onclick="checkOrganism(this.id)" />
Add the checked attribute to your checkbox based on your desired condition:
...
Fungi<input type="checkbox" value="Fungi" name="causalOrganism" id="causalOrganism1" onclick="checkOrganism(this.id)" ${(d.causalOrganism == 'causalOrganism1') ? "checked='checked'" : ''}/>
...
You should think about using the grails checkBox. With this you could use the checked attribute directly.

Resources