Meteor AutoForm simpleSchema this.field function does not work - meteor

I am getting a trouble to use SimpleSchema in Meteor.
What I've aimed is to change "min" field based on "type" field value so I wrote this code.
'min': {
type: Number,
optional: true,
label: function() {
if(this.field('type').value == "Year") {
return "Minimum Year"
}
return "Minimum Value"
}
},
But when I debug it, it shows this is blank object.
I don't have really idea how to handle this issue.
Please help me!

Hope this helps ,
{{> afQuickField name="min" label=checking}}
Template.x.helpers({
checking:function(){
if(Autoform.getFieldValue(field,formId)=="expected")
return "expected label"
else
return ""
}
})

Related

Meteor Autoform validateForm ignores unique

The method AutoForm.validateForm(formID) returns true although unique is true in SimpleSchema and a duplicate value is entered. Nobody else seems to have this issue so I wonder what I'm doing wrong. This is the full sample code:
common/collections.js
import SimpleSchema from 'simpl-schema';
SimpleSchema.extendOptions(['autoform']);
const afCollection = {};
Meteor.isClient && Template.registerHelper('afCollection', afCollection);
checkTable = afCollection.checkTable = new Meteor.Collection('checkTable');
// Meteor.isServer && checkTable._dropCollection();
checkTable.attachSchema(new SimpleSchema({
checkValue: {
type: String,
index:true,
unique:true,
optional:false
}
}, { tracker: Tracker }));
client/maintenance.js
AutoForm.debug();
Template.Maintenance.events({
'click .save' () {
if (AutoForm.validateForm("newOne")) {
$('form#newOne').submit() }
else {
console.log("should see error message now")
};
console.log("Saved:",checkTable.find().fetch())
}
});
client/maintenance.html
<template name="Maintenance">
<a class='save' href=#>Save</a>
{{#autoForm id='newOne' type="insert" collection=afCollection.checkTable autosave=false }}
{{> afQuickField name="checkValue"}}
{{/autoForm}}
</template>
packages:
aldeed:autoform#6.2.0
aldeed:collection2-core#2.0.4
aldeed:schema-index#2.1.1
validateForm works correctly in case of input is empty. In case of unique is violated, validateForm returns true. When you call .submit(), the error message in the template is displayed correctly and you could react on the error using an AutoForm.hook (probably, not tested).
Unfortunately this does not help in my situation, because clicking on "save" will submit several forms at once. I must ensure that all forms are error-free before the first one is submitted.
What am I missing?

how to enable or disable editing of a field value depending on another field's value?

I have two fields, one which need to be dependent on the other field's value. The 1st field is "RELEASE" which has "latest" and "on-going" as the dropdown options. The 2nd field is "BUILD" which should be editable only when we select "on-going" as the release. When the release is "latest" it should take a default value and not be editable.
test.html
{{> afQuickField name='Release' options='allowed' }}
<span title = "eg:PRODUCT/10.X.X.1234 or PRODUCT:latest">
<a style="font-size:1.2em"><h5>ProductBuild</h5></a>
</span>
{{> afQuickField name='PRODUCT_BUILD' }}
I'm also trying to set the tool tip for PRODUCT_BUILD field so that when a user hover over it, he/she will know the correct format like "PRODUCT:latest" for Latest and "PRODUCT/10.X.X.1234" for "On-going".
schema.js
Release:{
type: String,
label: "Release",
optional: true,
allowedValues:["LR-Latest Release","OR-Ongoing Release"],
autoform:{
afFieldInput:{
firstOption:"(Select the Release)"
}
}
},
PRODUCT_BUILD:{
type:String,
label:' ',
regEx: /^(PRODUCT)(\/|:)((([0-9]+\.)+[0-9]+)|(latest))/,
defaultValue:"PRODUCT:latest"
},
How can I do this?
One of two ways of doing this:
One: Use readonly property within the autoform.
Release:{
type: String,
label: "Release",
optional: true,
allowedValues:["LR-Latest Release","OR-Ongoing Release"],
autoform:{
afFieldInput:{
firstOption:"(Select the Release)"
}
}
},
PRODUCT_BUILD:{
type:String,
label:' ',
regEx: /^(PRODUCT)(\/|:)((([0-9]+\.)+[0-9]+)|(latest))/,
defaultValue:"PRODUCT:latest",
autoform:{
readonly: function(){
if(AutoForm.getFieldValue('Release') == 'on-going'){
// if the above does not get you the "Release" field's value then try:
// AutoForm.getFieldValue('Release','formID');
// if your formID is dynamically set, then use AutoForm.getFormId(); to get the form's ID
return false;
}
else {
return true;
}
}
}
},
Two: Use a custom function to set/unset readonly property
Release:{
type: String,
label: "Release",
optional: true,
allowedValues:["LR-Latest Release","OR-Ongoing Release"],
autoform:{
afFieldInput:{
firstOption:"(Select the Release)"
}
},
custom: function(){
if(this.value == 'on-going'){
$('[name=PRODUCT_BUILD]').prop('readonly', true);
}
else {
$('[name=PRODUCT_BUILD]').prop('readonly', false);
}
}
},
PRODUCT_BUILD:{
type:String,
label:' ',
regEx: /^(PRODUCT)(\/|:)((([0-9]+\.)+[0-9]+)|(latest))/,
defaultValue:"PRODUCT:latest",
},
You can play around with both methods here before you actually try implementing it.

Access reactive-table row data in template/helper?

For Tabular:
“In your template and helpers, this is set to the document for the
current row”
Is there an equivalent of this for reactive-table? I need to access a rows data in my template/helper but am just struggling to find a way to access it, with tabular it was as easy as using this.
I'm using a template for a column called "Status" and in this there are different types of labels, depending on what the row data returns it will be a different type of label. The code below works for Tabular but I'm not really sure how to make this work for reactive-table?
Example.html
<template name="ApplicationStatus">
<div class="row">
{{#if statusPending}}
<label class="label label-warning label-xs">"Pending</label>
{{/if}}
{{#if statusConnected}}
<label class="label label-primary label-xs">Connected</label>
{{/if}}
</div>
</template>
Example.js
Template.ApplicationStatus.helpers({
statusPending: function() {
if (this.applications.app_status === 'Pending')
return true;
else
return false;
},
statusConnected: function() {
if (this.applications.app_status === 'Connected')
return true;
else
return false;
}
});
I am currently adding it to my reactive-table by doing this:
{ tmpl: Template.ApplicationStatus, label: 'Status' }
Any info is greatly appreciated or if there's a better way to achieve what I'm trying to achieve I would love to hear that as well!
TL; DR
Try Template.instance().data instead of this.
Little explanation
I am not shure what happenes when you miss key definition, but accoriding to docs:
You can specify a template to use to render cells in a column, by
adding tmpl to the field options.
{ fields: [
{ key: 'name', label: 'Name', tmpl: Template.nameTmpl },
{ key: 'location', label: 'Location', tmpl: Template.locationTmpl }
] }
The template's context will be the full object, so it will have access
to all fields.
So inside helpers and event handlers you can get access to full row object via Template.instance().data.

Meteor: Publish a collection and attach it to the appropriate user

I have a list of student users and collection of classes. When I publish the list of students I want it to display the classes they are attending. My code currently displays all the classes that every student is attending under each student instead of the classes that are relevant to the individual student.
How can I get it to display the right classes under the right stendents.
Path: classes.js
Template.classes.helpers({
studentList: ()=> {
return Meteor.users.find({_id: { $ne: Meteor.userId() }});
},
classes: ()=> {
return Classes.find({_id: { $ne: Meteor.userId() }});
},
});
Path: classes.html
{{#each studentList}}
{{profile.firstName}}
{{#each classes}}
{{class}}
{{/each}}
{{/each}}
Path: Classes.js
Schemas.Classes = new SimpleSchema({
class: {
type: String
},
teacherUserId: {
type: String,
autoValue: function() {
return this.userId
},
autoform: {
type: "hidden"
}
},
studentUserId: {
type: String,
optional: true,
autoform: {
type: "hidden"
}
}
});
The code above means: "Find all classes where the id of the class isn't equal to the current user's id." I think you want: "Find all classes where the current user's id is in the list of students."
Assuming classes have a students field, which is an array of user ids, you'd do this:
return Classes.find({students: Meteor.userId()});
Thanks to everyone's input my problem is solved! Tim hit the nail on the head. For those of you encountering a similar problem, check out the code below. I hope it helps. Thanks again Tim.
Path: classes.js
Template.classes.helpers({
classes: ()=> {
return JobOffers.findOne({candidateUserId: this._id});
},
});
Path: classes.html
{{#each studentList}}
{{profile.firstName}}
{{#with classes}}
{{class}}
{{/with}}
{{/each}}
When you are defining classes: () => Classes.find({_id: { $ne: Meteor.userId() }}) what you are telling the computer is:
Every time I ask you for the box labeled 'classes' I want you to go
through the box we called 'Classes' and fill 'classes' with everything
you find that doesn't have the '_id' property set to whatever you find
when you look inside of the box that 'Meteor.userId()' gives you.
That is not what you are wanting to ask your worker for. You want to ask your worker:
Every time I ask you for the box labeled 'classes' I want you to go
through the box we called 'Classes' and fill 'classes' with everything
that you find where the '_id' is set to a certain string that I am passing
you.
Which, without really trying to write it for you, it might have something to do with this being used somewhere in your helper function

Publish/subscribe not working - how to debug?

I am trying to switch my app from autopublish to publish/subsribe. After trying to set publish/subscribe up or my first collection, none of my items from the collection are being displayed anymore, and I am at a loss at what might be going wrong.
Can someone spot a mistake in my code below or give any hints as to how to debug this type of problem?
client/routes.js
Router.route('/slug', {
name: 'route.name',
title: 'My Page',
template: 'TemplateName',
subscriptions: function() {
this.subscribe('myPublication');
},
action: function() {
this.render();
}
});
server/lib/collectionLib.js
...
Meteor.publish('myPublication', function() {
return MyCollection.find();
});
client/myCollection/view/mycollection-list.html
...
{{#each myCollection}}
...
{{/each}}
...
I was missing the exposure of the collection to the template. See answer provided here: https://forums.meteor.com/t/publish-subscribe-not-working-how-to-debug/13127/6?u=codejak
Oh I see now, unless you are doing MyCollection.find() in your template helper you need to do it in the route:
data: function () {
return MyCollection.find({}).fetch();
}

Resources