Sona Rijesh
Sona Rijesh

Reputation: 1035

How can i validate url in Yii

I have a form in Yii.

It have model Validation.

In that form I have to insert url in textfield.

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

In database I kept fields as varchar.

Rules in model are

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

How can I keep validation for this in Model?

Upvotes: 0

Views: 3113

Answers (3)

RAJESH CHAUHAN
RAJESH CHAUHAN

Reputation: 89

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

Upvotes: 2

Nasim Saleh
Nasim Saleh

Reputation: 181

array('inputURL', 'url')  

and if you want to add http in front if missing use

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

Upvotes: 1

ScaisEdge
ScaisEdge

Reputation: 133390

You should use url validator

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

}

Upvotes: 0

Related Questions