Skip to content

Commit 658252d

Browse files
update
1 parent e201fe1 commit 658252d

File tree

3 files changed

+48
-72
lines changed

3 files changed

+48
-72
lines changed

src/Traits/PropertyTrait.php

Lines changed: 15 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,84 +6,49 @@
66

77
trait PropertyTrait {
88

9-
/**
10-
* param
11-
*/
9+
/** @var mixed form param data */
1210
public $param;
1311

14-
/**
15-
* attribute
16-
* @var mixed
17-
*/
18-
public $attribute;
12+
/** @var mixed attribute */
13+
protected $attribute;
1914

20-
/**
21-
* public message notice
22-
* @var mixed
23-
*/
15+
/** @var mixed public message data */
2416
public $message;
2517

18+
/** @var array Defined rules collectors */
19+
private $rules = [];
20+
2621
/** @var \Symfony\Component\HttpFoundation\JsonResponse|null */
27-
public $jsonResponse = null;
22+
public $jsonResponse;
2823

2924
/** Flag to avoid double-sending responses */
3025
protected bool $responseSent = false;
3126

32-
/**
33-
* Defined rules collectors
34-
*
35-
* @var array
36-
*/
37-
private $rules = [];
38-
39-
/**
40-
* Keep track of manually calling the validate method
41-
*
42-
* @var bool
43-
*/
27+
/** @var bool Keep track of manually calling the validate method */
4428
private $isValidatedCalled = false;
4529

46-
47-
/**
48-
* proceed
49-
* @var bool
50-
*/
30+
/** @var bool proceed */
5131
public $flashVerify;
5232

53-
/**
54-
* proceed
55-
* @var bool
56-
*/
33+
/** @var bool proceed */
5734
private $proceed;
5835

59-
/**
60-
* error
61-
* @var bool
62-
*/
36+
/** @var bool error */
6337
private $error = false;
6438

65-
/**
66-
* flash
67-
* @var array
68-
*/
39+
/** @var array flash data */
6940
public $flash = [
7041
'message' => [],
7142
'class' => '',
7243
];
7344

74-
/**
75-
* error class
76-
* @var array
77-
*/
45+
/** @var array style class */
7846
public $class = [
7947
'success' => 'alert alert-success',
8048
'error' => 'alert alert-danger',
8149
];
8250

83-
/**
84-
* error class
85-
* @var array
86-
*/
51+
/** @var array error configuration */
8752
public $config = [
8853
'csrf' => true,
8954
'operator' => false,

src/Validator.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,29 +122,37 @@ public function validate($closure = null)
122122

123123
/**
124124
* Form save response
125-
*
125+
*
126126
* @param Closure $function
127127
* @return mixed
128128
*/
129129
public function save($closure)
130130
{
131-
// if validation already failed, return stored JsonResponse
131+
// If user returns a JsonResponse
132132
if (ValidatorMethod::isJsonResponse($this->jsonResponse)) {
133-
return $this->jsonResponse;
133+
return $this->jsonResponse->send();
134134
}
135135

136136
if($this->isValidated()){
137-
137+
138138
// save into a remembering variable
139139
ValidatorMethod::resolveFlash($this);
140-
140+
141141
$response = $this->callback($closure);
142142

143-
// If user returns a JsonResponse in save, return it
143+
dd(
144+
'sss'
145+
);
146+
exit();
147+
148+
// If user returns a JsonResponse in save, send and return it
144149
if (ValidatorMethod::isJsonResponse($response)) {
145-
return $response;
150+
if (!$this->responseSent) {
151+
$response->send();
152+
$this->responseSent = true;
153+
}
146154
}
147-
155+
148156
// delete csrf session token
149157
CsrfToken::unsetToken();
150158
}
@@ -265,6 +273,16 @@ public function merge($keys = null, $data = null)
265273
return ValidatorMethod::merge($keys, $data);
266274
}
267275

276+
/**
277+
* Get Attribute Data
278+
*
279+
* @return mixed
280+
*/
281+
public function getAttribute()
282+
{
283+
return $this->attribute;
284+
}
285+
268286
/**
269287
* Return previously entered value
270288
*

tests/server.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
include_once __DIR__ . "/include.php";
44

55

6-
$form->token(true)->error(false)->rules([
6+
$result = $form->token(true)->error(false)->rules([
77
"string|name" => 'Please enter a name',
88
"str_len|name|<|5" => 'Name should be more than five(5) characters',
99
"email|email" => 'Please enter a valid email address',
@@ -16,26 +16,19 @@
1616
"enum:terms" => 'Accept terms and condition',
1717
])->validate(function($response){
1818

19-
// return $response->json(1, $response->message);
20-
21-
var_dump(
22-
$response,
23-
'sss'
24-
);
25-
exit();
19+
return $response->json(1, $response->message);
2620
})->save(function($response){
2721
// access the form data
2822
$param = $response->param;
2923

3024
// access parent scope data\ $data
3125
$attribute = $response->attribute;
3226

33-
// message
34-
$response->message = "Submitted Successfully";
27+
// dump(
28+
// // $param->message,
29+
// $param->toArray(),
30+
// );
3531

36-
dump(
37-
// $param->message,
38-
$param->toArray(),
39-
);
32+
return $response->json(0, "Submitted Successfully");
4033
});
4134

0 commit comments

Comments
 (0)