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

Adding a default value to selects #216

Closed
stramel opened this issue Jan 15, 2015 · 3 comments
Closed

Adding a default value to selects #216

stramel opened this issue Jan 15, 2015 · 3 comments

Comments

@stramel
Copy link
Contributor

stramel commented Jan 15, 2015

I am creating a month select and need a default value of 'MONTH' which should be invalid. I have tried setting the model to a value that is declared in the titleMap but not the enum. Validation works as expected but I can't get a default value to show in the select. Is there something I am missing?

// Schema
scope.schema = {
  type: 'object',
  title: 'Payment Information',
  required: [
    'expirationDate'
  ],
  properties: {
    expirationDate: {
      title: 'Expiration Date',
      type: 'object',
      properties: {
        month: {
          title: 'Month',
          type: 'string',
          enum: monthsOfYear // Array of valid Months
        },
        year: {
          title: 'Year',
          type: 'string',
          enum: getValidCcYears() // Array of valid Years
        }
      }
    }
// Form
scope.form = [
  {
    key: 'expirationDate',
    notitle: true,
    items: [
      {
        key: 'expirationDate.month',
        type: 'select',
        titleMap: createTitleMap('MONTH', monthsOfYear), // Prepends 'MONTH'
        validationMessage: expirationMonthValidationMessages
      }, {
        key: 'expirationDate.year',
        type: 'select',
        titleMap: createTitleMap('YEAR', getValidCcYears()), // Prepends 'YEAR'
        validationMessage: expirationYearValidaitonMessages
      }
    ]
  }
// Model
scope.model = {
  expirationDate: {
    month: '?', // '?' is the key associated to the defaults
    year: '?' // '?' is the key associated to the defaults
  }
};
@stramel
Copy link
Contributor Author

stramel commented Feb 20, 2015

bump?

@urpro-bas
Copy link

I have tried your code, I had to make some adjustments to get the test running but it seems to work. Maybe you createTitleMap function works differently?
Here is the code that works for me:

var monthsOfYear = ["Jan", "Feb"];
var years = ['2003', '2004'];
$scope.schema = {
    type: 'object',
    title: 'Payment Information',
    required: [
      'expirationDate'
    ],
    properties: {
      expirationDate: {
        title: 'Expiration Date',
        type: 'object',
        properties: {
          month: {
            title: 'Month',
            type: 'string',
            enum: monthsOfYear // Array of valid Months
          },
          year: {
            title: 'Year',
            type: 'string',
            enum: years // Array of valid Years
          }
        }
      }
    }
};
var createTitleMap = function(x, arr)
{
    var result = [];
    result.push({name: x, value:"?"});
    for(var i = 0; i < arr.length; i++)
        result.push({name: arr[i], value:arr[i]});
    return result;
};
$scope.form = [
{
  key: 'expirationDate',
  notitle: true,
  items: [
    {
      key: 'expirationDate.month',
      type: 'select',
      titleMap: createTitleMap('MONTH', monthsOfYear), // Prepends 'MONTH'
    }, {
      key: 'expirationDate.year',
      type: 'select',
      titleMap: createTitleMap('YEAR', years), // Prepends 'YEAR'
    }
  ]
}
];
$scope.model = {
    expirationDate: {
        month: '?', // '?' is the key associated to the defaults
        year: '?' // '?' is the key associated to the defaults
    }
};

@stramel
Copy link
Contributor Author

stramel commented May 20, 2015

I'm going to close this issue. I feel like it is probably user error lol

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