8000 Fix timer validation on Reply and Thread create forms · laravelio/laravel.io@5dc3f3b · GitHub
[go: up one dir, main page]

Skip to content

Commit 5dc3f3b

Browse files
Fix timer validation on Reply and Thread create forms
1 parent ffbbf14 commit 5dc3f3b

File tree

5 files changed

+26
-24
lines changed

5 files changed

+26
-24
lines changed

app/Lio/Core/FormModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php namespace Lio\Core;
22

3-
use Validator, App;
3+
use App, Validator;
44
use Lio\Core\Exceptions\NoValidationRulesFoundException;
55

66
class FormModel

app/Lio/Forum/Replies/ReplyForm.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
<?php namespace Lio\Forum\Replies;
22

33
use Lio\Core\FormModel;
4-
use Input, Validator;
4+
use Validator;
55

66
class ReplyForm extends FormModel
77
{
88
protected $validationRules = [
99
'body' => 'required',
10+
'_time' => 'min_time:2',
1011
];
1112

12-
protected function beforeValidation()
13+
public function beforeValidation()
1314
{
14-
$time = Input::get('_time');
15+
$type = isset($this->inputData['_type']) ? $this->inputData['_type'] : null;
1516

16-
// Conditional validation rule:
17-
// - compare processing time to form-creation time
18-
// - if the difference is less than 2 seconds
19-
// - we apply a rule where the max length of the _time input is 0
20-
// - and then validation will fail because it is not 0
21-
Validator::sometimes('_time', 'max:0', function() use ($time)
22-
{
23-
return (strtotime("now") - $time) < 2;
24-
});
17+
// Time validation on Create forms
18+
if ($type === 'create') {
19+
Validator::extend('min_time', function ($attribute, $time, $params) {
20+
$minTime = $params[0];
21+
22+
return (time() - $time) > $minTime;
23+
});
24+
}
2525
}
2626
}

app/Lio/Forum/Threads/ThreadForm.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php namespace Lio\Forum\Threads;
22

33
use Lio\Core\FormModel;
4-
use App, Input, Validator;
4+
use App, Validator;
55

66
class ThreadForm extends FormModel
77
{
@@ -11,6 +11,7 @@ class ThreadForm extends FormModel
1111
'tags' => 'required|max_tags:3',
1212
'is_question' => 'in:0,1',
1313
'laravel_version' => 'required|in:0,3,4',
14+
'_time' => 'min_time:5',
1415
];
1516

1617
protected function beforeValidation()
@@ -28,16 +29,15 @@ protected function beforeValidation()
2829
return true;
2930
});
3031

31-
$time = Input::get('_time');
32+
$type = isset($this->inputData['_type']) ? $this->inputData['_type'] : null;
3233

33-
// Conditional validation rule:
34-
// - compare processing time to form-creation time
35-
// - if the difference is less than 5 seconds
36-
// - we apply a rule where the max length of the _time input is 0
37-
// - and validation will always fail because it is not 0
38-
Validator::sometimes('_time', 'max:0', function() use ($time)
39-
{
40-
return (strtotime("now") - $time) < 5;
41-
});
34+
// Time validation on Create forms
35+
if ($type === 'create') {
36+
Validator::extend('min_time', function ($attribute, $time, $params) {
37+
$minTime = $params[0];
38+
39+
return (time() - $time) > $minTime;
40+
});
41+
}
4242
}
4343
}

app/views/forum/replies/_create.blade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
{{-- Time field used to check for spam bots --}}
66
{{ Form::hidden('_time', time()) }}
7+
{{ Form::hidden('_type', 'create') }}
78

89
<div class="form-row">
910
<label class="field-title">Reply</label>

app/views/forum/threads/create.blade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
{{-- Time field used to check for spam bots --}}
1414
{{ Form::hidden('_time', time()) }}
15+
{{ Form::hidden('_type', 'create') }}
1516

1617
<section class="padding">
1718
<div class="form-row">

0 commit comments

Comments
 (0)
0