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

Don't modify form or schema #407

Open
davidlgj opened this issue May 29, 2015 · 9 comments
Open

Don't modify form or schema #407

davidlgj opened this issue May 29, 2015 · 9 comments

Comments

@davidlgj
Copy link
Contributor

Related to #395

Incoming form definition objects are reused and in case of some defaults the schema might be changed as well. We should make a copy to use internally.

@Anthropic
Copy link
Member

What would you do if the user updates theirs, re-process? What if they have a decorator that makes changes? Just thinking out loud...

@davidlgj
Copy link
Contributor Author

Currently we re-process when a ordinary watch changes it, i.e. the schema object instance or form array is changed (ordinary ===). Or if you issue a schemaFormRedraw event. I don't want to change that, just that the scope.form used in the isolated scope is a deep copy of outside form as well as the schema so that the "original" form definition and schema isn't changed by schema form.

This probably needs to be optional, since in certain cases you can change part of your form, ex. description and it will update without needing to re-render the form (I never designed it to be able to do that, but angular 1.2 support forced me to not use ::, so now people are doing it I guess it's best not to break it). Basically it's hard supporting both being able to swap out the schema and modification to the form after rendering.

@rayhooker
Copy link

Perhaps this is a related issue. I have created a form editor. I noticed an odd behavior. I store the form def and form schema in a subkey of a larger JSON. If I specify the form and schema, for some reason any form element keys are changed from say key: "description" to key: ["description"]. So I thought "okay let's clone it" and use JQuery's extend to create a deep copy. Unfortunately only the initial extend causes the form display to update. Subsequent changes do not cause the form to be redrawn. As a result added or changed fields are not reflected in the "preview" where I display a hidden div to render the form being created. Any suggestions or clues?

@davidlgj
Copy link
Contributor Author

@rayhooker Yes this is the exact same problem. Schema Form messes with the form definition internally, massaging it in to a "canonical" form. Redraws of form is only done if the instance of the object changes, not just a property. See https://github.com/Textalk/angular-schema-form/blob/development/docs/index.md#updating-form

So the solution for you is to issue a scope.$broadcast('schemaFormRedraw') after a change to propagate it to the preview.

@tuvokki
Copy link

tuvokki commented Sep 21, 2015

I noticed that this also affects the way the 'key'-value gets stored. I had a couple of keys containing '.' (dots).

  "model": {
    "Field.SystemSettingExchange.ExchangeEnabled": true,
    //etc... 

I add these exact same keys to the form like this:

$scope.form.push({
  'key': fieldEntry.key,
  'type': fieldEntry.type
});

and they come out in a array, which breaks the coupling with the model, among other things:

  "form": [
    {
      "key": [
        "Field",
        "SystemSettingExchange",
        "ExchangeEnabled"
      ]
    },
   //etc...

@davidlgj
Copy link
Contributor Author

@tuvokki yes that id the internal format. The wok being done on PR #505 means that we really need to make a copy of schema and form. So it will probably be fixed soon

@tuvokki
Copy link

tuvokki commented Sep 21, 2015

That's great to hear. I've changed my backend to remove the dots so I can test my use-case easily if this is done. Let me know if I can help out.

@tjones0808
Copy link

Was this fixed? I am having the same problem. I am building dynamic forms and I need my form objects left unchanged so I can reproduce forms at a later date. Schema Form keeps changing my keys to arrays whenever I render.

@Anthropic
Copy link
Member

Anthropic commented Oct 4, 2016

@tjones0808 I just had a quick look and I would expect if you search in the compiled file for initialForm and find the line:
var form = scope.initialForm || [ '*' ];
And replace it with
var form = scope.initialForm ? angular.copy(scope.initialForm) : [ '*' ];
it was like that in another line so should have the desired effect.

I will look at adding it to the next release. Please let me know if that was all that was required!

To be honest it seems too easy, I suspect I am missing something.

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