dousaoxiancy199896 2014-11-23 19: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 19: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'),
        );
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?