I have the next constraint in my domain object usuario/Usuario.groovy:
phone blank: false, matches: "[0-9 -+()]{3,15}"
And I have in i18n/messages_es.properties:
usuario.telefono.matches=Se admiten nĂºmeros, espacios y los caracteres: -+(). Se admiten entre 3 y 15 caracteres en total.
This is the error message if the phone doesn't match the pattern:
The translation of the message would be something like: "Please, adjust the requested format"
I have not any similar error message in any of the i18n/messages... files. It looks a client side error message as it appears instantly.
In create gsp there is the next code (I think the error message could come from here):
<g:hasErrors bean="${usuarioInstance}">
<bootstrap:alert class="alert-error">
<ul>
<g:eachError bean="${usuarioInstance}" var="error">
<li <g:if test="${error in org.springframework.validation.FieldError}">data-field-id="${error.field}"</g:if>><g:message error="${error}"/></li>
</g:eachError>
</ul>
</bootstrap:alert>
</g:hasErrors>
How could I change this error message by a custom one?
The first thing you have to understand: this message is html form validation error and doesn't connected to your application messages. So the fix will be in html. Try to add oninvalid attribut to input tag:
<input type="text" name="phone" pattern="[0-9 -+()]{3,15}" required="" value="" id="phone" oninvalid="setCustomValidity('Please, type valid phone number')">
You can move 'Please, type valid phone number' to i18n file
Edit: if you use fields plugin add oninvalid attr with input- prefix.
<f:field bean="${usuarioInstance}" property="phone" input-oninvalid="setCustomValidity('Please, type valid phone number')"/>
I think you are missing 'invalid' as part of the message key.
Use the following error code for matches:
className.propertyName.matches.invalid = 'errorMessage'..
Still if it doesn't work ..try the following approach.
Add def messageSource in controller /service.
user.errors?.allErrors?.each{
println messageSource.getMessage(it, null)
};
Go through the following link which explains this better
http://johnrellis.blogspot.com/2010/02/retrieve-grails-domain-errors-from.html
Related
I have an input for a login form that is required:
<input
autoComplete="email"
defaultValue={email}
disabled={state === 'submitting'}
id="email"
name="email"
placeholder={t('user-authentication:email-placeholder')}
required // HERE!
type="email"
/>
How can I test that the input is required with Playwright? My native browser warning in Chromium is: "Please fill in this field."
Is there a way to check / assert for this warning in Playwright? Or another way to test that the input is required?
I tried writing nothing in the input with .fill('') and then pressing enter with page.keyboard.press('Enter'), but those did not cause the warning to show up.
If the message is displayed by the browser and not something added to the DOM, you would struggle to test it directly in Playwright.
You could check the the required attribute is present, and trust that the browser is going to display the warning.
await page.locator('input#email[required]')
There's also a Constraint validation you could apply
If the element is required and the element's value is the empty string, then the element is suffering from valueMissing and the element will match the :invalid pseudo class.
await page.locator('input#email[required]:invalid')
I have a problem everywhere I use the mat date picker in the project:
ERROR TypeError: Cannot read properties of undefined (reading 'dateInput')
at MatDatepickerInput._onInput (datepicker.js:3145)
at MatDatepickerInput_input_HostBindingHandler (datepicker.js:3388)
at executeListenerWithErrorHandling (core.js:15308)
at wrapListenerIn_markDirtyAndPreventDefault (core.js:15352)
at HTMLInputElement.<anonymous> (platform-browser.js:561)
at ZoneDelegate.invokeTask (zone.js:406)
at Object.onInvokeTask (core.js:28659)
at ZoneDelegate.invokeTask (zone.js:405)
at Zone.runTask (zone.js:178)
at ZoneTask.invokeTask [as invoke] (zone.js:487)
If I select the date using date picker it works fine, but when I m trying to edit it manually it generates that error and it happens everywhere in the project. For example, I have:
<div class="datepicker-wrapper mr-20">
<input [matDatepicker]="fromPicker" formControlName="from" placeholder="dd/mm/yy" />
<mat-datepicker-toggle [for]="fromPicker" matSuffix>
<mat-icon matDatepickerToggleIcon svgIcon="calendar"></mat-icon>
</mat-datepicker-toggle>
<mat-datepicker #fromPicker></mat-datepicker>
</div>
I tried to update the material and moment-adapter versions from 12.2.5 to 12.2.12, but I receive the same error. Also I created a new project and it works fine there.
Does anyone encountered this until now? Thank you in advance!
matInput attribute is missing in the input element.
<input matInput [matDatepicker]="fromPicker" formControlName="from" placeholder="dd/mm/yy" />
Try this
I had the same error. I resolved it by checking the provider MAT_DATE_FORMATS.
The reason of the js error is related to the wrong dateInput format configuration of MAT_DATE_FORMATS provider. Please, check your module where you provide the MAT_DATE_FORMATS and take into account on the property dateInput:
{
provide: MAT_DATE_FORMATS,
useValue: {
display: {
dateInput: '<check-the-format-here>',
}
}
}
I confirm that the errors on on the object before I send them to the view
trip.riders.each{rider->
rider.errors.each{it->
//visually confirming to myself the errors are there
println it;
}
}
render(view:'edit',model:[trip:trip);
return;
The error I am confirming prints to console "Field error in object on field 'mileage'...."
Yet when rendered
<g:each in="${trip?.riders}" var="rdr" status='i'>
<div class='form-category'>
<div class='error'>
<g:hasErrors bean="${rdr}" field="mileage">
<g:renderErrors bean="${rdr}" field="mileage" as="list" />
</g:hasErrors>
No errors show up. What could be happening here?
UPDATE
I believe the problem is along these lines...
http://forum.spring.io/forum/other-spring-related/grails/97568-haserrors-true-disappears
Seems hibernate persists things in some instances in a way that causes validation errors to be cleared.
Try
<g:hasErrors>
<g:eachError> ${it} </g:eachError>
</g:hasErrors>
If you have added custom errors using "rejectValue()" method, validate() or save() method will remove custom errors added to the domain instance
The code in controller is as follows:
for(o in options){
if(o){
if(!o.isInteger()){
don.errors.reject("The value was not integer")
render(view: "editdonation", model: [id:id, donation:don])
return
}
don.addToDenominations(o.toInteger())
}
}
I intentionally pass in invalid input so that the don.errors.reject() code is executed.
The editdonation.gsp has the following code to print the error in the donation object.
<ul class="inline-errors" role="alert">
<g:hasErrors bean="${donation}">
<div class="errors">
ok it has errors
</div>
</g:hasErrors>
</ul>
Why is the view not displaying the error? I appreciate any help!
The Errors.reject() method expects an error code rather than an error description.
Creating the error code
To create your own error code:
Edit the file grails-app/i18n/message.properties
Add a new line with your error code. Ex: option.donation.notinteger=The option value {0} was not an Integer
Save the file grails-app/i18n/message.properties
Using the error code
To use the error code, call Errors.reject() like this:
don.errors.reject('option.donation.notinteger', [o] as Object[], 'The value was not integer')
The code above passes the option to reject so that it can insert the value into the error message. And as required, it provides a fallback error message.
The question title is enough to explain excepting the attempts that i try :
Attempt1 :
<g:link controller="staff" action="all" <g:if test="${actionName=='all' }">class="active"</g:if> >Overview</g:link>
Error Message
Error 500
Class :org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException
Message Grails tag [g:link] was not closed
Attempt2 :
<g:link controller="staff" action="all" class="<g:if test="${actionName=='all' }">active</g:if>" >Overview</g:link>
Error Message
Error 500
Class org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException
Message Attribute value must be quoted (controller="staff" action="all" class="
How to do a branching to decide if an element has css class X or not ?
X(in this example)= active
I think It will be sufficient if you will use ternary operator e.g:
<g:link controller="staff" action="all" class="${actionName == 'all' ? 'active' : ''}">
Overview
</g:link>