我怎样才能在Yii中验证网址

时间:2015-12-25 07:56:09

标签: yii

我在Yii有一个表格。

它有模型验证。

在该表格中,我必须在文本字段中插入网址。

eg:https://www.google.co.in

在数据库中,我将字段保存为varchar。

模型中的规则是

public function rules() {
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('url', 'required'),
        array('name, url', 'length', 'max' => 255),

        // The following rule is used by search().
        // @todo Please remove those attributes that should not be searched.
        array('name, url', 'safe', 'on' => 'search'),
    );
}

如何在模型中对此进行验证?

3 个答案:

答案 0 :(得分:1)

array('inputURL', 'url')  

如果你想在缺少使用

的情况下在前面添加http
array('website', 'url',defaultScheme' => 'http')

答案 1 :(得分:0)

您应该使用网址验证器

这样array('siteurl', 'url'),

public function rules() {
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
    array('url', 'required'),
    array('name, url', 'length', 'max' => 255),
    array('url', 'url'),
    // The following rule is used by search().
    // @todo Please remove those attributes that should not be searched.
    array('name, url', 'safe', 'on' => 'search'),
);

}

答案 2 :(得分:0)

Try This:-

return array(
    array('yoururl', 'required'),
    array('yoururl', 'url'),//for url validation
    // The following rule is used by search().
    // @todo Please remove those attributes that should not be searched.
    array('name, yoururl', 'safe', 'on' => 'search'),
):