Search code examples
phpswiftalamofire

How to return validation errors back to swift from php or segue on success


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'


Solution

  • I am hoping someone will show me a better way to do this but I have made some progress and it is working. My understanding of the Alamofire documentation is that if you get a response that means it will always be a 200 response no matter what your server sends back. So, the only way I could work around that was to parse the response as json using codable and then do something based on that data, like this:

            Alamofire.request(url!, method: .post, parameters: parameters)
                .responseJSON { response in
                    let data = response.data
                    let jsonDecoder = JSONDecoder()
    
    
                    do {
    
                        let serverResponse = try jsonDecoder.decode(PhpResponse.self, from: data!)
                        if serverResponse.status == "200" {
    
                            print("Success")
    
                        } else {
    
                            print(serverResponse.message)
                        }
    
                    } catch {
    
                        debugPrint(error.localizedDescription)
    
                    }
            }
    

    So, I post the data from swift to the server, do some form validation using php and then using php's json_encode, I send that back to swift in json format and do something based on that response.