绕过输入的命名约定?

Getting around naming conventions on inputs?

我有一组我正在使用 jQuery 验证的输入,它们都基于它们的 'error-logs' 名称。

这不是问题,除了大多数名称都设置为适合我们的数据库,因此可以在提交时上传。

例如,我现在有这些命名规则,非常适合 'first name' 和 'email'

rules: {    
    firstname: {
        required: true,
    },
    email: {
        required: true,
    }
},
messages: {
    firstname: "Please enter your name",
    email: "A valid email is required",
}

但是每当我有这样的名字时输入:

<input id="edit-submitted-constituent-base-total-constituents" class="form-text hs-input" name="submitted[constituent_base][total_constituents] total_number_of_constituents_in_your_database" required="required" size="60" maxlength="128" type="number" value="" placeholder="">

有点棘手,因为它不会将 submitted[constituent_base][total_constituents] total_number_of_constituents_in_your_database 注册为名称,让我更改错误。

我唯一的两个想法是在输入上添加一个 name="",但这是行不通的,然后想办法弄清楚如何通过 id 或某种方式调用它.有人做到了吗?

当名称不是有效标识符时,您可以在 Javascript 对象文字中将 属性 名称用引号引起来:

rules: {
    "submitted[constituent_base][total_constituents] total_number_of_constituents_in_your_database": {
        required: true
    }
},
messages: {
    "submitted[constituent_base][total_constituents] total_number_of_constituents_in_your_database": "You have to fill in this field"
}

请注意,jquery-validate 会自动检测 <input> 元素中的 required 属性,因此您可以将其排除在规则之外,仅将其用于消息。