Pages

May 28, 2009

Path Validator

An English translation is available for this post...
הבטחתי, לא?
אז הנה איזה משהו קטנטן (באמת) שהוא חלק מאיזה פרוייקטון שאני עובד עליו. מדובר בואלידאטור שנועד לבדוק "תקינות" של path שמשתמש בכניס. השימושים די ברורים, למעשה כל אפליקציה שדורשת מהמשתמש להכניס Path כחלק מהפעולות שהוא רוצה לבצע.
הואלידאטור הנוכחי בודק אם בכלל יש קלט ואז, אם יש, הוא פשוט בודק אם הנתיב הזה קיים לוקאלית. זה הכל.
יש שם איזה try…catch קטן בכדי לא לפול על wrong arguments exception שיכול לעוף ברגע ש... יש ארגומנטים שלא יכולים להוות נתיב. זה הכל בתכלס. למטה אתם יכולים למצוא את הקוד של הולידאטור ואת השימוש בו. שימו לב שהאיתחול והשימוש בולידאטור נעשה ב-AS, למרות שהוא יכול להיעשות גם בדרך הקונבנציונאלית ב-MXML.
לכל שאלה, טענה וכיו"ב, אתם יודעים איפה למצוא אותי.
תבלו, וחג גבינות שמח!


I promised, didn’t I?
So here is something real tiny that is a part of some project I’m working on. It’s a Validator which checks a file path that the user have entered. The usage is pretty straight forward, any application that requires the user to enter a valid path as part of the operation he wants to perform. This Validator check if there’s an input at all, and there is, it checks that the file path does exists. That’s about it.
There is a little “try… catch” that ensures that the Validator won’t fall on wrong arguments exception that the File class might throw once the arguments for it are… well… wrong.
That’s it really, you can see the code for the Validator and the usage of it below.
Notice that the instatiation and usage of the validator is made in AS code, though it can be done in the more conventional way, in MXML.
For any question or comment, you know where to find me.
Take care.


PathValidator

package com.flashmattic.validator.pathvalidator {
import flash.filesystem.File;

import mx.validators.ValidationResult;
import mx.validators.Validator;

/**
* This validator validates that a path given is a valid path for an existing
* file or folder.
* @author MBz
*/
public class PathValidator extends Validator {
public function PathValidator() {
super();
}

/**
* Checks for illigal arguments for the File constructor and also if the
* path given is valid and points to an existing file or folder.
* This also checks of any input was inserted, if the user decided that
* this field is requiered.
* @param value
* @return Array
*/
override protected function doValidation(value:Object):Array {
// Convert value to a String.
var inputValue:String = value as String;

// Clear results Array.
var results:Array = new Array();

var validationResult:ValidationResult;

// Call base class doValidation().
results = super.doValidation(value);

// Return if there are errors.
if (results.length > 0) {
return results;
}

if (required && (inputValue == null || inputValue == "")) {
validationResult = new ValidationResult(true,
null,
"no string",
"You must enter a path");
results.push(validationResult);
return results;
}

try {
var testFile:File = new File(inputValue);
if (!testFile.exists) {
validationResult = new ValidationResult(true,
null,
"path not valid",
"Invalid Path");
results.push(validationResult);
return results;
}
} catch (e:ArgumentError) {
validationResult = new ValidationResult(true,
null,
"path not valid",
"Invalid Path");
results.push(validationResult);
}
return results;
}
}
}


Usage

filePathValidator = new PathValidator;
filePathValidator.addEventListener(ValidationResultEvent.VALID, onFilePathValid);
filePathValidator.addEventListener(ValidationResultEvent.INVALID, onFilePathInvalid);
filePathValidator.required = true;

filePathValidator.validate(value);

2 comments:

Unknown said...

Very nice, many developers are starting to use AIR to build desktop apps, such apps may also handle local files, this can be very useful in these cases.

Flashmattic said...

yeap, bro :)
this was the motivation behind it.
Cheers.