mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-10 05:02:36 +01:00
Merge pull request #767 from turbo124/develop
Form Request validation re-implemented
This commit is contained in:
commit
4679beb246
@ -34,25 +34,8 @@ class AccountApiController extends BaseAPIController
|
||||
$this->accountRepo = $accountRepo;
|
||||
}
|
||||
|
||||
public function register(Request $request)
|
||||
public function register(RegisterRequest $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'email' => 'required|unique:users',
|
||||
'first_name' => 'required',
|
||||
'last_name' => 'required',
|
||||
'password' => 'required',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
|
||||
foreach($validator->errors()->all() as $message)
|
||||
{
|
||||
return $this->errorResponse(['message'=>$message],400);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
$account = $this->accountRepo->create($request->first_name, $request->last_name, $request->email, $request->password);
|
||||
$user = $account->users()->first();
|
||||
|
@ -2,7 +2,11 @@
|
||||
|
||||
use Auth;
|
||||
use App\Http\Requests\Request;
|
||||
use Illuminate\Http\Request as InputRequest;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Validation\Factory;
|
||||
use App\Libraries\Utils;
|
||||
use Response;
|
||||
|
||||
class RegisterRequest extends Request
|
||||
{
|
||||
@ -11,6 +15,13 @@ class RegisterRequest extends Request
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
public function __construct(InputRequest $req)
|
||||
{
|
||||
$this->req = $req;
|
||||
}
|
||||
|
||||
|
||||
public function authorize()
|
||||
{
|
||||
return true;
|
||||
@ -23,6 +34,7 @@ class RegisterRequest extends Request
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
|
||||
$rules = [
|
||||
'email' => 'required|unique:users',
|
||||
'first_name' => 'required',
|
||||
@ -32,4 +44,24 @@ class RegisterRequest extends Request
|
||||
|
||||
return $rules;
|
||||
}
|
||||
|
||||
public function response(array $errors)
|
||||
{
|
||||
/* If the user is not validating from a mobile app - pass through parent::response */
|
||||
if(!isset($this->req->api_secret))
|
||||
return parent::response($errors);
|
||||
|
||||
/* If the user is validating from a mobile app - pass through first error string and return error */
|
||||
foreach($errors as $error) {
|
||||
foreach ($error as $key => $value) {
|
||||
|
||||
$message['error'] = ['message'=>$value];
|
||||
$message = json_encode($message, JSON_PRETTY_PRINT);
|
||||
$headers = Utils::getApiHeaders();
|
||||
|
||||
return Response::make($message, 400, $headers);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user