{enumOptions.errors.map((err, idx) =>
{err}
)}
);
+ } else {
+ state.setIn(['enumOptions', 'error'], null);
+
+ if (defaultValue !== '' && !(defaultValue in enumOptions.options)) {
+ state.setIn(['default_value', 'error'], t('Default value is not one of the allowed options'));
+ }
+ }
+ } else {
+ state.setIn(['enumOptions', 'error'], null);
+ }
+ }
+
+ parseEnumOptions(text) {
+ const t = this.props.t;
+ const errors = [];
+ const options = {};
+
+ const lines = text.split('\n');
+ for (let lineIdx = 0; lineIdx < lines.length; lineIdx++) {
+ const line = lines[lineIdx].trim();
+
+ if (line != '') {
+ const matches = line.match(/^([^|]*)[|](.*)$/);
+ if (matches) {
+ const key = matches[1].trim();
+ const label = matches[2].trim();
+ options[key] = label;
+ } else {
+ errors.push(t('Errror on line {{ line }}', { line: lineIdx + 1}));
+ }
+ }
+ }
+
+ if (errors.length) {
+ return {
+ errors
+ };
+ } else {
+ return {
+ options
+ };
+ }
+ }
+
+ renderEnumOptions(options) {
+ return Object.keys(options).map(key => `${key}|${options[key]}`).join('\n');
+ }
+
+
+ async submitHandler() {
+ const t = this.props.t;
+
+ let sendMethod, url;
+ if (this.props.entity) {
+ sendMethod = FormSendMethod.PUT;
+ url = `/rest/fields/${this.props.list.id}/${this.props.entity.id}`
+ } else {
+ sendMethod = FormSendMethod.POST;
+ url = `/rest/fields/${this.props.list.id}`
+ }
+
+ try {
+ this.disableForm();
+ this.setFormStatusMessage('info', t('Saving field ...'));
+
+ const submitSuccessful = await this.validateAndSendFormValuesToURL(sendMethod, url, data => {
+ if (data.default_value.trim() === '') {
+ data.default_value = null;
+ }
+
+ if (data.type !== 'option') {
+ data.group = null;
+ }
+
+ data.settings = {};
+ switch (data.type) {
+ case 'checkbox':
+ case 'radio-grouped':
+ case 'dropdown-grouped':
+ case 'json':
+ data.settings.renderTemplate = data.renderTemplate;
+ break;
+
+ case 'radio-enum':
+ case 'dropdown-enum':
+ data.settings.enumOptions = this.parseEnumOptions(data.enumOptions).options;
+ data.settings.renderTemplate = data.renderTemplate;
+ break;
+
+ case 'date':
+ case 'birthday':
+ data.settings.dateFormat = data.dateFormat;
+ break;
+ }
+
+ delete data.renderTemplate;
+ delete data.enumOptions;
+ delete data.dateFormat;
+
+ if (data.type === 'option') {
+ data.orderListBefore = data.orderSubscribeBefore = data.orderManageBefore = 'none';
+ } else {
+ data.orderListBefore = Number.parseInt(data.orderListBefore) || data.orderListBefore;
+ data.orderSubscribeBefore = Number.parseInt(data.orderSubscribeBefore) || data.orderSubscribeBefore;
+ data.orderManageBefore = Number.parseInt(data.orderManageBefore) || data.orderManageBefore;
+ }
+ });
+
+ if (submitSuccessful) {
+ this.navigateToWithFlashMessage(`/lists/${this.props.list.id}/fields`, 'success', t('Field saved'));
+ } else {
+ this.enableForm();
+ this.setFormStatusMessage('warning', t('There are errors in the form. Please fix them and submit again.'));
+ }
+ } catch (error) {
+ if (error instanceof interoperableErrors.DependencyNotFoundError) {
+ this.setFormStatusMessage('danger',
+