How do I clean up data prior to validation?

How do I clean up data prior to validation?

ptaylorptaylor Posts: 28Questions: 7Answers: 0

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

  • allanallan Posts: 63,680Questions: 1Answers: 10,498 Site admin

    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

  • ptaylorptaylor Posts: 28Questions: 7Answers: 0
    edited May 2015

    According to the validation docs:

    Parameter 1: $val - The value to check for this field - this is the wire data - i.e. the raw 
    data submitted by the client, before any formatting has been applied by the 
    setFormatter() method (if it is used).
    

    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:

    ->setFormatter( function ($val, $data, $opts ) {
    return (str_replace('-','',$val));
    } )
    

    But, that only confirmed that the validation gets executed prior to the setFormatter.

    For this field, I have these validators in place:

    ->validator( 'Validate::numeric' )
    ->validator( 'Validate::unique' )
    ->validator( 'Validate::minMaxLen',10 ),
    

    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?

  • allanallan Posts: 63,680Questions: 1Answers: 10,498 Site admin
    Answer ✓

    I am looking for a routine that lets me alter the data prior to the validation routines running against 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 using preSubmit.

    Allan

  • ptaylorptaylor Posts: 28Questions: 7Answers: 0

    Handled it with the preSubmit to strip "-" from phone numbers, as well as spaces. Thanks!

        editor.on( 'preSubmit', function ( e, o, action ) {
            if ( action !== 'remove' ) {
                o.data.sims.phonenum = o.data.sims.phonenum.split('-').join('');
                o.data.sims.phonenum = o.data.sims.phonenum.split(' ').join('');
                o.data.sims.name = o.data.sims.name.split(' ').join('');
            }
        } );
    
This discussion has been closed.