Yii2 - 在开始时为 URL 类型创建表单验证器以跳过 "http://"
Yii2 - Make Form Validator For URL Type To Skip "http://" in the start
这是我的 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://',我希望它批准url。
例子:-
whosebug.com
应该没问题。
http://whosebug.com
应该没问题。
当前状态:-
whosebug.com
未被接受。
http://whosebug.com
被接受..
测试参数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'],
];
}
}
您只需要将 'defaultScheme' => 'http' 添加到您的验证规则中,因此
public function rules()
{
return [
[['url'], 'required'],
['url', 'url', 'defaultScheme' => 'http'],
];
}
这是我的 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://',我希望它批准url。
例子:-
whosebug.com
应该没问题。
http://whosebug.com
应该没问题。
当前状态:-
whosebug.com
未被接受。
http://whosebug.com
被接受..
测试参数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'],
];
}
}
您只需要将 'defaultScheme' => 'http' 添加到您的验证规则中,因此
public function rules()
{
return [
[['url'], 'required'],
['url', 'url', 'defaultScheme' => 'http'],
];
}