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

Include value even if not in enum so legacy values not overidden #36

Closed
Anthropic opened this issue Jan 23, 2013 · 3 comments
Closed

Include value even if not in enum so legacy values not overidden #36

Anthropic opened this issue Jan 23, 2013 · 3 comments
Labels

Comments

@Anthropic
Copy link

Can I suggest that there be an alteration to allow forcing a value into an enum for display perhaps with a format like the following to allow styling to illustrrate its origin:

<option value="{{value}}" class="forced-value">{{value}}</option>

So if form has legacyClass: "{{class name}}" as below:

{
    schema: {
        enemies: {
            title: "Scariest Enemy!",
            enum: ["dragons","pirates"],
        }
    },
    form: [
        {key: "enemies", legacyClass: "forced-value"}
    ],
    value: {
        enemies: "ninjas"
    }
}

And it is passed value: {enemies:"ninja"} which isn't in the enum as in the example above, then it could be added to the enum list and set as selected with output along the lines of:

<option value="ninjas" class="forced-value" selected>ninjas</option>

Where I work we are absolutely not allowed to overwrite legacy information without good reason. So I need to make sure it is included in the drop down and flagged as "historic", but I can add that label in css if the value was there with an appropriate css style.

@tidoust
Copy link
Contributor

tidoust commented Jan 23, 2013

Hi @Anthropic,

In your proposal, what happens when the form gets submitted with what you call a legacy value?

  1. Should the schema be automatically adjusted to accept the incorrect value?
  2. Should the submission trigger an error because the schema is not respected?

Option 1. does not sound quite right. What good is a schema if the code can decide to change it on its own? If you expect the unexpected values, the best would be to define the field as a regular string in the schema and to define the base enumeration in the form part. I do not know whether the enumeration is going to work as-is if it is defined that way though (I haven't checked). The good part is that doing so lets us move from option 1 to option 2, as the schema does not need to be automatically adjusted anymore.

Option 2. can be implemented with a custom field, I think. In other words, there should not need to be any change to the core of the JSON Form library. Just take the code of the select field type as basis to create a new extensibleselect (or some better name) that would create the appropriate markup for you.

One thing I would do differently is that I would not use a property legacyClass but rather a allowUndefinedValues flag or similar. The generated markup would still mark the unknown values with a specific class undefined-value for styling purpose. You would lose the ability to specify a custom class but would be more explicit about what you're trying to achieve.

I am not really sure this possibility needs to be supported by JSON Form. It sounds rather specific. Do others have similar needs?

@Anthropic
Copy link
Author

@tidoust ok I think you gave me an easier way (perhaps) to get the solution I am after!

If the schema was a plain string with no enum, then if I could define the enum at the form level using the already included titleMap field instead, it could allow for additions. In that way it could double as a suggestion list rather than a hard list were it set to text in the form definition perhaps.

I can imagine uses for a suggestion list on a form field beyond my current use case.

{
    schema: {
        enemies: {
            title: "Scariest Enemy!",
        }
    },
    form: [
        {
            key: "enemies",
            type: "select",
            titleMap: {
                dragon: "The Dragon",
                pirate: "Jack Sparrow",
                alien: "ALF"
            },
            // template for undefined value label
            undefined: "[legacy] {{value}}"
        }
    ],
    value: {
        enemies: "ninja"
    }
}

At the moment when I do the above I get a select box with nothing in it, if it could add the titleMaps values as options when no enum is defined, then my problem would be solved. :)

Obviously I could just add the option to the schema or titleMaps prior to initialising, but that doesn't feel right.

@tchapi tchapi added the Legacy label Jan 8, 2019
@tchapi
Copy link
Member

tchapi commented Jun 19, 2019

Closing as there is no activity on this issue. Please reopen a new one if needed.

@tchapi tchapi closed this as completed Jun 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants