LIVR Validate if element in object is empty array

Viewed 14

I have LIVR in a project i'm working now and is quite unclear to me how this work. I can't understand how to create new rules for custom validation. Here's the code:

LIVR.Validator.defaultAutoTrim(true);
let validator = new LIVR.Validator({});
LIVR.Validator.registerDefaultRules({
    nested_object_value() {

        return function (value) {
            if (!value || (value && !value.value || value === [])) {
                return 'REQUIRED';
            }

            return '';
        };
    },
    max_number_advancement() {
        return function (value) {
            if (value > 100) {
                return 'MAX_NUMBER';
            }
            return '';
        };
    },
    required_if_activity_present() {

        return function (value, allValue) {

            if (allValue.activitycycletype && !value || allValue.requestpeople === []) {
                console.log(first)
                return 'REQUIRED_IF_CYCLETYPE';
            }

            return '';
        };
    },
});

And this is how its used:

 validationForm = () => {
        const { formValue, updateErrors } = this.props;
        const validData = validator.validate(formValue);
        console.log(formValue)
        if (!validData) {
            const errorsValidator = validator.getErrors();
            if (errorsValidator && Object.keys(errorsValidator).length > 0) {
                const newErrors = {};
                Object.keys(errorsValidator).forEach((error) => {
                    newErrors[error] = errorsValidator[error];
                });
                updateErrors(newErrors);
            }
            blame(t('validation-error'));
            return false;
        }
        updateErrors({});
        return true;
    }

Opening the form with this validation in the app, seems to call only the last method required_if_activity_present(). What i expect here is that i can create a new method inside registerDefaultRules(), that is a LIVR method, like this:

LIVR.Validator.registerDefaultRules({
re quired_not_empty() {

        return function (value) {

            if (!value) {
                return 'REQUIRED';
            }

            return '';
        };
    },
... //other methods
}

but seems not working, the newer method is not being called at all by validator.validate() Anyone know how to create a new rules where i can check if an element inside the object that has to be validate is an empty array? Because seems that LIVR doesn't return a validation error in this case, but only on empty string and null values.

Thanks in advance

0 Answers
Related