Reputation: 5254
This is my form:
<input type="checkbox" name="domainSelected" id="domainCheckbox" value="True">Would you like a domain URL?
<div class="col-md-12">
{{> afQuickField name="domainURL" }}
</div>
I've got this in my schema:
domainSelected: {
type: String,
label: "Domain Selected?",
optional: true
},
domainURL: {
type: String,
label: "Domain Name",
optional: // I want this to be false IF domainSelected exists. True IF domainSelected does not exist.
},
Can I get some help here? Would it be something like:
domainSelected: {
type: String,
label: "Domain Selected?",
optional: true
},
domainURL: {
type: String,
label: "Domain Name",
optional: function() {
if ( $('#domainCheckbox').prop('checked') ) {
return false;
} else {
return true;
};
}
},
When I do this my meteor doesn't load and I get a console error:
Error: Invalid definition for domainURL field.
EDIT
To clarify:
On my frontend I have a checkbox (#domainCheckbox) and a form field where a user can input a domain name as a string.
When the checkbox is selected, the form field is required in the schema and Simple-Schema's form validations will fail if the form field is left blank.
When the checkbox is not selected, the form field is not required in the schema and Simple-Schema will no longer check for the presence of something in that field.
EDIT Erm, actually the code works.... sometimes... other times meteor just crashes.
Upvotes: 1
Views: 2513
Reputation: 2484
Have a look here: simpleSchema
domainSelected: {
type: String,
label: "Domain Selected?",
optional: true
},
domainURL: {
type: Boolean,
label: "Domain Name",
autoValue: function() {
if ( this.field('domainSelected').value == '' ) {
return true;
} else {
return false;
};
}
},
You can access a field from another field. Checking again '' isnt maybe the best way but I think its the way to go.
Edit:
Ok, some ideas how I would do it. I would not insert the jQuery statement into the schema. The schema runs on client and server it will fail always on serverside.
Your domainSelected field is in the HTML-View a checkbox. A checkbox just takes true or false as argument. So the domainSelectedfield should be a Boolean. Thus a checkbox just has two states, true OR false it isnt an optional parameter. Because if the checkbox is not selected it is false.
domainSelected: {
type: Boolean,
label: "Domain Selected?"
}
If domainSelected is true than domainUrl is required. If domainSelected is false than domainUrl should be empty.
domainURL: {
type: String,
label: "Domain Name",
autoValue: function() {
if ( this.field('domainSelected').value === true && this.value !== "") {
return this.value;
} else if(this.field('domainSelected').value === false) {
return "";
}else
{
this.unset();
};
}
}
I didnt tested it but it should fit your solution.
Upvotes: 2