I am using alamofire to post data to a php script which inserts data into a mysql database. This part is working fine but I have some validation on the server like checking for a valid email address for example. So, here is the swift code:
Alamofire.request(url!, method: .post, parameters: parameters)
.validate(statusCode: 200..<300)
.responseJSON { response in
switch response.result {
case .success:
print(response)
case .failure(let error):
print(error)
}
}
Here is the php that handles the inserting.
if($booking->create($conn)) {
$response['status'] = "200";
$response['message'] = "Success";
} else {
$response['status'] = "400";
$response['message'] = $booking->errors;
}
echo json_encode($response);
In swift, the response in console for failed validation is:
SUCCESS: {
message = (
"Invalid email address",
"Contact number required"
);
status = 400;
}
If there are no errors, I actually want to segue to another view controller and if there are errors, I want to display them in labels perhaps.
But, initially I just wanted to say if there are any errors, print them and if there are no errors, then segue. But if I try to put an if statement into the response I get an error.
case .success:
if response == "200" {
// segue
}
Binary operator '==' cannot be applied to operands of type 'DataResponse' and 'String'