I've had a few people tell me that I should have url verification set up before I post videos, podcasts (etc) - and I had another typo trip me up on the Modeling Panel Video yesterday. So this morning, I went ahead and added url verification to my posting tool (the iTunes tag editor, specifically). Here's what I did:
- Edited the input field for the url in the GUI builder, adding a validation method
- Implemented a method that answers true if the url responds, false otherwise
Now obviously, if the url is for a large video file, I don't want to download the entire thing to validate it - I just want the headers. So here's the method:
checkThatUrlExists: ctrlr
"check to see whether the URL exists before we move on"
| toCheck client resp |
toCheck := ctrlr view editValue.
('http*' match: toCheck)
ifFalse: [^false].
client := Net.HttpClient new.
resp := [client headers: toCheck]
on: Net.HttpException
do: [:ex | nil].
^resp notNil
If the validation method takes an argument, the controller comes in. I use that to get the edited value (as opposed to the last thing sitting in the model, which will not be what I've entered yet). Then I create the HttpClient, and send a request for just the headers.
If I get any http exception, I assume it's bad - for the purposes of url validation, I'm not that particular about why it failed. In terms of behavior, the tab out of that field fails, and no data for that field has been accepted yet. I should really change the accept button behavior to enable only on a validated field, but this is a UI only I use, so I'm not that worried about it at this point :)
Technorati Tags:
http, url validation