Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Placing custom validation template #456

Open
gentledepp opened this issue Sep 28, 2014 · 2 comments
Open

Placing custom validation template #456

gentledepp opened this issue Sep 28, 2014 · 2 comments

Comments

@gentledepp
Copy link

Hi!

I have the need to place the validation message somewhere else in the dom. Additionally I want to also display validation messages for other bindings than the "value" binding. This is because I am using "knockout kendo" and that in turn does not always use the "value" binding only.

So one solution would be using the "validationMessage" binding, but that is insufficient as I cannot reuse my "messageTemplate" there.

So instead of

I created an additional binding handler

the "validationElementFor" binding replaces its owning element with the automatically created knockout validation validationElement and also takes the "messageTemplate" into account.

Here is the binding:

ko.bindingHandlers['validationElementFor'] = {
    update: function (element, valueAccessor) {
        var obsv = valueAccessor(),
            config =    utils.getConfigOptions(element), // THIS REFERENCES INTERNAL utils FUNCTION!!
            val = ko.utils.unwrapObservable(obsv),
            msg = null,
            isModified = false,
            isValid = false;

        // if we're told to use a template, make sure that gets rendered
        if (config.messageTemplate) {
            ko.renderTemplate(config.messageTemplate, { field: valueAccessor() }, null, element, 'replaceNode');
        } else {
            ko.applyBindingsToNode(element, { validationMessage: valueAccessor() });
        }

    }
};

My problem: I cannot re-use the function utils.getConfigOptions(element) if I only add the binding in a separate javascript module and I do not want to copy loads of internal knockout validation stuff (such as the complete utils part) in order to get this working.
Could you provide me with some hint in how to do this best? Or even add this (or a similar) binding to the knockout validation library?

@crissdev
Copy link
Member

@gentledepp Why not making your kendo bindings validatable? See this line of code for an example.

@davhdavh
Copy link
Contributor

davhdavh commented Dec 8, 2014

+1 to this... And also allow it to be a virtual element

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants