将事件处理程序添加到文本框apostrophecms

发布于 2025-02-03 10:32:18 字数 168 浏览 5 评论 0原文

我是撇号世界的新手,我正在尝试将事件添加到文本框中,例如,当他们键入一个国家名称时,我想检查数据库以查看这个国家是否已经存在,我会使用此过程。我创建的一个小部件是为每个位置添加国家和特定办公室,因此我想在用户类型键入信息时拥有更多控制信息,例如让电话字段仅接受数字而不是字母等。 我希望我让自己清晰大声笑 非常感谢您的帮助

I'm new at the apostrophe world, I'm trying to add a event to a textbox, for example, when they type a country name I would like to check the database to see if this country already exists, I do this process with a widget that I create to add countries and specific offices for each location, so I would like to have more control when user's type type the information, like for example make phone fields accept only numbers instead of letters, etc.
I hope I made myself clear lol
Thank you so much for the help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

错々过的事 2025-02-10 10:32:18

我是Apostrophecms团队的一员。

Apostrophecms当前不支持其标准字符串和TextArea字段的自定义前端和后端验证。但是,它确实支持自定义模式字段类型。这是一条有据可查的途径,可以完全使用您想要的行为来创建自己的字段类型。所以我建议这种方法。我链接到此处的文档提供了一个完整的示例。

I'm part of the ApostropheCMS team.

ApostropheCMS does not currently support custom front-end and back-end validation of its standard string and textarea fields. However it does support custom schema field types. This is a well-documented path to creating your own field types with exactly the behavior you want. So I would recommend that approach. The documentation I've linked to here provides a complete example.

瞳孔里扚悲伤 2025-02-10 10:32:18

我确实设法创建了一个新字段,我叫Isunique:

    self.addFieldType({
  name: 'isUnique',
  converters: {
    string: function(req, data, name, object, field, callback) {
     
      object[name] = self.apos.launder.string(data[name]);

      /* Checking the object _edit. Mode */
      switch(object._edit){

        case true:

          

          /* Checking if the Name of the Country Name has Changed */
          if( object[field.options[0].fieldToCompareMode].toLowerCase().trim() !== data[field.options[0].fieldToCheck].toLowerCase().trim() ){
           
          return callback('required');
      //       /* Checking if the Content Already exists.  */
      //       (async () => {
      //         await self.apos.docs.db.findOne({
      //           title: data[field.options[0].fieldToCheck] 
      //         })
      //         .then((response, reject) => {
      //           console.log('Data ' + response);
      //           if(response === null){
      //             console.log('achou o erro')
      //             /* The Content Already exists, throwing an error*/
                 
      //             return setImmediate(callback(null));
      //           }
      //         })
      //         .catch(callback)
      //       })();

            
          };

          
      //     console.log('Edit Mode........');
      //   break;

      //   case undefined:
      //     console.log('Undefined .............');
      //     break;
      }

      return setImmediate(callback);
    },

    form: 'string'
  },
  exporters: {
    string: function(req, object, field, name, output, callback) {
      // no formatting, set the field
      output[name] = object[name];
      return setImmediate(callback);
    }
  },

  index: function(value, field, texts) {
    var silent = (field.silent === undefined) ? true : field.silent;
    texts.push({ weight: field.weight || 15, text: value, silent: silent });
  },
  isEmpty: function(field, value) {
    return !value.length;
  },

  addFilter: function(field, cursor) {
    cursor.addFilter(field.name, {
      finalize: function() {
        if (self.cursorFilterInterested(cursor, field.name)) {
          var criteria = {};
          criteria[field.name] = new RegExp(self.apos.utils.regExpQuote(cursor.get(field.name)), 'i');
          cursor.and(criteria);
        }
      },
      safeFor: 'manage',
      launder: function(s) {
        return self.apos.launder.string(s);
      },
      choices: function(callback) {
        return self.sortedDistinct(field.name, cursor, callback);
      },
    });
  },

  // validate: function(field, options, warn, fail, schema) {
  //   if (!field.choices) {
  //     // optional for booleans
  //     return;
  //   }
  //   if (!Array.isArray(field.choices)) {
  //     warn('If present, field.choices must be an array');
  //     return;
  //   }
  //   _.each(field.choices, function(choice) {
  //     _.each(choice.showFields || [], function(name) {
  //       if (!_.find(schema, { name: name })) {
  //         warn('showFields includes ' + name + ', a field that does not exist in the schema');
  //       }
  //     });
  //   });
  // }
});

当我尝试调用异步时,我无法设法创建一个回调,例如,此字段已经存在。

I did manage to create a new field, I called isUnique:

    self.addFieldType({
  name: 'isUnique',
  converters: {
    string: function(req, data, name, object, field, callback) {
     
      object[name] = self.apos.launder.string(data[name]);

      /* Checking the object _edit. Mode */
      switch(object._edit){

        case true:

          

          /* Checking if the Name of the Country Name has Changed */
          if( object[field.options[0].fieldToCompareMode].toLowerCase().trim() !== data[field.options[0].fieldToCheck].toLowerCase().trim() ){
           
          return callback('required');
      //       /* Checking if the Content Already exists.  */
      //       (async () => {
      //         await self.apos.docs.db.findOne({
      //           title: data[field.options[0].fieldToCheck] 
      //         })
      //         .then((response, reject) => {
      //           console.log('Data ' + response);
      //           if(response === null){
      //             console.log('achou o erro')
      //             /* The Content Already exists, throwing an error*/
                 
      //             return setImmediate(callback(null));
      //           }
      //         })
      //         .catch(callback)
      //       })();

            
          };

          
      //     console.log('Edit Mode........');
      //   break;

      //   case undefined:
      //     console.log('Undefined .............');
      //     break;
      }

      return setImmediate(callback);
    },

    form: 'string'
  },
  exporters: {
    string: function(req, object, field, name, output, callback) {
      // no formatting, set the field
      output[name] = object[name];
      return setImmediate(callback);
    }
  },

  index: function(value, field, texts) {
    var silent = (field.silent === undefined) ? true : field.silent;
    texts.push({ weight: field.weight || 15, text: value, silent: silent });
  },
  isEmpty: function(field, value) {
    return !value.length;
  },

  addFilter: function(field, cursor) {
    cursor.addFilter(field.name, {
      finalize: function() {
        if (self.cursorFilterInterested(cursor, field.name)) {
          var criteria = {};
          criteria[field.name] = new RegExp(self.apos.utils.regExpQuote(cursor.get(field.name)), 'i');
          cursor.and(criteria);
        }
      },
      safeFor: 'manage',
      launder: function(s) {
        return self.apos.launder.string(s);
      },
      choices: function(callback) {
        return self.sortedDistinct(field.name, cursor, callback);
      },
    });
  },

  // validate: function(field, options, warn, fail, schema) {
  //   if (!field.choices) {
  //     // optional for booleans
  //     return;
  //   }
  //   if (!Array.isArray(field.choices)) {
  //     warn('If present, field.choices must be an array');
  //     return;
  //   }
  //   _.each(field.choices, function(choice) {
  //     _.each(choice.showFields || [], function(name) {
  //       if (!_.find(schema, { name: name })) {
  //         warn('showFields includes ' + name + ', a field that does not exist in the schema');
  //       }
  //     });
  //   });
  // }
});

When I trying to call the async I can't manage to create a callback, for example This field Already exists.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文