Instructor: Hey hey.
In this video we're gonna do custom validation with JSF.
We'll cover the following topics.
We'll first start off with a demo of custom validation
and then we'll learn how
to actually implement the code for custom validation
and, finally, we'll pull it all together
with a full JSF page example
and we'll actually have some fun with this.
I have a really fun business rule
that we'll implement for our custom validation.
So we'll have a little fun with this one.
So the fun part of this video is
that we'll have a course code and our course code will have
to follow a special business rule validation.
In fact, it'll actually have to start
with the word LUV for luv2code.
So I'll enter the first name, last name.
I'll enter any weird course code here,
and I'll hit submit.
And we'll see that the error message says that
course code must start with LUV for luv 2 code.
That's the fun part, I'm all about love.
So what we need to do here is we have
to clean up the course code so here I'll say LUV123
and then I'll hit submit.
And so this passes.
So in the background I have
that little weird custom business rule that I created
to actually validate the information for a form field.
And I'll actually show you how to write the code for that
and how to implement your own custom validation rule
in this video.
I'll show you that in a second.
Alright so at every company you'll have
to set up a custom validation, right?
So for your company you have special rules
that you need to implement.
So, in my case, I had a course code
and I had the funny rule that hey,
the course code must start
with the letters LUV for love 2 code.
So what you can do here is you can make use
of JSF so your form can call your custom validation method
that resides on your MangedBean.
This custom message will read the input
and determine if it passes or fails.
And if it fails, it'll actually throw an exception.
And I'll show you how to write the code for this method,
coming up on the next slide.
Alright so we can create our own custom validation method
and we actually define it inside of our ManagedBean.
So really all you do here is you defined a method,
in this case my method name is validateTheCourseCode.
This method will take in three parameters:
a context, a component, and the Object value.
Value is the actual data that the user entered
so inside of this method you can say
if the value does not passed, then we throw an exception.
If the value passes, then the method ends gracefully.
So, in this case, no news good news.
Now when you define the method you can really give any name
that you want for the method.
Here I've called it validateTheCourseCode.
You could call it process, do some work,
anything as long as it's public void
and it accepts those three parameters:
context, component, and Object value.
Alright so now that we have our method created,
let's move into our JSF form.
So in our JSF form we have our normal inputText
with our studentFour.courseCode.
Now note this new entry here for validator.
We have validator="#{studentFour.validateTheCourseCode}"
so effectively what I'm doing here is I'm telling it,
hey to validate this field, call this method.
And so this method is defined in our ManagedBean
and we saw that on the previous slide.
So when I hit submit,
JSF will call that method validateTheCourseCode
and it'll pass in those three parameters:
the context, the UI component, and the Object value
that the user entered and that's basically it.
Alright so let's pull it all together.
In the top left we'll have our form
that has the new field here for our course code.
In the center we're gonna have our Managed Bean,
StudentFour, that holds the form data plus
that custom validation method.
And at the bottom we'll have our confirmation page
or our response page that'll simply display the course code
to the user.
Alright so let's take a look at our to-do list.
So the first thing we need
to do is add a new ManagedBean class, StudentFour.
It's gonna have those fields
for firstName, lastName, and courseCode
and we'll also add our custom validator method,
update the form to call the custom validator,
and then finally we'll update the confirmation page
to display that new course code.
Alright I'm excited here.
Let's jump into Eclipse and start coding.
Alright so we're in Eclipse.
What we're gonna do is, again,
use an existing project, validate-demo.
The first thing I need to do is create the Managed Bean
so I move down to the Java src directory,
through our package, and, again,
we're not gonna create it from scratch.
We'll start with the existing code
since we've done a lot of this work before
and I'll kinda walk through each part of it.
So this is StudentFour.java, our Managed Bean.
We're gonna have three fields here
for this bean: firstName, lastName, and courseCode.
And, remember, courseCode is this new field
that we're tracking here.
And our regular work
of having our no-arg constructor StudentFour
(the same name as the class), all
of our getters and setters, and the new section here
for getCourseCode setCourseCode.
That's our new item that we've added here.
And then I'll just move down a bit
to our custom validation method
called validateTheCourseCode.
Now this method can have any name that you want
as long as it follows this signature public void
and then here, validateTheCourseCode,
you can give it any name you want.
You can call it foo, process, do the work
as long as it's public void
and it accepts these three parameters being passed in.
So it must accept the FacesContext,
the actual UIComponent, and the Object value.
Now Object value that's very important
because that actually holds the text
that the user entered.
And if there's a problem during this validation,
we're gonna throw an error or throw an exception.
So this is the basic definition
that you would have for a validation method,
but, again, the actual name of the method can be anything.
Alright so here I just check the value
to check if it's null.
If it's null, I simply just return,
meaning I'm not gonna validate on it.
Here I take the value and I get String version of it.
That's the actual String data that the user entered.
And then on line 55 this is my documentation
to myself like hey this is the business rule:
the course code must start with LUV,
again, we're having fun here right?
All about love, it must start with LUV.
If not, throw an exception.
So on line 56 I check to see if data
so I have the exclamation point meaning not,
so if data does not start with LUV,
then there's a problem or we need to throw an exception.
So here I put a FacesMessage,
which is basically like our error message,
it's really just a generic message,
but here I'm gonna use it as an error message.
So I create a new FacesMessage
with the actual problem, "Course code must start with LUV,"
and then I actually throw the exception based
on that message.
And over in our JSF page when we use our h:message,
then that'll display the appropriate error message here.
So basically here I'm just checking the String
to see if it does not start with LUV,
then we're gonna throw an exception.
So note the exclamation point there
in front of the not data.startsWith.
Alright so this looks good.
So that's our custom validation method.
Now moving to the next step what we need
to do is actually call this validator method
from our JSF form.
So I'll open up our form here, student_four_form.xhtml.
And, again, this is very similar
to what we've done before in all previous videos.
Just on line 17 we have a form and then I'll scroll on down
and I'll highlight this one section here
for course code.
So we have the course code, we have our inputText.
This value maps over to studentFour.courseCode
in our Managed Bean.
And the new entry here is for validator
so this is where we're gonna call
our custom validator method,
studentFour.validateTheCourseCode.
Now again, it could be any method name
but here we called it validateTheCourseCode.
When they do a submit, JSF will actually call this method
and automatically pass in those three parameters
(the FacesContext, the UIComponent,
and the actual Object value).
If there is an error,
then we throw that FacesMessage, right?
And we'll display the error here.
If everything's okay, there are no error messages
that it'll consider, then it'll proceed
on with the submission.
Alright so just moving on down
to the bottom, we have our submit button.
And then we have our action student_four_response
and just remember from all the previous videos,
JSF will call student_four_response.xhtml.
Alright so this looks really good here
for our JSF form, good job.
Alright so the next thing we wanna do
is update our confirmation page
so I'll move here to student_four_response.xhtml.
Again, very similar to what we had before,
on line 12 we simply display the student's first name
and last name and on line 16 is our new information
where we display the student's course code
by saying studentFour.courseCode.
And that's basically it here
for our confirmation page for our JSF page.
Alright so let's go ahead and try this out.
So I just ran the application.
We have our form: first name, last name, and course code.
And, again, our course code has
that validation rule attached to it.
So here I'll just enter anything for first name, last name,
and then for course code I'll just enter some bad data.
ABC1234.
And this is where we're gonna have some fun
with it here when I hit the submit button.
It says hey, the course code must start with LUV.
Love2code.
So let's go back and clean up the course code
and enter some good data that starts with LUV
and 123 and then I can go ahead and hit submit.
And this will pass my validation rule.
I make it to the confirmation page
and we see the desired course code accordingly.
So this is great, good job.
So we worked with adding in a new custom validation method.
So in summary, we started off with a custom validation demo
then we went through
and we implemented a custom validation method
and then we put it all together
with the full JSF page example.
Well this wraps up the video.
In this video I showed you
how to perform custom validation with JSF forms.