json-schema-form / angular-schema-form

Generate forms from a JSON schema, with AngularJS!
https://json-schema-form.github.io/angular-schema-form
MIT License
2.47k stars 653 forks source link

Question about extending and scopes #599

Closed tomsowerby closed 8 years ago

tomsowerby commented 8 years ago

Hello, I'm seeking clarity please.

So we extend schema-form in a simple way:

angular.module('schemaFormExtender', ['schemaForm']).config(function(schemaFormDecoratorsProvider, sfBuilderProvider) {

    schemaFormDecoratorsProvider.defineAddOn(
        'bootstrapDecorator',         // Name of the decorator you want to add to.
        'newfield',                    // Form type that should render this add-on
        'newfield.html',    // Template name in $templateCache
        sfBuilderProvider.stdBuilders // List of builder functions to apply.
    );
});

Then we have our newfield.html template:

<div class="form-group schema-form-{{form.type}} {{form.htmlClass}}">
    <span><label>{{form.title}}</label></span>
    <input sf-field-model ng-model="$$value$$" schema-validate="form" type="hidden">
    {{customScopeVariable}}
</div>

So, question: Is there a way to inject or have the template inherit our customScopeVariable without us referencing it using x amount of $parent. references to get to it?

Consider fields "Billing address line one" and "Shipping address line one", using the same schema containing "title":"address line one" and a Billing/Shipping prefix variable being passed from the scope. This is a basic example that probably doesn't require custom fields, but hopefully it gives you an idea of what I'm trying to achieve.

Anthropic commented 8 years ago

@tomsowerby I think I have had the same issue myself, this is something worth looking at as we move forward and in the planning gitter but for now I have solved this by adding additional form keys that aren't in my model and updating those in a decorator I use just for additional info/hints which evaluates on the top level scope and watches for changes.

It is hacky but it worked for me.

But re-reading your question in the example you could re-use the schema for each but have a different title in the form for each, I do that a lot too.

Anthropic commented 8 years ago

@tomsowerby not sure why the documentation changed, but take a look at this older version of it for what is on the scope https://github.com/json-schema-form/angular-schema-form/blob/1345067fdfabf23507db16542465572b1509f4d8/docs/extending.md

I suspect evalExpr is what you are after, sorry I didn't know it was removed i will check why, but I think it is still available.

Please re-open if you do not feel the question has been answered sufficiently.

BioPhoton commented 8 years ago

For me this is not clearly answered. I want to a create a custom date input out of 3 different inputs. If any of these inputs change i try to set the value to the original (hidden) date input.

I just need to add a custom function for ng-change on the 3 inputs to update the date input. I have not found any solution yet and the above explanations are not really helpful for me.

Could anybody give me a hint how to achieve this?

BioPhoton commented 8 years ago

Ended up with adding the function to the item (form).

One problem is left. The valid state is rendered correct. The invalid state is just black instead of red.

Any idea what i am missing?

Anthropic commented 8 years ago

@BioPhoton your issue is not related to the current issue directly I suggest making a new issue to work through your problem specifically. Include a gist to help with examining the issue also please.