Open schel4ok opened 2 years ago
Hi,
I can see you are calling the reset function. But I can not see, how you update your UI with a new captcha.
I guess your "modalform" json response needs to include the new captcha and your javascript needs to replace the input html element.
One idea would be, not to call "->reset()" in order to keep your existing captcha valid as long as the user has the form open. (In app/Http/Controllers/MainController.php)
I don't see this as a problem with this package, more with your approach to UI and JavaScript.
I managed to change captcha after successfull POST request and after click on refresh button like that
blade view
<!-- mathcaptcha -->
<div class="mathcaptcha" :class="errorData.mathcaptcha ? 'text-red-500' : ''">
<label class="" for="mathcaptcha">Введите результат функции: <span x-text="mathcaptchaLabel" ></span></label>
<div class="flex space-x-4 text-gray-400 focus-within:text-gray-800">
<x-modules.button icon="refresh" style="black-outline" class="px-4" @click.prevent="mathcaptchaReset()" />
{!! app('mathcaptcha')->input(['class' => 'appearance-none rounded-md shadow-sm border-gray-300 placeholder-gray-400 focus:border-sky-500 focus:ring-1 focus:ring-sky-500 focus:outline-none valid:border-green-500 invalid:border-red-500 block w-full', 'id' => 'mathcaptcha', 'type' => 'text', 'name' => 'mathcaptcha', 'x-model' => 'formData.mathcaptcha']) !!}
</div>
<span x-text="errorData.mathcaptcha" class="text-red-500 text-xs"> </span>
</div>
<script>
function topbar() {
return {
mathcaptchaLabel: '{{ app('mathcaptcha')->label() }}',
mathcaptchaReset() {
axios.get('/reload-captcha')
.then( (response) => {
console.log(response);
this.mathcaptchaLabel = response.data.mathcaptchaLabel; // assign mathcaptchaLabel value new captcha from json response
});
},
submitData() {
axios.post('/modalform', this.formData)
.then( (response) => {
...
this.mathcaptchaLabel = response.data.mathcaptchaLabel; // assign mathcaptchaLabel value new captcha from json response
})
},
routes/web.php
Route::get('/reload-captcha','MainController@reloadCaptcha')->name('reloadCaptcha');
MainController.php
public function modalform(ModalformRequest $request) {
Mail::to( config('mail.to.address') )->send(new Modalform());
app('mathcaptcha')->reset();
return response()->json([
'status' => 'success',
'messageHeader' => 'Ваш вопрос отправлен!',
'messageContent' => 'В ближайшее время мы свяжемся с вами.',
'mathcaptchaLabel' => app('mathcaptcha')->label(),
]);
}
public function reloadCaptcha()
{
app('mathcaptcha')->reset();
return response()->json([
'mathcaptchaLabel' => app('mathcaptcha')->label(),
]);
}
I use your captcha in ajax form. All is working, except that after successfull POST request I still see old captcha and if I try to send message again with the same captcha I got validation error. It is changed only when I refresh the page after successfull POST request. What is wrong? And how to make button for manual captcha reset?
blade view
routes/web.php
app/Http/Controllers/MainController.php
app/Mail/Modalform.php
App/Http/Requests/ModalformRequest.php