2015-12-25 68 views
-1

我在Yii中有一個表單。我如何驗證Yii中的url

它有模型驗證。

在這種形式中,我必須在文本字段中插入url。

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'), 
    ); 
} 

我怎樣才能保持驗證了這模型?

回答

1
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'), 
): 
0

您應該使用URL驗證

這樣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'), 
); 

}

0
array('inputURL', 'url') 

,如果你想在前面添加http如果缺少使用

array('website', 'url',defaultScheme' => 'http')