This repository has been archived by the owner. It is now read-only.

Set default value for field? #269

Closed
s992 opened this Issue May 1, 2015 · 6 comments

Comments

Projects
None yet
2 participants
@s992
Copy link

s992 commented May 1, 2015

Is it possible to set a default value for a field? I've been perusing the code, but I don't see any obvious way to do so. My use case is a form that has values pre-populated (e.g. the user is returning to edit a form that they've already completed).

I can manually set the values by storing a value in my field's data object and then iterating over the fields to set the value into the model, but it becomes cumbersome to do so when working with nested fields, like the ones in the Advanced Layout example.

I tried setting initialValue on my field options, but that appears to get overwritten in formly-field.js. The relevant line is:

const initialValue = $scope.model && $scope.model[key];

This variable is then extended onto the field options, overwriting it. I was able to get the behavior I'm looking for by changing the line to

const initialValue = options.initialValue || ( $scope.model && $scope.model[key] );

and then iterating each of my fields to call resetModel(). This is still somewhat cumbersome, but not as bad as setting the model value manually.

Would you consider a pull request to set the model automatically if an initialValue exists and the model has not already been set? Probably something like this, executed right after addModelWatcher():

function setModelValue() {
    $timeout(function() {
        var field = $scope.options;
        if(!angular.isDefined($scope.model[field.key]) && angular.isDefined(field.initialValue)) {
            field.resetModel();
        }
    });
}

Alternatively, if I'm missing something and this is already built in, please tell me how to do it! :)

@kentcdodds

This comment has been minimized.

Copy link
Member

kentcdodds commented May 1, 2015

Watch the first video on this page:
http://docs.angular-formly.com/v6.2.0/docs/learn-angular-formly

Toward the end I talk about this.

Let me know if you have more questions. Please close this issue if this
solves your problem :-)

  • Kent C. Dodds

(Sent from my mobile device, please forgive typos or brevity)
On May 1, 2015 2:12 PM, "Sean Walsh" notifications@github.com wrote:

Is it possible to set a default value for a field? I've been perusing the
code, but I don't see any obvious way to do so. My use case is a form that
has values pre-populated (e.g. the user is returning to edit a form that
they've already completed).

I can manually set the values by storing a value in my field's data
object and then iterating over the fields to set the value into the model,
but it becomes cumbersome to do so when working with nested fields, like
the ones in the Advanced Layout
http://angular-formly.com/#/example/other/advanced-layout example.

I tried setting initialValue on my field options, but that appears to get
overwritten in formly-field.js
https://github.com/formly-js/angular-formly/blob/master/src/directives/formly-field.js#L100.
The relevant line is:

const initialValue = $scope.model && $scope.model[key];

This variable is then extended onto the field options, overwriting it. I
was able to get the behavior I'm looking for by changing the line to

const initialValue = options.initialValue || ( $scope.model &&
$scope.model[key] );

and then iterating each of my fields to call resetModel(). This is still
somewhat cumbersome, but not as bad as setting the model value manually.

Would you consider a pull request to set the model automatically if an
initialValue exists and the model has not already been set? Probably
something like this, executed right after addModelWatcher():

function setModelValue() {
$timeout(function() {
var field = $scope.options;
if(!angular.isDefined($scope.model[field.key]) && angular.isDefined(field.initialValue)) {
field.resetModel();
}
});
}

Alternatively, if I'm missing something and this is already built in,
please tell me how to do it! :)


Reply to this email directly or view it on GitHub
#269.

@s992

This comment has been minimized.

Copy link
Author

s992 commented May 1, 2015

The approach you mention in your video (vm.model.firstName = 'Obi wan') is what I'm hoping to avoid. My fields are being retrieved from the server as JSON, so I'd like to specify the value in that JSON and have it set into the model automatically when Formly is building my input. Right now, I have to do something like this:

angular.forEach( fields, function( field ) {
    model[field.key] = field.data.value;
});

This works fine, but it gets difficult to reason about once you start nesting fields to accomplish some of the more advanced layouts. Some of this difficulty can be alleviated by using a recursive function, but it's still not as intuitive (for me, at least) as having Formly set the model value automatically.

@kentcdodds

This comment has been minimized.

Copy link
Member

kentcdodds commented May 1, 2015

Actually, I just had a conversation with @ckniffen about this on gitter and I finally understand why people want this feature. It's pretty much what you described. I'll try to get this in a 6.3.0 very soon. Thanks for the issue!

@s992

This comment has been minimized.

Copy link
Author

s992 commented May 1, 2015

Great, looking forward to it!

@kentcdodds kentcdodds closed this in a567e8c May 1, 2015

@s992

This comment has been minimized.

Copy link
Author

s992 commented May 1, 2015

Quick note on this - if defaultValue is falsy, it will not be set on the model. Use case:

{
    "type": "radio",
    "key": "falseValues",
    "defaultValue": false,
    "templateOptions": {
        "label": "Does this work with false values?",
        "options": [
            {
                "name": "Yes",
                "value": true
            },
            {
                "name": "No",
                "value": false
            }
        ]
    }
}

@kentcdodds kentcdodds reopened this May 1, 2015

@kentcdodds

This comment has been minimized.

Copy link
Member

kentcdodds commented May 1, 2015

can't believe I overlooked that... Thanks

@kentcdodds kentcdodds closed this in 9451278 May 1, 2015

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.