Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Validation Grails,用于更新的自定义可验证对象_Validation_Grails_Groovy - Fatal编程技术网

Validation Grails,用于更新的自定义可验证对象

Validation Grails,用于更新的自定义可验证对象,validation,grails,groovy,Validation,Grails,Groovy,我对我的用户进行了自定义验证。它工作正常,但用于创建,但当我想要更新用户时,我会遇到一个错误。有些字段在更新部分不可用,例如用户名、密码和电子邮件,这些字段不应为空 import grails.validation.Validateable class UserCommand implements Validateable { Long id String firstName String lastName String username String

我对我的用户进行了自定义验证。它工作正常,但用于创建,但当我想要更新用户时,我会遇到一个错误。有些字段在更新部分不可用,例如用户名、密码和电子邮件,这些字段不应为空

import grails.validation.Validateable

class UserCommand implements Validateable {
    Long id
    String firstName
    String lastName
    String username
    String password
    String email
    UserStatus status = UserStatus.CREATED


    static constraints = {
        firstName nullable: false, blank: false
        lastName nullable: false, blank: false
        username nullable: false, blank: false, validator: { val, obj ->
            if (obj.id) {
                if (User.countByUsernameAndIdNotEqual(val,obj.id)) {
                    return "user.already.exist"
                }
            } else {
                if (User.countByUsername(val)) {
                    return "user.already.exist"
                }
            }
        }
        password nullable: false, blank: false
        email nullable: false, blank: false, validator: { val, obj ->
            if (obj.id) {
                if (User.countByEmailAndIdNotEqual(val,obj.id)) {
                    return "user.already.exist"
                }
            } else {
                if (User.countByEmail(val)) {
                    return "user.already.exist"
                }
            }
        }
    }
}

    def update(UserCommand command) {
        if(command.validate()) {
            try {
                user = userService.update(command)
            } catch (ValidationException e) {
                respond user.errors, view: 'edit'
                return
            }
        }
    }
在我的控制器中,当我想验证输入时,它会给我一个
NullPointerException
,因为所提到的字段不应该为null

import grails.validation.Validateable

class UserCommand implements Validateable {
    Long id
    String firstName
    String lastName
    String username
    String password
    String email
    UserStatus status = UserStatus.CREATED


    static constraints = {
        firstName nullable: false, blank: false
        lastName nullable: false, blank: false
        username nullable: false, blank: false, validator: { val, obj ->
            if (obj.id) {
                if (User.countByUsernameAndIdNotEqual(val,obj.id)) {
                    return "user.already.exist"
                }
            } else {
                if (User.countByUsername(val)) {
                    return "user.already.exist"
                }
            }
        }
        password nullable: false, blank: false
        email nullable: false, blank: false, validator: { val, obj ->
            if (obj.id) {
                if (User.countByEmailAndIdNotEqual(val,obj.id)) {
                    return "user.already.exist"
                }
            } else {
                if (User.countByEmail(val)) {
                    return "user.already.exist"
                }
            }
        }
    }
}

    def update(UserCommand command) {
        if(command.validate()) {
            try {
                user = userService.update(command)
            } catch (ValidationException e) {
                respond user.errors, view: 'edit'
                return
            }
        }
    }
这是
edit.gsp


<!DOCTYPE html>
<html>
<head>
    <title>Edit information</title>
    <meta name="layout" content="main"/>
    <asset:stylesheet src="bootstrap.css" rel="stylesheet"/>
</head>
<body>
<g:form resource="${this.user}" controller="user" action="update" method="put">
    <div class="container">
        <div class="row">
            <div class="col-md-6">
                <div class="inputWithIcon">
                    <input type="text" class="custom-input" name="firstName" placeholder="First Name" value="${this.user.firstName}">
                    <i class="fa fa-id-badge fa-lg fa-fw" aria-hidden="true"></i>
                </div>
            </div>
            <div class="col-md-6">
                <div class="inputWithIcon">
                    <input type="text" class="custom-input" name="lastName" placeholder="Last Name" value="${this.user.lastName}">
                    <i class="fa fa-id-card fa-lg fa-fw" aria-hidden="true"></i>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-4">
                <div class="inputWithIcon">
                    <input type="text" class="custom-input" name="country" placeholder="Country" value="${this.user.country}">
                    <i class="fa fa-globe fa-lg fa-fw" aria-hidden="true"></i>
                </div>
            </div>
            <div class="col-md-8">
                <div class="inputWithIcon">
                    <textarea  type="text" class="custom-input" name="address" placeholder="Address" rows="3">${this.user.address}</textarea>
                    <i class="fa fa-address-card fa-lg fa-fw" aria-hidden="true"></i>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-12">
                <button type="submit" class="custom-button edit">Update</button>
            </div>
        </div>

    </div>
</g:form>
</div>
</body>
</html>


编辑信息
${this.user.address}
更新
处理验证的最佳方法是什么(也许我不完全理解这个概念)?如何解决这个问题


提前谢谢

我认为您可能需要在
update
方法中加载
UserCommand
,根据输入的参数更新字段

因此,与您的更新方法不同

def update() {
    UserCommand command = UserCommand.get(params.id)
    command.firstName = params.firstName // and more based on whatever you can update

    if(command.validate()) {
        try {
            user = userService.update(command)
        } catch (ValidationException e) {
            respond user.errors, view: 'edit'
            return
        }
    }
}

您也可以在
中的edit.gsp上有所有不可更新的字段,但我的偏好是不要跨,因为有恶意意图的人仍然可以通过使用DOM inspector或类似工具相当容易地更新这些字段。

我认为您可能需要做的是加载
update
方法中的
UserCommand
,根据输入的参数更新字段

因此,与您的更新方法不同

def update() {
    UserCommand command = UserCommand.get(params.id)
    command.firstName = params.firstName // and more based on whatever you can update

    if(command.validate()) {
        try {
            user = userService.update(command)
        } catch (ValidationException e) {
            respond user.errors, view: 'edit'
            return
        }
    }
}

您也可以在
中的edit.gsp上有所有不可更新的字段,但我的偏好是不要跨域发送这样的数据,因为有恶意意图的人仍然可以通过使用DOM inspector或类似工具相当容易地更新这些字段。

您可能也很难做到这一点

这些可以构建到用户GORM类中,从而避免构建单独的CommandObject


虽然我很确定你知道。我把这个放在这里是为了其他可能正在搜索这个主题的人。

你可能也在艰难地完成这项工作

这些可以构建到用户GORM类中,从而避免构建单独的CommandObject


虽然我很确定你知道。我只是把它放在这里,供其他可能正在搜索此主题的人使用。

您应该创建一个UpdateUser命令,甚至是CreateUser和UpdateUser可以扩展的基本命令,而不是试图将多个概念包含在一个中。您应该创建一个UpdateUser命令,甚至是CreateUser和UpdateUser可以扩展的基本命令从扩展而不是尝试将多个概念包含到一个概念中。