Knockout-Contrib / Knockout-Validation

A validation library for Knockout JS
1.02k stars 379 forks source link

Validation message computed based on the value of the field #588

Open RCMax opened 8 years ago

RCMax commented 8 years ago

As I see it, you currently can compute a validation message based on parameters, by using the template system.

The problem I have is that you cannot do the same based on the value of the field.

Let say you have a select field that supports tagging and you want apply validation to each value. It would be nice to able to include the invalid values in the validation message.

select field : values States : New York, Maine, Quebec, Montreal

Ex. Some values are invalid: Quebec, Montreal

This example is not the best since I would suggest states instead of allowing tagging, but the need is still there.

Actually, you can do this with async validation rules since you can return an object. like this :

return { isValid: false, message: 'Some values are invalid: Quebec, Montreal' };

Is it possible to do the same with normal (not async) validation?

crissdev commented 8 years ago

@RCMax Are you after some like this? http://jsfiddle.net/40s6w103/

RCMax commented 8 years ago

I don't think so.

I want to create a reusable custom validation rule. The message logic shouldn't have to be specified/re-specified every time someone uses this custom validation rule. I would like this logic to be the responsibility of the custom validation rule, not the caller.

crissdev commented 8 years ago

I thought you're familiar with anonymous rules so I've defined it like that. Here's the updated example http://jsfiddle.net/40s6w103/2/

Will that help in your case? Thanks