Skip to content

Commit 159a820

Browse files
update
1 parent 58c3d78 commit 159a820

3 files changed

Lines changed: 51 additions & 21 deletions

File tree

src/Traits/ValidatorTrait.php

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ trait ValidatorTrait {
2222
private function callback($closure = null)
2323
{
2424
if(Tame::isClosure($closure)){
25-
$closure($this);
25+
return $closure($this);
2626
}
2727
}
2828

2929
/**
3030
* Use form methods without actually submitting form
3131
*
3232
* @param Closure $closure
33-
* @return void
33+
* @return mixed
3434
*/
3535
public function noInterface($closure)
3636
{
37-
$this->callback($closure);
37+
return $this->callback($closure);
3838
}
3939

4040
/**
@@ -80,17 +80,41 @@ public function getForm($type = null)
8080
return ValidatorMethod::getForm($type);
8181
}
8282

83+
/**
84+
* Return a JSON response with status code and message
85+
*
86+
* @param int $response
87+
* @param mixed $message
88+
* @param int $statusCode
89+
* @return \Symfony\Component\HttpFoundation\JsonResponse
90+
*/
91+
public static function json(int $response = 0, $message = null, int $statusCode = 200)
92+
{
93+
return Tame::json($response, $message, $statusCode);
94+
}
95+
96+
/**
97+
* Alias for `echoJson` method
98+
*
99+
* @param int $response
100+
* @param mixed $message
101+
* @return mixed<json>
102+
*/
103+
public static function jsonEcho(int $response = 0, $message = null)
104+
{
105+
self::echoJson($response, $message);
106+
}
107+
83108
/**
84109
* Returns encoded JSON object of response and message
85110
*
86-
* @param int|float $response
111+
* @param int $response
87112
* @param mixed $message
88-
* @return string
89-
* - [json]
113+
* @return mixed<json>
90114
*/
91-
public function echoJson(?int $response = 0, $message = null)
115+
public static function echoJson(int $response = 0, $message = null)
92116
{
93-
return Tame::echoJson($response, $message);
117+
Tame::jsonEcho($response, $message);
94118
}
95119

96120
/**

src/Validator.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class Validator implements ValidatorInterface
3737
PropertyTrait,
3838
ValidateSuccessTrait;
3939

40+
41+
public $jsonResponse = null;
42+
4043
/**
4144
* @param mixed $attribute
4245
* - Any outside parameter you would want to use within the form instance
@@ -109,7 +112,13 @@ public function validate($closure = null)
109112
// save into a remembering variable
110113
ValidatorMethod::resolveFlash($this);
111114

112-
$this->callback($closure);
115+
// run callback, which should return JsonResponse
116+
$response = $this->callback($closure);
117+
118+
// Instead of returning the JsonResponse, keep chaining
119+
if ($response instanceof \Symfony\Component\HttpFoundation\JsonResponse) {
120+
$this->jsonResponse = $response;
121+
}
113122
}
114123

115124
return $this;
@@ -119,10 +128,15 @@ public function validate($closure = null)
119128
* Form save response
120129
*
121130
* @param Closure $function
122-
* @return $this
131+
* @return mixed
123132
*/
124133
public function save($closure)
125134
{
135+
// if validation already failed, stop here
136+
if ($this->jsonResponse instanceof \Symfony\Component\HttpFoundation\JsonResponse) {
137+
return $this->jsonResponse;
138+
}
139+
126140
if($this->isValidated()){
127141

128142
// save into a remembering variable
@@ -133,8 +147,6 @@ public function save($closure)
133147
// delete csrf session token
134148
CsrfToken::unsetToken();
135149
}
136-
137-
return $this;
138150
}
139151

140152
/**

src/helpers.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
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-
159
if (! function_exists('form')) {
1610

1711
/**
@@ -27,7 +21,7 @@ function form($attribute = null)
2721
}
2822
}
2923

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

3226
/**
3327
* Return previously entered value
@@ -89,7 +83,7 @@ function config_form(?bool $error_type = false, ?bool $csrf_token = true, $reque
8983
}
9084
}
9185

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

9488
/**
9589
* Get Csrf Token
@@ -102,7 +96,7 @@ function csrf_token()
10296
}
10397
}
10498

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

107101
/**
108102
* Generate Input for Csrf Token

0 commit comments

Comments
 (0)