Apologies
You must be signed in to watch this lesson.
As with any type of application that takes user input, you'll need a way to verify that the inputs are actually correct. One easy way to present a friendly message to your users is by using UIAlertController to present a small warning box if you've detected invalid form entries. This process of checking user fields is typically referred to as Form Validation. Let's see this in action by introducing a birthday field for out Employees

Comments (13)
Sylvain
6 years ago
I've got something weird. If I tap 10/10/2010 and save. The fetched date in the cell is 10/09/2010. I don't know why I have one day of difference.
Brian Voong
6 years ago
Sylvain
6 years ago
Oh of course... Thanks !
Kilian Hiestermann
6 years ago
So far: Awesome course. I'm really enjoying it.
Uchenna Aguocha
6 years ago
Hey Brian! Two questions! Should I use contentView.addSubView( ) or addSubview( ) when adding things to a custom UICollectionViewCell? Is it best practice to use contentView.addSubView instead of .addSubView( )?
Brian Voong
6 years ago
mohitnandwani
6 years ago
Hey Brian! Just noticed something. How come your project uses less memory (mine is around 130 MB while I saw yours to be around 79 MB) despite having the similar code? How to reduce the memory usage? (only difference is that the simulator I'm using is iPhone SE on iOS 11.2).
Brian Voong
6 years ago
forsuresh
6 years ago
Hi. It seems that the handleSave doesn't reset itself completely when returning from 'func showError'. For example, if I get to add employee screen and then put an invalid value in for date, I get alert stating "Bad Date: Birthday date entered not valid". Now if I click on OK, make the Name Field empty and put in a valid date for birthdayTextField, employee with blank name is added to the company employees list. I was thinking of dismissing the CreateEmployeeController but that doesn't seem intuitive for the user if they make a small typo. Am I missing something obvious? Thank you for the great videos.
forsuresh
6 years ago
Sorry, Just looked over my code again and realized I made an extra paste when copy & pasting around. Was checking if the Date Field was .isempty when I should have been checking if the Name field was .isempty. Sorry for the false alarm.
RichyCode
6 years ago
works for me think you have a typo
Chase McElroy
6 years ago
Would you ever extent UIViewController with your alert func?
Dennisvm82
6 years ago
Hey Brian! Why did you use a more basic and not really user-friendly approach for the birthday field this time? I would have expected a UIDatePicker to get rid of unnecessary form validation and typing. Really enjoying this course, man! The tuples and protocols helped me out so much. For my own application I use a struct in conjunction with a function to throw different alerts. Very convenient if you have lots of different alert boxes to show within your app. enum AlertType { case mailError, mailSuccess, mailConfigure case userError, userLoginEmpty, userLoginIncorrect } struct Alert { static func showAlert(ofType type: AlertType, handler: ((UIAlertAction) -> ())? = nil) -> UIAlertController { var message: String case .mailError: message = “Could not send email message.” case .mailSuccess: message = “Email has been sent.“ case .mailConfigure: message = “Could not configure email account.“ case .userError: message = “An error has occurred. Please try again.“ case .userLoginEmpty: message = “Please enter your username and password to login. “ case .userLoginIncorrect: message = “Incorrect username and/or password..“ } let alert = UIAlertController(title: "Message", message: message, preferredStyle: .alert) alert.addAction(UIAlertAction(title: "OK", style: .default, handler: handler)) return alert } }
LandsPete
5 years ago
Hi Brian, I think I've found a mistake,or I did something wrong. It's about formatting the date, at the moment I can type in a wrong year (19876 instead of 1987) and it will be saved without error. The DateFormat works only for day and month, if those are wrong than the AlertController will show up. Unfortunately, I have not been: "dd.MM.yyyy" Is responsible for this task, but I can't find a solution for that problem. Do you have any idea why this might be the case? Anyway, awesome course. I'm really enjoying it, learn a lot of "crazy" Swift stuff and my English gets better every day!! 
Brian Voong
5 years ago
LandsPete
5 years ago
I thought that the DateFormatter = "dd.MM.yyyy" can prevent that. You can parse the date object and apply a check on the year itself and present and error if its not within a specific range that you specify, that sounds little bit complicated for me yet, perhaps in the future, maybe in 19876
Cinquain
5 years ago
Fire!
Bmccanrw
5 years ago
Hello Brian, I am not sure where to ask this question or where to go for more information on it. I am working on a program dealing with About 80,000 entities with about 15 attributes each. The attributes are very small amounts of data (a single integer or string in most cases) but in need to perform but I need to perform specific functions on them. This is really slowing down my application. I coded the same program in an excel spread sheet to prototype and test it before learning to code it in swift and my excel program handles the calculations quicker than my swift program. I think it might be the way exchange between CoreData and my app that is the slow down. Is there a better way when dealing with a large amount of entities? Thanks, Brice
Brian Voong
5 years ago
JulienR
5 years ago
Hi Brian, exactly at 9:37, what is the keyboard shortcut or keyboard / mouse combination to show directly the result of the var/let ?
Brian Voong
5 years ago
JulienR
5 years ago
ok thanks !
jdhindsa
5 years ago
Hi Brian, I'm confused by some steps you completed. For example, in handleSave(), you are using a guard let to make sure that the birthday field contains a value: guard let birthdayText = birthdayTextField.text else {return} But then after that you are presenting a UIAlertController if no date was entered. What I don't understand is that if no date was entered, then the guard let would cause the return statement to execute and you will exit the function. So, the alert never gets shown. This is at least my understanding of how guard let works.. but when I test your app out it works as you indicated. Is the guard-let-return not supposed to exit the function if the birthday is nil?
Cinquain
4 years ago
More fire
HELP & SUPPORT