How do I clean up data prior to validation?
How do I clean up data prior to validation?
I have a form with a phone number field which I'm storing as digits in the DB. On the data table, I'm displaying it with dashes (like 904-123-4567). Similarly, I have another field with a string of 20 digits, which I'm breaking up with spaces.
I'd like to strip any dashes or spaces that users might enter into the form prior to running the validation. Is there a function I've overlooked that would let me filter this data out before the validation runs against it? Can this cleanup be done in a custom validation, then passed to some of the built-in validators?
This question has an accepted answers - jump to answer
Answers
The validator only validates the data, it doesn't format it. For formatting, you want a formatter :-). Specifically a set formatter in this case - the documentation for the PHP and .NET libraries cover how you can create custom formatters.
Regards,
Allan
According to the validation docs:
I am looking for a routine that lets me alter the data prior to the validation routines running against it, so that I can strip out dashes or any other formatting characters. This would let someone copy and paste data in that's been formatted, then let Editor strip out the unneeded characters to clean it up.
I went ahead and tried a simple custom setFormatter:
But, that only confirmed that the validation gets executed prior to the setFormatter.
For this field, I have these validators in place:
Without some sort of preValidation routine that allows me to alter the data, I'm thinking that I will need to write a single validator function that first strips out the characters I want to remove, then does the numeric, unique, and length validations.
Any other ways to go about it?
There isn't one built into the Editor PHP libraries. You would need to either pre-process the data before handing it into the
process()
method, or do it on the client-side usingpreSubmit
.Allan
Handled it with the preSubmit to strip "-" from phone numbers, as well as spaces. Thanks!