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

default value #60

Closed
Nemo64 opened this issue Jan 26, 2014 · 10 comments
Closed

default value #60

Nemo64 opened this issue Jan 26, 2014 · 10 comments

Comments

@Nemo64
Copy link
Contributor

Nemo64 commented Jan 26, 2014

I miss an option defaultValue which would be inserted by the clean method. I know that collection2 delivers a autoValue but things aren't always inserted into the database and even then it takes some of the same logic every time I want to implement a default value.

@aldeed
Copy link
Collaborator

aldeed commented Jan 27, 2014

Can you provide an example use case?

@Nemo64
Copy link
Contributor Author

Nemo64 commented Jan 27, 2014

// lets say you create blog posts
new SimpleSchema({
    visable: {
        type: boolean,
        defaultValue: false // not visable if not explicitly told
    },
    // ...
    comments: {
        type: [CommentSchema],
        defaultValue: [] // should be an empty array on creation
    }
});

I can probably think of more examples but you should get the idea.
This would also allow autoform to read defaultValues out of the schema into a form.
The default value could also kick in if you read from the database and the field is not defined (to make sure there is an array instead of undefined) so you can extend your schema later without needing to check everywhere if the field is defined or to create a database query to extend all existing entries.

I also had the idea of making defaultValue a callback like label for multiple reasons.

  1. The default value must be cloned before inserting it into the data and a callback would let the developer decide if a clone or a reference should be inserted.
  2. My old translation reason. It would again allow to change the language later and then changing the default accordingly (even though it would not change existing values)
city: {
    type: String,
    defaultValue: function () {
        return (Meteor.isClient &&  geoLocationVariable)
                ? geoLocationVariable : __("city.default")
    }
}

With autoValue this would not work as the data is (if you documentation is correct) generated on the server which

  1. does not have access to the Session and therefor the translation fails or falls back to default language (depending on translation toolkit)
  2. does not have access to the geolocation service of the browser in that example.

@aldeed
Copy link
Collaborator

aldeed commented Jan 27, 2014

But what is the overall context for this example? Because if it's an autoform, then you can use formToDoc or a before hook to set default values on the client. If it's something else, then you can theoretically set the default value manually.

I'm not saying this shouldn't be done; it's probably a good idea, but I'd like to have a clear grasp of the legitimate use cases to make sure it's implemented correctly.

It's been suggested also that a more basic autoValue should be part of simple-schema, which might make sense. Then C2 would simply extend the SS autoValue to add on the isInsert, etc. That idea intertwines with this idea a bit.

@Nemo64
Copy link
Contributor Author

Nemo64 commented Jan 27, 2014

Wait, autoValue and defaultValue are different. The thing is that defaultValue is only called (or used) if there is no value and should be added every time clean is called.

It may be true that there are ways with autoform and collection2 to get this functionality already.
In my specific use case I wanted to set a default values for a city in a user profile as I know where most of my user would come from ( if it wouldn't be an experiment ;) ). I think it's just fitting to put that into the schema with a simple defaultValue: "Berlin" instead of creating an object with those values set before giving it to the form or database.

@aldeed
Copy link
Collaborator

aldeed commented Jan 27, 2014

Yes, there is value in having a defaultValue that is not a callback, as a simpler way to do what autoValue can already do. However, if you need complex logic, then you might as well use the autoValue that is already available, or use a hook on the client.

So I think I'd be OK with adding defaultValue that only supports simple non-callback values. Will continue to think about it a bit more, though.

@Nemo64
Copy link
Contributor Author

Nemo64 commented Jan 27, 2014

you are probably right... I'm always trying to make things too flexible ;) also my translations probably don't belong into a default value.

however there are problems with a static value. how do you copy it safely (without making references)?

@aaronjudd
Copy link

I'll +1 the concept of a defaultValue. As the number of fields we have increases, I don't really want to program hooks or autovalues for everything - just the major cases. Just being able to set a boolean to default to true would be nice (again, simple value, not a callback). Not a must have, but just sounds convenient.

@aldeed
Copy link
Collaborator

aldeed commented Feb 20, 2014

Today's release includes a defaultValue option, and also moves autoValue out of Collection2 and into SimpleSchema. See the readme. Let me know if anyone has issues.

@aldeed aldeed closed this as completed Feb 20, 2014
@AndyWilkinUK
Copy link

Liking the defaultValue, works well with the select box, automatically selects that option. Though is there anyway I can use that value to pre-fill the input box in autoform?

Apologies - does work something wrong with Meteor hot code reload :(

@vincro
Copy link

vincro commented Oct 20, 2015

I can see no apparent and obvious way to set default values on array fields. I am using a custom template within which I have afArrayEachItem, but when clicking .autoform-add-item, the entry comes up blank, with no preselected value.

tracking a dependency on the form.field.deps, doesn't work without using _.delay, as one cannot guarantee items have been added to the dom reactively, and surely using a default element in the field is the best option.

Any direction would be handy.
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants