Constraints change widget defaults?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Constraints change widget defaults?

Eric Williams-3

If I create a domain class like this, create/edit views are generated with input type=”text”:

 

class Author {

            @Property Long id

            @Property Long version

            @Property String name

            @Property Date birthDate

            @Property Set books = new HashSet()

 

            @Property relatesToMany = [books:Book]

 

    String toString() { "${this.class.name} :  $id" }

}

 

However, if the same class has constraints and I generate views, I get textareas instead:

 

class Author {

            @Property Long id

            @Property Long version

            @Property String name

            @Property Date birthDate

            @Property Set books = new HashSet()

 

            @Property relatesToMany = [books:Book]

 

            @Property constraints = {

                        name(unique:true,blank:false)

            }

 

    String toString() { "${this.class.name} :  $id" }

}

 

Does this mean that if I specify constraints the default widget changes? I’d rather not have to specify it… am I doing something wrong?

 

Thanks,

Eric

 

Reply | Threaded
Open this post in threaded view
|

RE: Constraints change widget defaults?

Eric Williams-3

Please correct me if I’m wrong, but it looks like the following block of code from DefaultGrailsTemplateGenerator.groovy ensures that I can’t get input type=’text’ if I have constraints in my domain class when I generate my view, no matter what:

 

    private renderStringEditor(domainClass, property) {
        def cp = domainClass.constrainedProperties[property.name]
        if(!cp) {
            return "<input type='text' name='${property.name}' value='\${${domainClass.propertyName}?.${property.name}}' />"
        }
        else {
            if(cp.maxLength > 250 && !cp.password && !cp.inList) {
                return "<textarea rows='1' cols='1' name='${property.name}'>\${${domainClass.propertyName}?.${property.name}}</textarea>"
            }
            else {
                if(cp.inList) {
                   def sb = new StringBuffer('<select ')
                   sb << "name='${property.name}'>">
                   cp.inList.each {
                        sb << "<option value='${it}'>${it}</option>"
                   }
                   sb << '</select>'
                   return sb.toString()
                }
                else {
                    def sb = new StringBuffer('<input ')
                    cp.password ? sb << 'type="password" ' : sb << 'type="text" '
                    if(!cp.editable) sb << 'readonly="readonly" '
                    if(cp.maxLength < Integer.MAX_VALUE ) sb << "maxlength='${cp.maxLength}' "
                    sb << "name='${property.name}' value='\${${domainClass.propertyName}?.${property.name}}'></input>"
                    return sb.toString()
                }
            }
        }
    }

 

Should this be changed so that if (cp.maxLength <= 250 && !cp.password && !cp.inList) input type=’text’ is used instead of textarea?

 

Having to run grails generate-all before I can add constraints so I don’t get textareas seems like a pain.

 

I’m giving a demo on Grails to our development staff later this week and I know this is something they’ll ask about.

 

Thanks,

Eric

 


From: Eric Williams [mailto:[hidden email]]
Sent: Wednesday, April 19, 2006 11:06 AM
To: [hidden email]
Subject: [grails-user] Constraints change widget defaults?

 

If I create a domain class like this, create/edit views are generated with input type=”text”:

 

class Author {

            @Property Long id

            @Property Long version

            @Property String name

            @Property Date birthDate

            @Property Set books = new HashSet()

 

            @Property relatesToMany = [books:Book]

 

    String toString() { "${this.class.name} :  $id" }

}

 

However, if the same class has constraints and I generate views, I get textareas instead:

 

class Author {

            @Property Long id

            @Property Long version

            @Property String name

            @Property Date birthDate

            @Property Set books = new HashSet()

 

            @Property relatesToMany = [books:Book]

 

            @Property constraints = {

                        name(unique:true,blank:false)

            }

 

    String toString() { "${this.class.name} :  $id" }

}

 

Does this mean that if I specify constraints the default widget changes? I’d rather not have to specify it… am I doing something wrong?

 

Thanks,

Eric

 

Reply | Threaded
Open this post in threaded view
|

Re: Constraints change widget defaults?

graemer
Hi Eric,

Yeh this is a bug, but your change is unlikely to have an effect,
because if you don't set a maxLength then its assumed to be
Integer.MAXIMUM

Possibly something like

if (cp.maxLength > 250 && !cp.maxLength == Integer.MAXIMUM  &&
!cp.password && !cp.inList)

would do the trick.. I encourage you to file an issue and it will get
done asap, even quicker iif you submit a patch! ;-)

Cheers
Graeme

On 4/19/06, Eric Williams <[hidden email]> wrote:

>
>
>
> Please correct me if I'm wrong, but it looks like the following block of
> code from DefaultGrailsTemplateGenerator.groovy ensures
> that I can't get input type='text' if I have constraints in my domain class
> when I generate my view, no matter what:
>
>       private renderStringEditor(domainClass, property) {
>         def cp =
> domainClass.constrainedProperties[property.name]
>         if(!cp) {
>             return "<input type='text' name='${property.name}'
> value='\${${domainClass.propertyName}?.${property.name}}' />"
>         }
>         else {
>             if(cp.maxLength > 250 && !cp.password && !cp.inList) {
>                 return "<textarea rows='1' cols='1'
> name='${property.name}'>\${${domainClass.propertyName}?.${property.name}}</textarea>"
>             }
>             else {
>                 if(cp.inList) {
>                    def sb = new StringBuffer('<select ')
>                    sb << "name='${property.name}'>">
>                    cp.inList.each {
>                         sb << "<option value='${it}'>${it}</option>"
>                    }
>                    sb << '</select>'
>                    return sb.toString()
>                 }
>                 else {
>                     def sb = new StringBuffer('<input ')
>                     cp.password ? sb << 'type="password" ' : sb <<
> 'type="text" '
>                     if(!cp.editable) sb << 'readonly="readonly" '
>                     if(cp.maxLength < Integer.MAX_VALUE ) sb <<
> "maxlength='${cp.maxLength}' "
>                     sb << "name='${property.name}'
> value='\${${domainClass.propertyName}?.${property.name}}'></input>"
>                     return sb.toString()
>                 }
>             }
>         }
>     }
>
>
>
>
> Should this be changed so that if (cp.maxLength <= 250 && !cp.password &&
> !cp.inList) input type='text' is used instead of textarea?
>
>
>
> Having to run grails generate-all before I can add constraints so I don't
> get textareas seems like a pain.
>
>
>
> I'm giving a demo on Grails to our development staff later this week and I
> know this is something they'll ask about.
>
>
>
> Thanks,
>
> Eric
>
>
>
>  ________________________________
>
>
> From: Eric Williams [mailto:[hidden email]]
>  Sent: Wednesday, April 19, 2006 11:06 AM
>  To: [hidden email]
>  Subject: [grails-user] Constraints change widget defaults?
>
>
>
>
> If I create a domain class like this, create/edit views are generated with
> input type="text":
>
>
>
> class Author {
>
>             @Property Long id
>
>             @Property Long version
>
>             @Property String name
>
>             @Property Date birthDate
>
>             @Property Set books = new HashSet()
>
>
>
>             @Property relatesToMany = [books:Book]
>
>
>
>     String toString() { "${this.class.name} :  $id" }
>
> }
>
>
>
> However, if the same class has constraints and I generate views, I get
> textareas instead:
>
>
>
> class Author {
>
>             @Property Long id
>
>             @Property Long version
>
>             @Property String name
>
>             @Property Date birthDate
>
>             @Property Set books = new HashSet()
>
>
>
>             @Property relatesToMany = [books:Book]
>
>
>
>             @Property constraints = {
>
>                         name(unique:true,blank:false)
>
>             }
>
>
>
>     String toString() { "${this.class.name} :  $id" }
>
> }
>
>
>
> Does this mean that if I specify constraints the default widget changes? I'd
> rather not have to specify it… am I doing something wrong?
>
>
>
> Thanks,
>
> Eric
>
>
>