¿Cómo puedo rellenar previamente un campo de formulario oculto en griales?

I have just gotten started with Grails and I have a very basic application running. I want to pre-populate a hidden form field with a random string.

What is the best way to do this? I have looked at the taglib but I am not sure what the best practice is for this sort of thing. Should I create a class in the src/java or src/groovy folder or is there a better way to get this done?

Lastly, and I know this is a very basic question, but if I do create a class or taglib, how exactly is that called from within the .gsp page?

¡Gracias!

preguntado el 01 de julio de 12 a las 04:07

Well for creating a hidden field your dont need any taglib or special class.. just create the random string in your controller, pass it in model and user it as the value of hidden field in gsp -

Thanks, that does make sense. I am new to grails however and my create action does not have a model associated with it, it is simply: def create() { [orgInstance: new Org(params)] } How can I get the random string passed from within my create action to the create view? Thanks... -

1 Respuestas

If your action looks like this

def create() { [orgInstance: new Org(params)] }

it means that a new Org object is passed to your view which can be referenced as orgInstance

Since the model [orgInstance: new Org(params)] is a map, you can simply add another parameter:

def create() { [orgInstance: new Org(params), hiddenValue: 'something random'] }

This can be used in your .gsp in the following way:

<input type="hidden" name="test" value="${hiddenValue}" />

Regarding your other question: a custom taglib is used in the same way as the other Grails-Tags: <g:myTag ...>...</g:myTag> . You can change the namespacegto whatever you like -g` is the default. See the documentation for more details: http://grails.org/doc/latest/ref/Tag%20Libraries/Usage.html

Respondido 01 Jul 12, 16:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.