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

AccountsTemplates.addFields using dynamic values for select #569

Closed
Kunal1985 opened this issue Oct 28, 2015 · 9 comments
Closed

AccountsTemplates.addFields using dynamic values for select #569

Kunal1985 opened this issue Oct 28, 2015 · 9 comments

Comments

@Kunal1985
Copy link

Hi,

I am trying to assign dynamic values to select option as below:
AccountsTemplates.addFields([
{
_id: 'customField',
type: 'select',
displayName: 'Custom Field',
select: custFunct(),
}
]);

custFunct() is a function i have defined as below
var custFunct = function() {
var jsonArr = [];
var jsonObj = {};
jsonObj.text = "Cust1";
jsonObj.value = "cust1";
jsonArr.push(jsonObj);

jsonObj = {};
jsonObj.text = "Cust2";
jsonObj.value = "cust2";
jsonArr.push(jsonObj);

return jsonArr;

};

Post this I am able to see the dropdown getting populated with Cust1 and Cust2 correctly

Now, i have to get these values from one of my collection which is not working as below
var custFunct = function() {
return CustCollection.find().fetch(); // this line gives error that CustCollection is not defined.
};

Kindly suggest how to proceed.

@cunneen
Copy link

cunneen commented Oct 30, 2015

+1. I can't figure out any way to get reactive data sources in a SELECT using the default templates. It won't work in a Tracker.autorun() block.

@splendido
Copy link
Member

Have you already found out about #356 and #328?

@cunneen
Copy link

cunneen commented Oct 30, 2015

No, I searched for but didn't find them. Thanks!
For the record, my solution is the same as your last comment on #328. But it would be nice for the default templates to be reactive.

@cunneen
Copy link

cunneen commented Oct 30, 2015

@splendido any idea about the best way to validate dynamic selects as per your suggestion in #328 ?

@splendido
Copy link
Member

I think a custom valudation function could be used to pick up values from the collection you're using and check one of those was actually chosen.

@cunneen
Copy link

cunneen commented Nov 2, 2015

custom validation functions aren't getting run when I use a custom template.

@splendido
Copy link
Member

this is weird...
how are you defining your custom template?
do you have a public reproduction for the problem?

@cunneen
Copy link

cunneen commented Nov 5, 2015

This link should illustrate the problem. How can I hook the select's validation function to its onchange event?

http://meteorpad.com/pad/t5LdBwn6YiXjCFwko/UserAccounts%20Validation

@splendido
Copy link
Member

@cunneen, sorry for the late reply.

I had a look at the problem and it's a mixture of things:

  1. validation events for selects never fired so far! My bad :-(
  2. to actually get validation performed you should keep the correct naming for fields' id: at-field-school is what you need (at-field-<field_name> is what is used for every template, lets have a look here for example)
<template name="selectSchool">
  <label for="at-field-school">{{displayName}}</label>
  <select id="at-field-{{_id}}" name="at-field-{{_id}}">
    <option value="select">Please select</option>
    <option value="one">School One</option>
    <option value="two">School Two</option>
  </select>
</template>

then you also need to enable validation setting to true either negativeValidation or positiveValidation (or both, see the Guide) when you configure the field:

AccountsTemplates.addFields([
    /* ... */
    {
        _id: 'school', //Add a school select to the form
        type: 'select',
        displayName: 'Select a school',
        select: [],
        template: "selectSchool",
        errStr: 'Select a School',
        func: function(value){
            if (Meteor.isClient) {
                    console.log("Validating school..." + value);
                    this.setSuccess();
                    // or this.setError(userExists);
                    this.setValidating(false);

                    return;
            }
        },
        negativeValidation: true,
    },
    /* ... */
]);

hopefully this should be enough to solve your issue!

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

3 participants