在Grails中更改控制组的Twitter Bootstrap验证状态 [英] Changing twitter bootstrap validation states of a control group in grails

查看:67
本文介绍了在Grails中更改控制组的Twitter Bootstrap验证状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个带有表单的gsp,并且我在验证表单字段时尝试使用twitter验证状态.我正在使用grails命令对象进行验证.问题是,如果命令对象中的字段使用"$ {hasErrors(bean:validationCommand,field:'start','error')}"错误,则grails提供了一种将"error"类添加到控制组的方法. .如果字段开始"中包含验证错误,则会添加错误标签.在某种程度上,这可以正常工作.因此,开始字段在验证错误时变为红色.但我也希望它也能在验证成功时变绿!这没有发生.

so I have a gsp with a form, and I am trying to use twitter validation states when validating the form fields. I am using a grails command object for validation. The problem is that grails provides a way to add the "error" class onto a control group if a field from the command object has errors using " ${hasErrors(bean:validationCommand, field:'start', 'error')}" . This adds the error tag if the field "start" has validation errors. This works fine, up to a point. So the start field turns red on validation error. But I want it to turn green on a validation success also! which is not happening.

因此,这是开始表单字段:

So, here is the start form field :

 <div 
class="control-group ${hasErrors(bean:validationCommand, field:'start', 'error')}">
                    <label class="control-label" for="start">Starting Tag</label>
                    <div class="controls">
                        <g:textField name="start"
                            value="${fieldValue(bean:validationCommand,field:'start')}"
                            class="input-medium " />
                        <g:hasErrors bean="${validationCommand}" field="start">
                            <ul>
                                <span class="help-inline"> <g:renderErrors
                                        bean="${validationCommand}" field="start" as="list" />
                                </span>
                            </ul>
                        </g:hasErrors>
                    </div>
                </div>

我可以想到的一种方法是通过测试以查看字段是否有错误,然后将变量(例如startState)设置为成功还是错误,然后在div中使用$ {startState}类.

One way I could think of setting this up is by using a test to see if the field has errors and setting a variable (say startState) to success or error and then using ${startState} class in the div.

<g:if test="${ hasErrors(bean:validationCommand, field:'start') }">
                    <g:set var="startState" value="error"/>
                </g:if>
                <g:else>
                    <g:set var="startState" value="success"/>
                </g:else>
                </g:if>
                <div
                    class="control-group ${startState}">
                    <--same gsp as above-->

这是因为该字段从一开始就为绿色(即使在文本字段中未输入任何内容),然后在出现错误时变为红色,或保持绿色.

What this does is that it the field is green from the beginning (even when nothing is entered in the text field) and then turns red on error, or stays green.

我进行这项工作的最后一次尝试涉及另一个if测试,以查看是否使用

My last attempt to make this work involved another if test to see whether the field is set using

<g:if test="${fieldValue(bean:validationCommand,field:'start') != null }">

这不会影响第二次尝试的结果.

This doesnt affect the outcome from the second attempt.

问题是,如何根据输入字段的验证情况将成功类动态添加到div标记中.

So the question is , how can i dynamically add the success class to a div tag based on the validation of the input field.

控制器如下所示:

class ValidationController {

def index() {
}

def validate(ValidationCommand cmd) {
    if (cmd.hasErrors()) {
        render view: 'index', model: [validationCommand: cmd]
    } else {
        try {
            SourceLocation repo = SourceLocation.findById(cmd.repo)
            def parentJira = cmd.issueService.findIssue(cmd.jira)

            Collection<IssueViewModel> issues = cmd.issueService.listIssuesForParent(cmd.jira)
            Collection<SourceCommitViewModel> commits = cmd.sourceRepositoryService.listCommits(repo.project, repo.slug, cmd.start, cmd.end)

            def (valid, missing_linked_issues, missing_mentioned_issues, bad_commits, issues_with_filtered_commits) = cmd.validationService.validate(repo, issues, commits)
            [ParentJira: parentJira, Valid: valid, Commits: commits, Issues: issues, LinkedMissing: missing_linked_issues, MentionedMissing: missing_mentioned_issues, BadCommits: bad_commits, SourceCommitValidation: issues_with_filtered_commits]
        } catch (e) {
            log.error( "Validation Controller Error", e )
            flash.message = "validation.service.failed"
            flash.args = [e.message]
            flash.default = "Validation Service failed"
            redirect action: 'index', params: params
        }
    }
}
}

命令对象是:

class ValidationCommand {

def validationService
def sourceRepositoryService
def issueService

String jira
String repo
String start
String end

static constraints = {
    jira blank: false, nullable: false, matches: "[a-zA-Z]+-[0-9]+", validator: existsIssueAndIsARelease
    repo blank: false, nullable: false, validator: { val, obj -> SourceLocation.findById(val) != null }
    start blank: false, nullable: false, validator: existsTag
    end blank: false, nullable: false, validator: existsTag
}
}

推荐答案

您可以在此处查看: <g:set var="startState" value="error"/> </g:if> <g:else> <g:set var="startState" value="success"/> </g:else> <div class="control-group ${startState}">

You can look here: Grails hasErrors method with ternary operator?, but shortly, hasErrors(bean:validationCommand, field:'start') does not behave as you expect it to (meaning - it does not return a boolean). It's intended to be used in css-class expressions. So in your case I'd create a tag for that or there is also a quick workaround (which kinda looks ugly, so I don't really recommend it): <g:if test="${ hasErrors(bean:validationCommand, field:'start', 'true') } == 'true'"> <g:set var="startState" value="error"/> </g:if> <g:else> <g:set var="startState" value="success"/> </g:else> <div class="control-group ${startState}">

这篇关于在Grails中更改控制组的Twitter Bootstrap验证状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆