Knockout-Validation icon indicating copy to clipboard operation
Knockout-Validation copied to clipboard

Validation message computed based on the value of the field

Open RCMax opened this issue 10 years ago • 3 comments
trafficstars

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?

RCMax avatar Oct 14 '15 13:10 RCMax

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

crissdev avatar Oct 14 '15 16:10 crissdev

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.

RCMax avatar Oct 14 '15 17:10 RCMax

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

crissdev avatar Oct 14 '15 21:10 crissdev