gcanti / tcomb-form

Forms library for react
https://gcanti.github.io/tcomb-form
MIT License
1.16k stars 136 forks source link

Get values to t.list() from db #311

Closed ljack closed 8 years ago

ljack commented 8 years ago

Hi, I's going through the documents but being new to tcomb and tcomb-form I don't understand how to achieve following: I want to be able to fill in possible values to t.list just before rendreing the

module 1
const Users = t.enums.of [ "user1","user2];
const Schema = t.struct({
  sharedTo: t.list( Users ),
  });
module 2 , imports module 1
      // modify Schema here by  changing what are possible values to sharedTo t.list..
     // e.g. get list of users from db and put it into the schema, but how?
    <t.form.Form ref="form" type={Schema} value={json}/>

Maybe I'm doing something upside down but just can't figure it out ;)

gcanti commented 8 years ago

Can't you just create the schema dinamically when you get the users?

ljack commented 8 years ago

Yes, I might have to reconsider my approach.

2016-04-06 13:47 GMT+03:00 Giulio Canti notifications@github.com:

Can't you just create the schema dinamically when you get the users?

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/gcanti/tcomb-form/issues/311#issuecomment-206305045

gcanti commented 8 years ago

Ok, feel free to re-open this issue if you need help.

ljack commented 8 years ago

Ok, changed how the schema is generated

export default function() {
  var users = Meteor.users.find().fetch();
  var values = {};
  users.map((user) => {
    values[user._id] = user.profile.name;
  });
  var Users = t.enums(values, "Users");
  var Schema = t.struct({
    _id: t.maybe(t.String),
    name: t.String, // a required string
    owner: t.maybe(t.String),
    sharedTo: t.list(Users),
    done: t.Boolean

  });

  return Schema;
}

And it works ;)

Now the next thing I'd like archive is that in the tcomb-form UI when ever I add one of the users I'd like to remove if from the selectable values.. So that if the Todo is shared to everyone there're no more users to add. And also to prevent from adding the same user multiple times.

Any hints?