Skip to content

Custom tv4 keyword ignored by $broadcast('schemaFormValidate') #633

@kohanyirobert

Description

@kohanyirobert

I've a custom tv4 keyword defined like this

tv4.defineKeyword('email', function(data, value, schema) {
    if (schema.email) {
        if (/^\S+@\S+$/.test(data)) {
            return null;
        }
        return {code: 10001};
    }
    return null;
});

and a custom error like this

tv4.defineError('EMAIL', 10001, 'Invalid email address');

and I'm configuring sfErrorMessageProvider like this on startup

app.config(['sfErrorMessageProvider', function(sfErrorMessageProvider) {
    sfErrorMessageProvider.setDefaultMessage(10001, 'Invalid email address');
}]);

In my schema I have the following definition

"CONTACTEMAIL": {
     "type": "string",
     "maxLength": 255,
     "email": true
}

My field definition is like this

{
     "key": "CONTACTEMAIL",
     "placeholder": "Enter contact email",
     "feedback": false
}

Before submitting my form I call $scope.$broadcast('schemaFormValidate'), however the field is not validated. For example when it contains the data "NULL" (as a string). This happens when I open an edit form and supply the form data, the model value directly. The field will be initialised with the value "NULL".

However, if I edit the field manually everything works just fine. The field is marked as invalid, my error message shows up.

Right now, as a workaround I'm marking my form with sf-options="{validateOnRender: true}". This way the following happens:

  • the field initially won't be marked as erroneous (at least not with the related CSS classes, so the field is not red)
  • when I call $broadcast('schemaFormValidate') it will be marked as erroneous (it'll be red)
  • however, the validation message won't show up until I type something in the field

So, validateOnRender: true isn't completely ideal, but this at least prevents invalid form submission.

Any idea?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions