# fieldValue

## Purpose

This tag will inspect a bean which has been the subject of data binding and obtain the value of the field either from the originally submitted value contained within the bean's errors object populating during data binding or from the value of a bean's property. Once the value is obtained it will be automatically HTML encoded.

## Examples

    <g:fieldValue bean="${book}" field="title" />// or as a method
    <input type="text" value="${fieldValue(bean:book,field:'title')}" />

## Description

Attributes

- `bean` (required) - The bean instance to inspect
- `field` (required) - The name of the field to obtain the value of

## Source

Show Source

    def fieldValue =  { attrs, body ->
            def bean = attrs.bean
            def field = attrs.field?.toString()
            if(bean && field) {
                if(bean.metaClass.hasProperty( bean,'errors')) {
                    Errors errors = bean.errors
                    def rejectedValue = errors?.getFieldError(field)?.rejectedValue
                    if(rejectedValue == null ) {
                        rejectedValue = bean
                        field.split("\\.").each{ String fieldPart ->
                            rejectedValue = rejectedValue?."$fieldPart"
                        }
                    }
                    if(rejectedValue != null) {
                        out << formatValue(rejectedValue)
                    }
                }
                else {
                    def rejectedValue = bean
                    field.split("\\.").each{ String fieldPart ->
                        rejectedValue = rejectedValue?."$fieldPart"
                    }
                    if(rejectedValue != null) {
                        out << formatValue(rejectedValue)
                    }
                }
            }
        }    def extractErrors(attrs) {
            def model = attrs['model']
            def checkList = []
            if (attrs?.containsKey('bean')) {
                if(attrs.bean)
                    checkList << attrs.bean
            } else if (attrs.containsKey('model')) {
                if(model)
                    checkList = model.findAll {it.value?.errors instanceof Errors}.collect {it.value}
            } else {
                request.attributeNames.each {
                    def ra = request[it]
                    if(ra) {
                        def mc = GroovySystem.metaClassRegistry.getMetaClass(ra.getClass())
                        if (ra instanceof Errors && !checkList.contains(ra))
                            checkList << ra
                        else if (mc.hasProperty(ra, 'errors') && ra.errors instanceof Errors && !checkList.contains(ra.errors)) {
                            checkList << ra.errors
                        }
                    }
                }
            }        def resultErrorsList = []        for (i in checkList) {
                def errors = null
                if (i instanceof Errors) {
                    errors = i
                } else {
                    def mc = GroovySystem.metaClassRegistry.getMetaClass(i.getClass())
                    if (mc.hasProperty(i, 'errors')) {
                        errors = i.errors
                    }
                }
                if (errors?.hasErrors()) {
                    // if the 'field' attribute is not provided then we should output a body,
                    // otherwise we should check for field-specific errors
                    if (!attrs['field'] || errors.hasFieldErrors(attrs['field'])) {
                        resultErrorsList << errors
                    }
                }
            }        return resultErrorsList
        }