Skip to content

Commit 7730b52

Browse files
update
1 parent b1a5c7e commit 7730b52

File tree

6 files changed

+184
-8
lines changed

6 files changed

+184
-8
lines changed

src/Methods/Datatype.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ protected static function validateFormInput(array $rulesData)
156156
$sanitizeArray = function ($arr) use (&$sanitizeArray) {
157157
return array_map(fn($v) =>
158158
is_array($v) ? $sanitizeArray($v) :
159-
(is_string($v) ? Purify::string($v) : $v), $arr);
159+
(is_string($v) ? Purify::string((string) $v) : $v), $arr);
160160
};
161161

162162
return (is_array($array) && count($array) > 0)
@@ -177,17 +177,17 @@ protected static function validateFormInput(array $rulesData)
177177

178178
// ---------------- HTML ----------------
179179
case in_array($ruleFlag, self::RULE_HTML, true):
180-
$sanitized = Purify::html($value);
180+
$sanitized = Purify::html((string) $value);
181181
return (empty($sanitized) && $sanitized !== '0') ? false : $sanitized;
182182

183183
// ---------------- DEV ----------------
184184
case in_array($ruleFlag, self::RULE_DEV, true):
185-
$sanitized = Purify::dev($value);
185+
$sanitized = Purify::dev((string) $value);
186186
return (empty($sanitized) && $sanitized !== '0') ? false : $sanitized;
187187

188188
// ---------------- DEFAULT STRING ----------------
189189
default:
190-
$sanitized = Purify::string($value);
190+
$sanitized = Purify::string((string) $value);
191191
return (empty($sanitized) && $sanitized !== '0') ? false : $sanitized;
192192
}
193193
}

src/Traits/ValidatorTrait.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,16 @@ public function getClass()
136136
return ValidatorMethod::getClass();
137137
}
138138

139+
/**
140+
* Alias form `errorType` method
141+
* @param bool $type
142+
* @return $this
143+
*/
144+
public function error(?bool $type = false)
145+
{
146+
return $this->errorType($type);
147+
}
148+
139149
/**
140150
* Error type handler
141151
* @param bool $type

src/helpers.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
use Tamedevelopers\Validator\Methods\ValidatorMethod;
77

88

9+
/**
10+
* Helps without calling the method multiple times
11+
*/
12+
$Tame_isAppFramework = function_exists('Tame_isAppFramework') ? Tame_isAppFramework() : false;
13+
14+
915
if (! function_exists('form')) {
1016

1117
/**
@@ -21,7 +27,7 @@ function form($attribute = null)
2127
}
2228
}
2329

24-
if (! Tame_isAppFramework() && ! function_exists('old')) {
30+
if (! $Tame_isAppFramework && ! function_exists('old')) {
2531

2632
/**
2733
* Return previously entered value
@@ -54,7 +60,7 @@ function old($key = null, $default = null)
5460
*
5561
* @return void
5662
*/
57-
function config_form(?bool $error_type = false, ?bool $csrf_token = true, $request = null, ?array $class = [])
63+
function config_form($error_type = false, $csrf_token = true, $request = null, $class = [])
5864
{
5965
// config holder
6066
if(!defined('TAME_VALIDATOR_CONFIG')){
@@ -83,7 +89,7 @@ function config_form(?bool $error_type = false, ?bool $csrf_token = true, $reque
8389
}
8490
}
8591

86-
if (! Tame_isAppFramework() && ! function_exists('csrf_token')) {
92+
if (! $Tame_isAppFramework && ! function_exists('csrf_token')) {
8793

8894
/**
8995
* Get Csrf Token
@@ -96,7 +102,7 @@ function csrf_token()
96102
}
97103
}
98104

99-
if (! Tame_isAppFramework() && ! function_exists('csrf')) {
105+
if (! $Tame_isAppFramework && ! function_exists('csrf')) {
100106

101107
/**
102108
* Generate Input for Csrf Token

tests/include.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
include_once __DIR__ . "/../vendor/autoload.php";
4+
5+
$form = form()->post();
6+

tests/index_ajax.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
include_once __DIR__ . "/include.php";
4+
?>
5+
6+
<!DOCTYPE html>
7+
<head>
8+
<title>Form validation</title>
9+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
10+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, maximum-scale=1">
11+
<link href="style.css" rel="stylesheet" type="text/css">
12+
</head>
13+
<html>
14+
<body>
15+
16+
<form method="post" action="<?= tasset('tests/server.php', false, true);?>" class="form"
17+
onsubmit="return submitForm(this)">
18+
<h2>Form sample</h2>
19+
20+
<div class="errorMSg mb-5 <?= $form->getClass() ?>">
21+
<?= $form->getMessage() ?>
22+
</div>
23+
24+
<?php csrf() ?>
25+
26+
<div class="row">
27+
<div class="">
28+
<label for="html">Name</label>
29+
<input type="text" name="name" value="<?= $form->old('name'); ?>">
30+
</div>
31+
32+
<div class="">
33+
<label for="html">Email</label>
34+
<input type="text" name="email" value="<?= old('email'); ?>">
35+
</div>
36+
37+
<div class="">
38+
<label for="html">Age</label>
39+
<input type="number" name="age" value="<?= old('age'); ?>">
40+
</div>
41+
42+
<div class="">
43+
<label for="html">Loan Amount</label>
44+
<input type="number" step="any" name="amount" value="<?= old('amount'); ?>">
45+
</div>
46+
47+
<div class="">
48+
<label for="html">Message</label>
49+
<textarea name="message" rows="5" style="resize:none;"
50+
cols="81"><?= old('message'); ?></textarea>
51+
</div>
52+
53+
<div class="activities">
54+
<p class="title">
55+
Activities you're interested in:
56+
</p>
57+
58+
<label for="reading">
59+
Reading
60+
<input type="checkbox" name="activities[]" value="reading" id="reading" <?= old('activities.reading') ? 'checked' : '' ?> >
61+
</label>
62+
<label for="writing">
63+
Writing
64+
<input type="checkbox" name="activities[]" value="writing" id="writing" <?= old('activities.writing') ? 'checked' : '' ?>>
65+
</label>
66+
67+
<label for="terms" style="margin-top: 30px;">
68+
Accept terms
69+
<input type="checkbox" name="terms" id="terms"
70+
value="accepted" <?= old('terms') ? 'checked' : '' ?> >
71+
</label>
72+
</div>
73+
74+
<button type="submit" class="btn mt-2">Submit</button>
75+
</div>
76+
</form>
77+
78+
79+
<script>
80+
function submitForm(form){
81+
event.preventDefault();
82+
83+
let formData = new FormData(form);
84+
85+
// send via fetch
86+
fetch(form.action, {
87+
method: form.method, // POST
88+
body: formData
89+
})
90+
.then(response => response.text())
91+
.then(data => {
92+
// parse the JSON response
93+
data = JSON.parse(data);
94+
95+
console.log(
96+
data,
97+
data.response,
98+
data.message
99+
);
100+
return false;
101+
// handle success/error response from server
102+
if(data.status === "success"){
103+
alert("Form submitted successfully ✅");
104+
105+
} else {
106+
alert("Error: " + data.message);
107+
}
108+
})
109+
.catch(error => {
110+
console.error("Error:", error);
111+
});
112+
}
113+
</script>
114+
</body>
115+
</html>

tests/server.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
include_once __DIR__ . "/include.php";
4+
5+
6+
$form->token(true)->error(false)->rules([
7+
"string|name" => 'Please enter a name',
8+
"str_len|name|<|5" => 'Name should be more than five(5) characters',
9+
"email|email" => 'Please enter a valid email address',
10+
"int:age" => 'Age is required',
11+
"int:age:<:16" => 'Sorry! you must be 16yrs and above to use this site',
12+
"float:amount" => 'Enter Loan Amount',
13+
"array:activities" => 'Select one or more activities',
14+
"array:activities" => 'Select one or more activities',
15+
"string:message" => 'Message cannot be empty',
16+
"enum:terms" => 'Accept terms and condition',
17+
])->validate(function($response){
18+
19+
return $response->json(1, $response->message);
20+
var_dump(
21+
$response->message,
22+
);
23+
exit();
24+
})->save(function($response){
25+
// access the form data
26+
$param = $response->param;
27+
28+
// access parent scope data\ $data
29+
$attribute = $response->attribute;
30+
31+
// message
32+
$response->message = "Submitted Successfully";
33+
34+
dump(
35+
// $param->message,
36+
$param->toArray(),
37+
);
38+
});
39+

0 commit comments

Comments
 (0)