Looking for clarification regarding form validatio...
# suitecommerce
c
Looking for clarification regarding form validation when using SCFormView. Using the GitHub sample code 2020_2, Example-part9. In the past, when validating front-end form data, you would extend the Backbone.Model and use
validation : { }
. However, within @Steve Goldberg example 2020_2 Example-part9, front end form data is validated via
ExampleUserPreferencesModel.prototype.getValidationRules
. Help me understand what the following code does in that example.
Copy code
ExampleUserPreferencesFormView.prototype.getFormFieldValue = function (input) {
        var field = {
            name: input.attr('name'),
            value: input.val()
        };

        if (!this.formModel.validate(field)) {
            SCFormView.prototype.removeErrorMessage.call(this, field.name)
        }

        return field
    }
s
😅 Did I not explain it clearly in the article?
Anyway, you're looking to pair this method with
getValidationRules()
in the model
The model is determining what property:value pairs it will accept
So the method you've posted is grabbing the input (literally the input element) and then building up an object of its details, called
field
At this point, we could do nothing and return
field
which then gets passed on to whatever called it
Instead, we're calling the
validate
function built in to the model
If the value is no good it will show an error message, but our code is saying "if it validates, remove any error message that may be showing for this field" because if the field failed validation checks b efore, it will be showing an error message
c
So getValidationRules should look something like this? Can we no longer use the second block of code?
Copy code
MySCModel.prototype.getValidationRules = function () {

        return {
            firstname: [
                function (value, name) {
                    if (typeof value === 'undefined' || value.length === 0 || !value.trim()) {
                        return _('First name is required').translate();
                    }
                }
            ],
            lastname: [
                function (value, name) {
                    if (typeof value === 'undefined' || value.length === 0 || !value.trim()) {
                        return _('Last name is required').translate();
                    }
                }
            ]
        };
    }
Copy code
validation: {
            firstname: {
                required: true,
                msg: _('First name is required').translate()
            },
            lastname: {
                required: true,
                msg: _('Last name is required').translate()
            }
        }
Also, do we need to add ValidationUtils as a dependency?
s
You're incorrectly using the string 'first name' in the
lastname
block but yes that first block looks fine to me
And no, you don't set
validation
properties anymore
👍🏻 1
c
Is it still possible to use
___.validatePhone
_.validateZipCode
_.validateState
?
s
So, it is still possible but the impression I got from the developers at the time was that they didn't like the idea of calling those core modules in extensions. I think if you're customising SCA then you're probably OK to use them
(I'm talking about the Backbone Validation methods, I don't think we bind validation methods to the Underscore global var)
👍🏻 1
c
@Steve Goldberg How would we continue to use
state: { fn: _.validateState }
to validate the state dropdown?
s
I don't have any info on that
c
I ask because the validate method allowed you to pass the value, field, and form data into the function. However, getValidationRules() only contains the value and name of the current input.