Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'll add some more input to the confusion and say this:</p> <p>You won't want a single method to validate your values. Today, just passing an array of valid values might be enough. But tomorrow, you'll have something like an e-mail address to validate, and then you'll need a method that validates against a RegEx. Maybe next week, you'll need to validate against a set of values that derives from the context the value was taken from, and so on...</p> <p>Using inheritance in this context, as one comment suggested, is not a good idea - you'll tightly couple your validations to the rest of the code, and sooner or later you'll find yourself changing a lot of things when only a simple validation call should have changed. Same goes for a utility class: You'll find yourself using that class reference lots of times, and if you ever choose to change your validation method, you'll have to accommodate for lots of changes in lots of places.</p> <p>So, in good OO fashion, you best use an interface, let's call it <code>Validator</code> and let all of your validating classes implement it:</p> <pre><code>public interface Validator { function validate ( value : * ) : Boolean; } </code></pre> <p>By the way, that's also the ultimate reason not to use a static class: There are no static interfaces in ActionScript.</p> <p>Now for some classes. Let's start with your own validation method, based on an array of values:</p> <pre><code>public class ArrayValidatorImpl implements Validator { private _validValues : Array; public function validate ( value : * ) : Boolean { return value in _validValues; } public function ArrayValidatorImpl (validValues:Array ) { _validValues = validValues; } } </code></pre> <p>...and the e-mail one:</p> <pre><code>public class EmailValidatorImpl implements Validator { public function validate ( value : * ) : Boolean { var reg:RegExp = /(^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.(ac|ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|asia|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cat|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|info|int|io|iq|ir|is|it|je|jm|jo|jobs|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mobi|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tel|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|travel|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|xn|ye|yt|yu|za|zm|zw{2,4})$)/; return reg.exec( value.toString() ); } } </code></pre> <p>Any time you need validation now, you can simply pass an instance of the interface to the class that needs it, for example:</p> <pre><code>public class MyValidatingClass { private var _validator:Validator; public function myGreatMethod ( myValue : * ) : void { if( _validator.validate( myValue ) ) doStuffWith( myValue ); } // ... public function MyValidatingClass( validator:Validator ) { _validator = validator; } } </code></pre> <p>If your requirements change, you can simply pass a different implementation, with out ever having to touch the code for <code>MyValidatingClass</code> again. Clean, simple, loosely coupled - and ready to be reused in the next program you write. And the one after that. And so on...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload