tags:

views:

30

answers:

3

I have a need to validate a field against our database to verify unique-ness. The problem I seem to be having is that the validators doValidation() exits before we've heard back from database.

How can I have the validator wait to return its payload until after we've heard from the DB?

Or perhaps a better question might be (since I think the first question is impossible), how can I set this up differently, so that I don't need to wait, or so that the wait doesn't cause the validation to automaticallly return valid?

A: 

If you're using a remote object, you can specify the method call inside your remote declaration and assign a function to the result call. The result call only runs once the remote server returns something, so it won't be run before your validation.

Do your validation call in said result function call (which you will have to create) and you should be good. Your code should go something like this:

<s:RemoteObject id="employeeService"
  destination="ColdFusion"
  source="f4iaw100.remoteData.employeeData"
  endpoint="http://adobetes.com/flex2gateway/"
  result="employeeService_resultHandler(event)"/>
    **<s:method name="dataCheckCall" result="dataCheckResult(event)"/>**
<s:RemoteObject />

And in your script:

function protected dataCheckResult(event:ResultEvent):void {
  **doValidate();**
}

Edit: As soon as you call "dataCheckCall" the method will start running. If, for whatever reason, you want to call this WITHIN your validator, you can do so, and then dataCheckResult will run whenever it returns with it's payload (pretend doValidate is called elsewhere). I've left a message below as well.

Organiccat
yeah...I was hoping to be able to fully encapsulate the entire idea *IN* the validator.....
reidLinden
Not sure how you want to handle that. Are you saying you want your validator to run separately from your database call? Technically, that's what you are doing in the first place, but there isn't a great way to handle the update unless you're binding the data to something. If you are, then you can update the results on the return call, yet let your validate function run without interference. I'd need further info to figure out how best to tackle the problem though, and what you're trying to do and why.
Organiccat
I just have a series of validators that are already running...I would have preferred to have made it so that this validator fully encapsulates the logic, instead of having some parts over here...and others over there...that would also keep all the validation similar as well, instead of these validate THIS way, and that one validates through a completely different mechanism.
reidLinden
A: 

You are trying to fit an asynchronous process (fetching data from a DB) into a synchronous process (checking all the validators in turn).

This won't work...

You'll need to either roll your own validator framework, or use a different method of determining the legality of your controls.

P.S. The MX validators are rubbish anyway!

Gregor Kiddie
are they ? They seem a great improvement over what I had access to in classic ASP :D. What is a better way? Can you point me at a better validation solution ?
reidLinden
I don't think there's a good 3rd party solution out there. Most people who've rolled their own keep them quite closed. As an example of why they aren't that nice, try validating an input by comparing it to another one... that's where it starts to fall down.
Gregor Kiddie
Hmm...I've actually had good success with a custom mx validator for that issue. Comparing 'email' to 'retyped email', to make sure they are the same. Not sure where I found it, but, its working out pretty well so far. As far as rolling my own goes, I don't see how that would actually help me in this case, as I'd still have to solve the problem at hand, which is, how to make validation against a remote asynchronous source work.
reidLinden
Yeah, now try having two date fields, one of which must be before the other, and the other must be after the first. Now do it in a nice way... It's not easy.
Gregor Kiddie
The point about rolling your own, is that you'll have to. The MX validators are synchronous, you need to make a validator framework that's asynchronous, you'll need to roll your own.
Gregor Kiddie
ahh...icky, that.
reidLinden
A: 

What I've managed to do, seems to work, mostly. I don't like it, but it at least performs the validation against the remote source.

What I've done, then, is to use an 'keyUp' event handler to spin off the database lookup portion. In the meanwhile, I set up a string variable to act as some kind of a Flag, which'll be marked as 'processing'. When the response event fires, I'll examine its contents, and either clear the flag, or set it to some kind of other error.

Then, I have created a new 'EmptyStringValidator' will check the contents of this flag, and do its job accordingly.

Its indirect, but, so far, seems to work.

reidLinden