这就是我的yii2模型的样子: -
namespace app\models;
use Yii;
use yii\base\Model;
class UrlForm extends Model
{
public $url;
/**
* @return array the validation rules.
*/
public function rules()
{
return [
[['url'], 'required'],
['url', 'url'],
];
}
}
如果使用时尚未写入' http://'我希望它批准该网址在一开始。
示例: -
stackoverflow.com
应该可以正常使用。
http://stackoverflow.com
应该可以正常使用。
当前状态: -
stackoverflow.com
不被接受。
http://stackoverflow.com
..
答案 0 :(得分:1)
您只需添加'defaultScheme'=> 'http'到您的验证规则,所以
public function rules()
{
return [
[['url'], 'required'],
['url', 'url', 'defaultScheme' => 'http'],
];
}
答案 1 :(得分:0)
测试参数URL(很可能是一个字符串)是否在开头包含http://
; if (doesntcontainHTTP){addHttpToURL();}
示例:
class UrlForm extends Model
{
public $url;
/**
* @add "http://" to the url in here
*/
public function rules()
{
/**
* @Place the if statement here
*/
return [
[['url'], 'required'],
['url', 'url'],
];
}
}