Reputation: 1219
In grails 2 we were able to reference the domain object constraints in a gsp as to keep the html 5 configration dry. On grails 3 (tried both 3.1.10 and 3.2.0.RC1) I get an error for code I tested in grails 2 successfully. I am trying to reference the constraint matches in the attribute phone and use that for the HTML 5 pattern. The scaffolding use to generate this code but for Grails 3 the scaffolding generates use the fields plugin so I cannot see that code. Any ideas?
Here is the domain object code:
class Disruption {
static constraints = {
phone(matches:/^[0-9]{10}$/, nullable:true)
email(email:true, nullable:false)
}
String name
String phone
String email
Here is the gsp code:
<div class="form-group ${hasErrors(bean: disruption, field: 'phone', 'error')}">
<label for="phone" class="control-label col-sm-3">
Phone
</label>
<div class="col-sm-2">
<g:textField name="phone" style="width: 7em" class="form-control" title="Phone 10 digits" pattern="${disruption.constraints.phone.matches}" maxlength="10" placeholder="##########" value="${disruption.phone}"/>
</div>
</div>
Here is the exception:
URI /disruption/create Class java.lang.NullPointerException Message Request processing failed; nested exception is org.grails.gsp.GroovyPagesException: Error processing GroovyPageView: [views/disruption/create.gsp:92] Error executing tag : Error evaluating expression [disruption.constraints.phone.matches] on line [58]: Cannot get property 'phone' on null object Caused by Cannot get property 'phone' on null object
Upvotes: 2
Views: 600
Reputation: 1219
Domain Objects need to use the constrainedProperties and Command Object need to use the constraintsMap see examples below.
<g:textField name="phone" style="width: 7em" class="form-control" title="Phone 10 digits" pattern="${disruption.constrainedProperties.phone.matches}" maxlength="10" placeholder="##########" value="${disruption?.phone}"/>
OR for Command Objects
<g:textField name="phone" style="width: 7em" class="form-control" title="Phone 10 digits" pattern="${searchCommand.constraintsMap.phone.matches}" maxlength="10" placeholder="##########" value="${searchCommand?.phone}"/>
Upvotes: 4