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 in propObject not creating any property #551

Closed
Tim-mhn opened this issue Dec 14, 2021 · 2 comments
Closed

Default value in propObject not creating any property #551

Tim-mhn opened this issue Dec 14, 2021 · 2 comments

Comments

@Tim-mhn
Copy link

Tim-mhn commented Dec 14, 2021

Describe the bug

I'm creating a form model with rxweb/reactive-form-validators with a nested property. The documentation suggests to manually create a value for this in the component such as this

user.model.ts

export class Address {


  @prop({ defaultValue: 'Berlin' })
  city: string;

  @prop({ defaultValue: 'Germany' })
  country: string;
}



export class User {
  @prop({ defaultValue: 'Bob' })
  name: string;

  @prop({ defaultValue: 18 })
  age: number;

  @propObject(Address)
  address: Address;

}

app.component.ts

const user = new User(); 
user.address = new Address(); // my nested object
...
this.formGroup = this.rxFb.formGroup(user);

I would very much like to avoid creating an object for each nested properties of my user model and automatically provide a default value when creating the user object. I see that propObject has a defaultValue option but it doesn't seem to be working. Indeed, when i call

const user = new User();

user.address is null and the formGroup doesn't hold any address formgroup.

const DEFAULT_ADDRESS = new Address('London', 'UK');

export class User {
  @prop({ defaultValue: 'Bob' })
  name: string;

  @prop({ defaultValue: 18 })
  age: number;

  @propObject(Address, { defaultValue: DEFAULT_ADDRESS}) // doesn't seem to be working. When i call new User(), user.address is null
  address: Address;

}

Expected Behavior

Automatically, set a value for a nested property from the value provided in propObject 's defaultValue without the need to manually create an object.

To Reproduce

You can check out the stackblitz link and comment out line. 22 in app.component.ts. You can see that, in this case, user.address doesn't exist and, thus, the formGroup doesn't have a address sub-formGroup.

Url (stackblitz or plunker or other)

https://stackblitz.com/edit/angular-ivy-k2y7d3?file=src/app/app.component.ts

Package Version

@rxwebrxweb/[email protected]

Thanks for your help !

@ajayojha
Copy link
Member

ajayojha commented May 9, 2022

@Tim-mhn The feature is available in 2.1.7 version.

Here are the two ways to set the default value.

The object will be created automatically

@propObject(Address, { autoCreate:true })

you can pass the default value of the object.

@propObject(Address, { defaultValue: new Address(...) })

@ajayojha ajayojha closed this as completed May 9, 2022
@gregoriusus
Copy link

Hi! One more scenario :-)

It is not set, if propObject is inside other propObject.

class Contact{
@propObject(Order, { autoCreate:true })
order:Order;
}
class Order{
@propObject(Address, { autoCreate:true })
address: Address
}

I get error: Cannot access 'Address' before initialization

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

3 participants