grails唯一域约束

ubof19bj  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(170)

我是grails的新手,我遇到过这个问题。在我的表格里有名字和代码。代码必须是唯一的,所以如果用户试图注册相同的代码,已经存在它会显示用户和错误消息,但我不知道如何实现这一点。以下是表格:

<g:form controller="customer" action="saveCustomer" class="customer-form">

                        <form>
                            <div class = "row">
                                <div class = "col-md-5 mx-auto" style = "margin:0px;padding:0px;">

                                    <h5 style="font-weight: bold;margin-left:15px;";>Please enter customer information:</h5>
                                </div>
                            </div>

                            <div class = "row">
                                <div class = "col-md-5 mx-auto">

                            <div class="form-group">
                                <label for="name">Name</label>
                                <input  type="text" required  class="form-control name" id="name" placeholder="Enter first and last name" name="name">
                            </div>
                                </div>
                            </div>

                            <div class = "row">
                                <div class="col-md-5 mx-auto">
                            <div class="form-group">
                                <label for="code">Code</label>
                                <input type="text" required  class="form-control" id="code" placeholder="Enter code" name="code">
                            </div>
                            </div>
                            </div>
</form>
</g:form>

下面是我的域类:

class Customer {
    String code
    String name

    static constraints = {
        code unique: true
        name nullable: false

    }
}

你知道我该怎么做吗?非常感谢。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题