- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
下面的代码允许我的客户创建一个具有不同类型字段的自定义表单,然后允许他们的客户填写该表单并将其提交到某个地方。基本上是 Wordpress 的自定义表单生成器。
我已经使用以下代码成功替换了 HTML5
表单验证错误消息的自定义行为。但是,我为不同类型的表单字段设置的自定义消息仅在第二次尝试提交表单时出现。第一次按下提交按钮时,将显示默认消息。
我怎样才能获得我定义的第一次使用的自定义消息?
抱歉,代码很长,这是一个复杂的应用程序。在 PHP
部分,在 Start Input Wrap
部分下查看我设置自定义错误消息的位置,如下所示:
onchange="setCustomValidity(\'\')"oninvalid="this.setCustomValidity(\''.$placeholder.' is required.\')"
PHP/HTML:
function sdForm($atts) {
//Check for a form id variable in the shortcode and, if it's exists, put it into the $formID variable
$shortcodeAtt = shortcode_atts( array(
'id' => false,
), $atts );
if ($shortcodeAtt != false) {
$formID = $shortcodeAtt['id'];
//Setup this form's settings and variables
global $post;
$post = get_post($formID);
setup_postdata($post);
//General Variables
$formTitle = get_the_title();
$formSlug = $post->post_name;
//Form Submit Behavior
$onSubmit = get_field('on_form_submit');
if ($onSubmit == 'default') {
//refresh page, display thank you
} elseif ($onSubmit == 'message') {
//refresh page, display custom message
$message = get_field('custom_submission_message');
} elseif ($onSubmit == 'url') {
//send user to this page
$url = get_field('submission_redirect');
}
//Form Submit To
$actionUrl = get_field('form_action_link');
//Set value of submit button
if (get_field('submit_value')) {
$submitValue = get_field('submit_value');
} else {
$submitValue = 'Submit';
}
//Set ID for form
if (get_field('form_id')) {
$formHtmlId = get_field('form_id');
} else {
$formHtmlId = $formSlug;
}
//Set Classes for form
if (get_field('form_classes')) {
$formClasses = get_field('form_classes');
} else {
$formClasses = '';
}
//Set any messages to display
$messages = '';
$confirmMessage = get_field('custom_submission_message');
if ($_GET['confirm-message']) {
$messages .= $confirmMessage.'<br />';
} elseif ($_GET['confirm-submit']) {
$messages .= 'Thanks for your submission!';
}
//Start the HTML output
$output = '';
//Do some clearing, just in case
$output .= '<div style="clear:both;"></div>';
$output .= '<div class="sdFormWrapper">';
$output .= '<div class="sdFormMessages">';
$output .= $messages;
$output .= '</div>';
$output .= '<form name="'.$formSlug.'" id="'.$formHtmlId.'" class="'.$formClasses.' sd_form" method="post" ';
if (isset($actionUrl)) {
$output .= 'action="'.$actionUrl.'" ';
}
$output .= '>';
//We add a hidden field to identify which form is being processed after submit
$output .= '<input type="hidden" name="formID" value="'.$formID.'" />';
//This loops through each added field and displays each one
if (have_rows('input_type')) {
while (have_rows('input_type')) { the_row();
//We're putting a uniform wrap around each input element for styling
if (get_sub_field('fill_row') == true) {
$fullWidth = 'fullWidth';
} else {
$fullWidth = '';
}
if (get_row_layout() == 'section_header') {
$output .= '<div class="sectionHeader">';
$output .= get_sub_field('header_text');
$output .= '</div>';
} else {
//We turn the field label into a slug with no spaces or special characters
$fieldSlug = sanitize_title(get_sub_field('label'));
//Check if this field is required
if (get_sub_field('required') == true) {
$required = 'required';
} else {
$required = '';
}
//Check for custom name
if (get_sub_field('input_name')) {
$name = get_sub_field('input_name');
} else {
$name = $fieldSlug;
}
//Check for custom html id
if (get_sub_field('input_id')) {
$htmlId = get_sub_field('input_id');
} else {
$htmlId = $name;
}
//Check for custom html classes
if (get_sub_field('input_class')) {
$htmlClass = get_sub_field('input_class').' sdForm-input';
} else {
$htmlClass = 'sdForm-input';
}
//Check for icons
if (get_sub_field('icon')) {
$icon = get_sub_field('icon');
} else {
$icon = '';
}
//Generate Placeholder (this is the field label)
$placeholder = get_sub_field('label');
//Start input wrap
$output .= '<div title="'.$placeholder.'" class="sdForm-inputWrap '.$fullWidth.'">';
//Error message for field
//$output .= '<div class="inputError">'.$placeholder.' is required!</div>';
$output .= $icon;
if (get_row_layout() == 'text') {
$output .= '<input type="text" name="'.$name.'" id="'.$htmlId.'" class="'.$htmlClass.'" placeholder="'.$placeholder.'" title="'.$placeholder.'" '.$required.' onchange="setCustomValidity(\'\')" oninvalid="this.setCustomValidity(\''.$placeholder.' is required.\')" />';
} elseif (get_row_layout() == 'textBox') {
$output .= '<textarea rows="4" id="'.$htmlId.'" class="'.$htmlClass.'" placeholder="'.$placeholder.'" title="'.$placeholder.'" '.$required.' onchange="setCustomValidity(\'\')" oninvalid="this.setCustomValidity(\''.$placeholder.' is required.\')"></textarea>';
} elseif (get_row_layout() == 'email') {
$output .= '<input type="email" name="'.$name.'" id="'.$htmlId.'" class="'.$htmlClass.'" placeholder="'.$placeholder.'" title="'.$placeholder.'" '.$required.' onchange="setCustomValidity(\'\')" oninvalid="this.setCustomValidity(\''.$placeholder.' is required and must be valid.\')"/>';
} elseif (get_row_layout() == 'phone') {
$output .= '<input type="tel" name="'.$name.'" id="'.$htmlId.'" class="'.$htmlClass.'" placeholder="'.$placeholder.'" title="'.$placeholder.'" '.$required.' onchange="setCustomValidity(\'\')" oninvalid="this.setCustomValidity(\''.$placeholder.' is required.\')"/>';
} elseif (get_row_layout() == 'url') {
$output .= '<input type="url" name="'.$name.'" id="'.$htmlId.'" class="'.$htmlClass.'" placeholder="'.$placeholder.'" title="'.$placeholder.'" '.$required.' onchange="setCustomValidity(\'\')" oninvalid="this.setCustomValidity(\''.$placeholder.' is required.\')"/>';
} elseif (get_row_layout() == 'checkboxes') {
if (have_rows('checkbox_selections')) {
if ($placeholder != '') {
$output .= '<label for="'.$htmlId.'">'.$placeholder.'</label><br />';
}
while(have_rows('checkbox_selections')) { the_row();
$selection = get_sub_field('checkbox_selection');
$output .= '<input type="checkbox" name="'.$name.'" value="'.$selection.'" id="'.$htmlId.'" class="'.$htmlClass.'" title="'.$placeholder.'" '.$required.' onchange="setCustomValidity(\'\')" oninvalid="this.setCustomValidity(\''.$placeholder.' is required.\')"/>';
$output .= ' <label for="'.$htmlId.'">'.$selection.'</label><br />';
}
}
} elseif (get_row_layout() == 'radios') {
if (have_rows('radio_selections')) {
if ($placeholder != '') {
$output .= '<label for="'.$htmlId.'">'.$placeholder.'</label><br />';
}
while(have_rows('radio_selections')) { the_row();
$selection = get_sub_field('radio_selection');
$output .= '<input type="radio" name="'.$name.'" value="'.$selection.'" id="'.$htmlId.'" class="'.$htmlClass.'" title="'.$placeholder.'" '.$required.' onchange="setCustomValidity(\'\')" oninvalid="this.setCustomValidity(\''.$placeholder.' is required.\')"/>';
$output .= ' <label for="'.$htmlId.'">'.$selection.'</label><br />';
}
}
} elseif (get_row_layout() == 'select') {
if (get_sub_field('show_label', true)) {
$output .= '<span class="dropdownLabel">'.$placeholder.': </span>';
$showLabel = 'showLabel';
} else {
$showLabel = '';
}
$optionSlug = sanitize_title($selection);
if (have_rows('dropdown_selections')) {
$output .= '<select name="'.$name.'" id="'.$htmlId.'" class="'.$htmlClass.' '.$showLabel.'" title="'.$placeholder.'" '.$required.' onchange="setCustomValidity(\'\')" oninvalid="this.setCustomValidity(\''.$placeholder.' is required.\')">';
$output .= '<option selected="select" disabled>';
if ($showLabel != 'showLabel') {
$output .= $placeholder;
}
$output .= '</option>';
while(have_rows('dropdown_selections')) { the_row();
$selection = get_sub_field('dropdown_selection');
$optionSlug = sanitize_title($selection);
$output .= '<option value="'.$optionSlug.'">'.$selection.'</option>';
}
$output .= '</select>';
}
}
$output .= '<div style="clear:both;"></div>';
$output .= '</div>'; //.sdForm-inputWrap
} //end else (if not a message header)
}//endwhile
}
$output .= '<input type="submit" value="'.$submitValue.'" />';
$output .= '</form><div style="clear: both;"></div></div>';
wp_reset_postdata();
} else { //There is no ID set, so we can't show any form
//ERROR!! No form ID specified
}
echo $output;
}
add_shortcode('sdForm', 'sdForm');
JS:
(function($) {
$(document).ready(function() {
function sd_replaceValidationUI(form) {
// Suppress the default bubbles
form.addEventListener("invalid", function(event) {
event.preventDefault();
}, true);
// Support Safari, iOS Safari, and the Android browser—each of which do not prevent
// form submissions by default
form.addEventListener("submit", function(event) {
if (!this.checkValidity()) {
event.preventDefault();
}
});
var submitButton = form.querySelector("button:not([type=button]), input[type=submit]");
submitButton.addEventListener("click", function(event) {
var invalidFields = form.querySelectorAll(":invalid"),
errorMessages = form.querySelectorAll(".error-message"),
parent;
// Remove any existing messages
for (var i = 0; i < errorMessages.length; i++) {
errorMessages[i].parentNode.removeChild(errorMessages[i]);
}
for (var i = 0; i < invalidFields.length; i++) {
parent = invalidFields[i].parentNode;
parent.insertAdjacentHTML("beforeend", "<div class='error-message'>" +
invalidFields[i].validationMessage +
"</div>");
}
// If there are errors, give focus to the first invalid field
if (invalidFields.length > 0) {
invalidFields[0].focus();
}
});
}
// Replace the validation UI for all forms
var forms = document.querySelectorAll("form");
for (var i = 0; i < forms.length; i++) {
sd_replaceValidationUI(forms[i]);
}
//Changes the text of the dropdown to #666 when a selection is made
$('.sdForm-inputWrap select').change(function() {
$(this).css('color', '#666');
});
})
})(jQuery);
最佳答案
我认为您的问题是您正在设置自定义有效性 oninvalid
.这在默认 UI 上有效,但在您想要对其进行自定义时无效。
我尝试重新创建您的代码。第一次运行 invalidFields
循环正在获取默认的无效错误消息。在此之后 setCustomValidity
然后调用,因此您会在第二次运行时收到自定义错误消息。
要解决此问题,您必须遍历所有要设置自定义有效性的字段并调用 setCustomValidity
在提交表单之前。
一种方法是使用 HTML
中的数据属性代码。像这样为每个字段添加属性:
<input type="text" data-ErrorMessage="Your Custom Error Message" />
然后将这个添加到现有的js中:
for (var i=0; i<fields.length; i++){
var message = $(fields[i]).attr("data-ErrorMessage");
$(fields[i]).get(0).setCustomValidity(message);
}
关于jquery - 自定义 HTML5 表单验证错误仅在首次尝试后发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34259669/
我在 mongodb 中的玩家和锦标赛之间存在多对多关系。 我希望能够一次将许多玩家添加到锦标赛中。如果没有 ajax,这很简单,但我们有一个包含数千名玩家的数据库,因此表单选择变得巨大。 我们想为此
这个问题已经有答案了: When should I use html's and when spring's in Spring MVC web app? (3 个回答) 已关闭 6 年前。 我正
我正在 C++ Builder XE4 上使用 VCL。 我有以下组件。 FormMain 具有 TButton *B_select; FormSelect(或DialogSelect)具有 TCom
如何在不影响表单控件的情况下更改表单的 alphablend? 德尔福XE7 最佳答案 此问题的一个解决方案是使用多设备应用程序(如果无法使用VCL)。 如果您需要保留透明的TForm,只需更改属性T
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我正在尝试扩展 Django 注册以包含我自己的注册表单。原则上这是相当简单的。我只需要编写自己的表单( CustomRegistrationForm ),它是原始表单( RegistrationFo
我正在尝试为我的网站实现聊天功能。为了做到这一点,我遵循了以下教程:https://channels.readthedocs.io/en/latest/tutorial/ 然后我稍微更改了代码以实现它
有一个问题,我需要用一个 html 表单提交两个相互关联的模型表单。我知道如何提交两个单独的表格,但是在相关模型表格的情况下外键让我发疯。 问题是,第二个表单应该用外键填充字段到第一个表单的实例。 在
我正在创建一个工具,允许某人输入食谱,然后将其保存为 XML 文件,我已经创建了 XSD,但我想知道如何在我的网页上制作一个表单以允许用户输入他们的食谱并遵守模式。我一直在研究 Ajax 和 Jque
在 .net win 表单(如 asp.net web 表单)中是否有可用的验证控件? 因为很难为我的每个控件设置正确的条件,所以我的表单中也有很多重复的代码。 正确的做法是什么? 最佳答案 看看这个
我有一个简短的问题。我正在学习如何使用 javascript 制作注册表,发现此链接非常有用。 http://www.w3resource.com/javascript/form/javascript
我正在开发一个项目,该项目将使用循环将许多表单添加到 mysql 数据库中。在 javascript 部分中,我无法让 var i 在函数 updatesum() 中工作。有人可以帮我吗? 我试图避免
在我的应用程序上有一个包含 2 个字段和一个保存按钮的表单。 在我的 onClick 结束时我需要什么来将光标返回到第一个字段。 我有这个来清除它们 txtData.setText("
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
<input type="text" name="textfield" onKeyPress="javascript:alert(event.
我正在构建的网站有一个登录表单,作为所有其他模板扩展的 base.html 模板的一部分;因此,我需要以某种方式处理每个页面上的登录/注销逻辑。 目前每个页面都在单独的 View 中加载,那么实现它的
我有一个表单类,看起来像.. #forms.py class ExampleForm(forms.Form): color = forms.CharField(max_length=25)
有没有办法在表单定义中给表单一个特殊的错误渲染函数?在 customizing-the-error-list-format 下的文档中它展示了如何为表单提供特殊的错误呈现函数,但似乎您必须在实例化表单
我正在处理由多个页面组成的表单,我想解决验证问题。 当我点击提交按钮时,当前页面上的所有字段都会在下方显示错误消息,但是如果我更改页面,那么我需要再次点击提交,因为这些字段未设置为已触摸。 如果我可以
是否可以附加到继承表单的 exclude 或 widgets 变量? 到目前为止,我有以下设置。 class AddPropertyForm(forms.ModelForm): num_mon
我是一名优秀的程序员,十分优秀!