Skip to content

Latest commit

 

History

History
32 lines (27 loc) · 1.11 KB

3_8.md

File metadata and controls

32 lines (27 loc) · 1.11 KB

3.8 Custom data transformers

If you use a custom view transformer, implement the matching transformer on the JavaScript side so validation receives the same normalized value.

For a PHP view transformer such as App\Form\DataTransformer\MyTransformer, register a JavaScript constructor in Svaroh.transformers under the class name with the namespace separators removed:

<script type="text/javascript">
    Svaroh.transformers.AppFormDataTransformerMyTransformer = function() {
        /**
         * Extra options from the PHP transformer can be copied into this object.
         */
        this.extraOption = '';

        /**
         * This method is required.
         * It should return the value that will be validated.
         */
        this.reverseTransform = function(value, model) {
            // Compose the real value here.
            return value;
        };
    };
</script>

Svaroh is the namespace of the library, as described in the README. Transformers declared as a global function of the same name still work, but that form is deprecated and support for it will be dropped in a future release.