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

Writing a custom Required True rule. #293

Closed
AJMcKane opened this issue Jun 14, 2013 · 1 comment
Closed

Writing a custom Required True rule. #293

AJMcKane opened this issue Jun 14, 2013 · 1 comment

Comments

@AJMcKane
Copy link

Hi there,

I'm brand new to KO and KO validation and I was wondering if anyone could help point me in the right direction?

I've created a form running as a SPA but I need validation on text areas to activate depending on the previous's select element's value (which is bound to a knockout value).

For example,
self.standardConstruction = ko.observable().extend({ required: true });
self.constructionType = ko.observable().extend({ required: {onlyIf: standardConstruction() === 'Yes'} });

However, I've found the onlyIf statement doesn't allow for anonymous functions and I can't set the array that populates my "[blank] ,'Yes', 'No'" fields to actually equate to true/false in the code behind.

I'm hoping there's a way of writing a custom validation rule that allows me to pass a value, an expected value and then activate the required: true flag based off that!

////Knockout validation rules
//ko.validation.rules['isReqYes'] = {
// validator: function( param, expected) {
// if(param === expected)
// {
// required: true
// }
// },
//
// message: 'This field is required.',
//};

Unfortunately I can't find much documentation and my experience is lacking so I'm making no headway! Any and all help is appreciated :D

Also, thanks for the library, it's been a great help with my project so far!
Regards,
Antony

@crissdev
Copy link
Member

Duplicate of #374

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

2 participants