Production apps user input checks

In production apps, how do you typically guard from instance where say you have a UIPicker for Integers and someone could just paste text in there? I’ve never been overly clear on what level of checks you should apply, short of checking that the fields aren’t empty so you can enable a “done” button to try and save the user input and guard that it isn’t null?

Short of something like:

// user pastes "abc" into a field meant for Integers (in String format)
if let value = Int(textfield.text!){
     object.value = value
     // save to core data
}

I’m used to JS/PHP/Python

Where there’s a lot of form validation first, then server validation etc

After successfully converting the input as an Integer you should be good to go (assuming you don’t store the data on a remote server).

So say all my info is required and a user fills it in, but uses text for a number field by pasting into a uipicker that uses numbers as it’s data and it can’t be casted.

Do i need to then be doing a final check that var 1, var2, var3 != nil before I try to save in CoreData?

I don’t understand exactly what you mean, but from your first question you are trying to save numbers into core data, right?
For all the free form text your user inserts into text fields, you should simply try to convert it to a number/integer. That also ensures your data is not nil.

I mean in this sense.

Model

@NSManaged var one: Int
@NSManaged var two: NSNumber?

Someone

if let value = Int(textfield1.text!){
entity.one = value // say this fails because someone pasted text
}
if let value = Int(textfield2.text!){
entity.two = value 
}

entity.one is required and can’t be cast. So entity.one is never set. So just calling blindly entity.save() for CoreData will crash the app. So should there be an additional step like this:

if entity.one != nil {
entity.save()
}

If this fails, you shouldn’t save it to core data at all. Instead let the user somehow know and ask for correct input.
The check for nil shouldn’t be neccessary.

That’s the basis of my question though, how is it actually handled? What are some good examples to notify users that a field is in the correct format? My simple solution was only to call save() if entity.one wasn’t nil.

Tutorials generally don’t cover this I find.

One possible solution would be an alert:

if let value = Int(textfield1.text!){
entity.one = value // say this fails because someone pasted text
} else {
let alert = UIAlertController(title: "", message: "Please enter a valid number!", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
textfield1.text = "";  // Clear the textfield
present(alert, animated: true, completion: nil)
}

I assume you’re calling this code inside your UIViewController.
present(alert, animated: true, completion: nil) is a method of UIViewController.

Let me know if you have any questions :slight_smile:

Thanks emir,

I was thinking this was probably a decent idea. But wanted to check as I haven’t seen much error checking like this going on :slight_smile:

No problem happy to help :wink: