1

I have a basic model defined:

protected function defineAttributes()
{
    return array(
        'id'        => array(AttributeType::String, 'required' => true),
        'url'       => AttributeType::Url,
        'info'      => array(AttributeType::String, 'required' => true, 'maxLength' => 4000),
        'title'     => array(AttributeType::String, 'required' => true, 'maxLength' => 65, 'minLength' => 15),
        'message'   => AttributeType::String,
    );
}

And when checking if the model is valid:

if (!$my_model->validate())
{
    return false;
}

Almost any string for 'url' is passing the validation, even without a '.com'

So if someone inputs sdfsdfsfsdfsdf in the input field, it'll pass as a valid URL and return as http://sdfsdfsfsdfsdf

I've also tried using AttributeType::UrlFormat but I run into the same problem. Am I not understanding what this attributetype does, or should it be validating that it's an actual URL string?

1

Craft extends Yii's CUrlValidator to add support for protocol-less URLs as well as URLs without a TLD (such as your example of http://sdfsdfsfsdfsdf).

If you need other behaviors for your URL validation, you can always write a custom URL validator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.