绕过输入的命名约定?

时间:2022-07-04 16:56:50

I have a set of inputs that I'm using jQuery validate on, and they all base their 'error-logs' off of their names.

我有一组输入,我正在使用jQuery验证,他们都将他们的'错误日志'基于他们的名字。

This wouldn't be a problem, except most of the names are set to fit with our DB so they can be uploaded on submit.

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

For example, I have these naming rules right now, which work great for 'first name' and 'email'

例如,我现在有这些命名规则,适用于“名字”和“电子邮件”

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

But whenever I have a name like this input:

但只要我有这样的名字输入:

<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="">

It gets a little tricky because it won't register submitted[constituent_base][total_constituents] total_number_of_constituents_in_your_database as a name and let me change the error.

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

My only two thoughts were to add a name="" onto the input, which doesn't work, and then somehow trying to figure out how to call to it by an id or some sort. Has anyone accomplished this?

我只有两个想法是在输入上添加一个名称=“”,这不起作用,然后以某种方式试图找出如何通过id或某种类型调用它。有没有人完成这个?

1 个解决方案

#1


2  

You can put quotes around property names in Javascript object literals when the name is not a valid identifier:

当名称不是有效标识符时,您可以在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"
}

Note that jquery-validate automatically detects the required attribute in the <input> element, so you can leave that out of the rules and just use this for the messages.

请注意,jquery-validate会自动检测元素中的必需属性,因此您可以将其保留在规则之外,并将其用于消息。

#1


2  

You can put quotes around property names in Javascript object literals when the name is not a valid identifier:

当名称不是有效标识符时,您可以在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"
}

Note that jquery-validate automatically detects the required attribute in the <input> element, so you can leave that out of the rules and just use this for the messages.

请注意,jquery-validate会自动检测元素中的必需属性,因此您可以将其保留在规则之外,并将其用于消息。