Can you alter a form item's contents after a failed validation?

Figured i’d post my questions rather than exclusively nagging @samrueby about them today! I already typed this out before thinking that it would probably fit better at stack overflow.

Technical question: is it possible to alter the content of a form item during validation (when a validation fails)? For an abstract example: say you have a text box that you want to be spell checked before it gets entered into the database. You use the modification’s GetSpellCheckedWordTextFormItem to get the form item, and you want to replace what the user enters (“teh”) with a likely suggestion (“the”) when the validation fails to find a word it knows. Then the user sees the validation error (“Is this the word you meant?”), looks at it and corrects it or not, then re-submits.

Is there a way to do that? If so, how?

(I already typed this out before thinking that it would probably fit better at the stack overflow tag. Whoooooops.)

Yes, this would be better on Stack Overflow.

But…the specific answer to your question is no, you can’t alter any form values if validation fails. To implement this, you’d need to let the validation succeed and let the data get modified. As part of the validation/modification, you could set a piece of page state that causes the next loadData pass to display the “is this the word you meant?” message near the spell-checked form item. Of course, you would have already saved the corrected text.

Alternatively, you could use PostBack.CreateIntermediate to make a post-back that only runs the spell-check, puts the corrected text in page state, and displays “is this the word you meant?”. You’d set that post-back to fire when the user tabs out of the text box, and then you’d have the main post-back grab the corrected text from page state and save it in the database or other durable storage.

Can you cross-post this on Stack Overflow so I can move my answer there?

Thanks!

Cross posted: http://stackoverflow.com/questions/23165935/in-ewl-can-you-alter-a-form-items-contents-after-a-failed-validation

Cool. Please accept and upvote my answer over there, if it’s helpful. It’s in our best interest to build up each other’s reputation so we have more power on the site. :smile:

1 Like