dousaoxiancy199896 2014-11-23 11:37
浏览 46
已采纳

如何在Yii 2.0中验证数据库的输入字段?

Suppose I have a field name id and I want the user to enter some integer in the id field but at the same time I want to check that id against the database whether it exists or not. If it exists then it should show some error.

Is there a function to achieve this in Yii 2.0?

Any help would be greatly appreciated. Thank you!

  • 写回答

1条回答 默认 最新

  • dongmu3187 2014-11-23 11:40
    关注

    If this is from a model, you can have a unique rule to it in the validation rules.

    In your model, you have a rules() function:

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        return array(
            array('name', 'required'),
            array('some_id', 'unique'),
        );
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部