我有一个表单验证,到目前为止,如果输入区域中存在两个定义的词组中的任何一个,就会返回一个错误信息。
add_filter('gform_validation_3', 'custom_validation');
function custom_validation($validation_result){
$form = $validation_result["form"];
foreach($form['fields'] as &$field){
/* Check that the value of the field that was submitted. e.g. the name="input_1" that is generated by Gravity Forms */
if($_POST['input_4'] == "Your First Name" || "SEO"){
// set the form validation to false
$validation_result["is_valid"] = false;
//The field ID can be found by hovering over the field in the backend of WordPress
if($field["id"] == "4"){
$field["failed_validation"] = true;
$field["validation_message"] = "This field needs to be your actual first name.";
}
}
}
//Assign modified $form object back to the validation result
$validation_result["form"] = $form;
return $validation_result;
}
我现在不知道如何创建一个数组来定义不允许的词,这样我就可以有一个更长的列表?
解决方案:
首先,第一个 “如果 “不对,我想你的意思是。
if($_POST['input_4'] == "Your First Name" || $_POST['input_4'] =="SEO")
一个很好的方法来实现你想要的东西。
$forbidden_words = ["Your First Name", "SEO"];
$is_valid = !in_array($_POST['input_4'], $forbidden_words); //false if the word is in array
之后你就可以走了。
if($is_valid)
//do magic
本文来自投稿,不代表运维实战侠立场,如若转载,请注明出处:https://www.shizhanxia.com/870.html