问题描述
我正在尝试为 recaptcha
制定验证规则。我找到了一个有效的规则示例。
我已将其添加到验证器:
g-recaptcha-response' => 'required|captcha
并且此验证规则总是会抛出一条消息:“g-recaptcha-response 字段是必需的。”无论是否选择了验证码表单。
这是我的代码:
a function in default.htm layout
function onSend()
{
/*if(empty(post('name')) ||
empty(post('email')) ||
empty(post('message')) ||
empty(post('g-recaptcha-response')) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
http_response_code(400);
echo "errors";
return false;
}*/
$name = post('name');
$email = post('email');
$msg = post('message');
$response = post('g-recaptcha-response');
$remoteip = $_SERVER['REMOTE_ADDR'];
$secret = "XXX";
$url = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$response."&remoteip=".$remoteip);
$response = json_decode($url,true);
$validation = Validator::make(
[
'name' => $name,'email' => $email,'msg' => $msg
],[
'name' => 'required','email' => 'required|email','msg' => 'required','g-recaptcha-response' => 'required|captcha',//'g-recaptcha-response' => 'required',]
);
if ($validation->fails())
{
throw new ValidationException($validation);
}
Flash::success('Sent!');
if (isset($response['success'])&&$response['success']=="true"){
$to = System\Models\MailSetting::get('sender_email');
$params = compact('name','email','msg');
Mail::sendTo($to,'martin.forms::mail.contactform',$params);
//Flash::success(Message::trans('Form submitted!'));
return true;
}
else {
return false;
}
}
还有一个表单本身:
<form class="form-control1" data-request="onSend" data-request-validate data-request-flash>
{{ form_token() }}
<div class="row">
<div class="col-12">
<div class="alert alert-success contact__msg" style="display: none" role="alert" id="{{ __SELF__ }}_forms_flash"></div>
</div>
</div>
<div class="form-group">
<label for="name">Name:</label>
<input name="name" type="text" class="form-control">
<span data-validate-for="name"></span>
</div>
<div class="form-group">
<label for="email">E mail</label>
<input name="email" type="email" class="form-control">
<span data-validate-for="email"></span>
</div>
<div class="form-group-2 mb-4">
<textarea name="message" class="form-control" rows="4" cols="30" rows="10"></textarea>
<span data-validate-for="message"></span>
</div>
<div>
<button id="simpleContactSubmitButton" data-attach-loading class="btn btn-main float-left" name="submit" type="submit">Send</button>
</div>
<div class="form-group">
{% partial 'genericForm/recaptcha' %}
</div>
<div class="alert alert-danger" data-validate-error>
<p data-message></p>
</div>
</form>
附言如果有人能向我指出一个很好的例子,其中包含 Magic Forms 的 AJAX 验证教程作为进一步改进
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)