 So, a big thanks to Rupal Khandan organizing team. So, coming to today's topic, today we are going to talk about how we can decouple the entity validations from the entity forms. Now, the title might sound very easy or very simple, but it is a very, very important thing, and without wasting any time, let's proceed further. So, validations. Since the word, since the beginning of word by word, validations, the forms, I mean, they have been a very cool for the very integral part of the word by word. I mean, forms are like everywhere, so do other validations. And in Rupal, the form validations or the way the form validations are being broken, or the way we have worked with them is by using these form pointer books, maybe Rupal 7 or Rupal 8, we have been doing these things, adding a form validator book or the form pointer book and adding the custom validate handler. That's how we all have been doing custom validations on the form forms or the entity forms. But if you have worked closely with them, you will know that there is a disadvantage, a big disadvantage associated with using the form alterables and adding validations. Now, I'll simply walk you through the disadvantage through a demo. So, let me just come out of the slides and let me take you to the demo site I have looked at. So, above the demo site first, I have created a Rupal 8.8.3 version of site. This is the standard Rupal profile installation. And I have created a content type called as moving trailer, and it has two fields in it. Apart from the regular title, we have the video summary field, which is a text plane long field. And we have a video URL field. Video URL is a link field. If you edit this particular field, you will notice that this field is going to accept external links only. And I have added a help text over here that says this field accepts you URL only. I'll come out of it and I'll go to the add content page of the movie trailer. Now, as you can see over here, this is the create content page of the type movie trailer. It has three fields, title, video URL, and video summary. Before creating the content model, let me take you to the code editor. So, I have created a custom module and that custom module is called as content validation. This module is already enabled and it has info file, module file and some more files. For this particular demo, I'll first go to the dot module file. And I will show you a book that I have written to hide a validation on the link field. So, I have written this particular book. It is called as book form, base form ID order. Now, this will basically check the node type and it will hide a validate handler. Our custom validate handler is this, which in turn basically looks for the free URL value in the submitted data. And it will check if the submitted URL contains the text UD. If the text is not present, then it is going to throw you an error saying that this particular URL is invalid and you are supposed to enter in UD URL only. So, this is the error you get if you try to enter any URL other than the UD URL. So, this is the validation that we have added. Let's see if this validation works. To do that or to check that, we go to the site again and I will try to hide the content over here. Let's say the content would be trailer of the movie soul. I hide a URL anything other than the UD URL. Let's say the URL is google.com slash radio1 and I hide the something. And let's hit the save button now. So, we got an error. It says the link is invalid and you are supposed to hide UD URL something. So, this is what is expected as per the code we have written. Now, let's try to do the same thing again. But let's try to do this using a rest API. Let's try to create the content using this API. Now, to give you a heads up about the backend information, I have added four modules over here from the code. I have limited all the rest modules and I have also nominated as UI modeling. That's how I am working with the rest APIs to show you the demo. I have tried to create the content now. I go to postman. We will not go to details about this. How I have set up and everything. I am trying to create the content over here. The title will be movie trailer. Then the title will be solely. I am entering the video URL as this. Now, please note that this is a non-YouTube URL and I am entering the video summary. Let me hit the save button now. Or the send button over here. I hit on send. If you observe carefully, we got the response from the server. Here we got the node ID. Now, this is not as desired. This is not what we wanted actually. Entering the non-YouTube URL. This is not what the business wants and there should have been an error. But we will not get an error because we have added the validation on the form. But the user is trying to create the content through this API. Now, let's go to the disadvantage. This is basically a disadvantage. When you add a validation through form order, you get your form validated only. And anything or any other mode of node creation will still be able to create or bypass the validation. In our case, we were able to bypass the validation through REST API. So, this is the disadvantage and this is something, you know. This is a very big one. Let's go ahead. In this paper question for the... I would like to introduce myself. Please, certainly skip this particular part. So, I am Kunal Ruseja. I am working with Aizalind as a Drupal engineer. And I am based out of Mumbai, India. I have been working with Drupal since last 7 and a half years. And I have mainly been working with Drupal talking and sometimes friend and group. And these are some places where you can reach out to me. Yeah. So, this is about me. And... Win for further. Now, we saw the disadvantage about forms, or the form validations. We had this problem in Drupal 7, but Drupal 8 certainly saw this problem using or introducing a symphony-validator component. And we can write validation once using symphony-validator component. And we can get our forms or entities validated without repeating our code. Like in Drupal 7, we wrote the validation for forms. If we wanted to validate our entities when they were created using the REST API, then we had to write validations for the REST API as well. That was like repeating your validations at multiple places. But using this, this is like writing your validation once and using them at multiple places. Now, to utilize this particular component, we should know two main concepts, like how you can create or use this particular component in Drupal. So, the very first concept is there is a class called as constraint. Now, it is a class from symphony-validator component. Using this class, you have to define rules. That rules can be like, is my trade null? Is it true? Is that a UFO? And a very important one, etc. So, you can write multiple rules over here using the constraint class. And symphony-validator component says after defining the constraints, you have to write constraint-validator class as well. So, this particular class constraint-validator, it is going to contain all the logic, all the backend-validation logic to support your constraints. So, if you have a constraint called as is that UFO? You are going to have a class or the constraint-validator class for that particular constraint. Now, this is a given theory. Part of it, you will see all this in action when we see the demo and the code. So, I move forward. Now, one thing to note over here is constraints and the constraint-validators. These are both the classes provided by symphony-validator component. And Rupal recognizes them only when the developers create plugins for it. So, in Rupal, the integration part of the integration between Rupal and symphony-validator is that you have to define constraint and constraint-validator as plugins, out of which your constraint class should be annotated. So, if your class has an annotation of constraint, then Rupal will recognize your class as a constraint. Now, proceeding further, I'll show you a demo. This is a very high-level summary of the demo. And the step one is, we'll be creating constraint classes and the constraint-validator classes followed by the step two, where we will be adding these constraints or the validation constraints to entity fields or the entities. And the step three is where you fill out about, after seeing the app. So, let's go to the demo. So, I will come out of the slides and I'll go to the editor. I'll simply comment this first. So, I am commenting the form validations. I have commented the form validations now. If you see in the demo for you, we have a sRC folder, then we have a learning folder followed by validation and constraint. Now, this is the place, or this is the learning space where your constraint and constraint classes are going to be. In the very first class, I have defined a new link. Now, this is a constraint class. You can recognize them and extend the constraint class over there. So, I have a class for new queue link. It extends the class constraint. And you can see that they are going to live in the name spaces, which is Drupal slash module name slash clubbing validator constraint. And this class should be advocated. The annotation is this, which says, Hydrate constraint, which accepts two parameters. The first one is ID. This is our constraint ID. This should be a unique ID for the constraint. In our case, it is YouTube link. This is exactly same as your class name, as well, and the file name. Followed by the label parameter. It accepts a label. The label is YouTube link and the context. Context can be any text. In our case, it is validation. Now, in this class, we define a property called as invalid link error, which says, value is invalid. Please enter YouTube URL. So, this is the constraint that we have created. After defining the constraint, we have defined a constraint validator class. Now, in our case, we have defined a constraint as YouTube link. And our constraint validator class is YouTube link validator. I am opening this file. So, there is a prerequisite over here that Drupal expects from you that you name your classes as constraint name, followed by the text, validator. So, if you so see over here, this is our constraint name and this is the validator. So, this is the prerequisite for defining validator classes. If you want to have some different demo here, then in that case, you have to go to your constraint constraint class and add a function over here where you have to overwrite that particular validator class. Now, that function name is for that you have to go to constraint class and there is a function called as validator byte. If you write this particular function in your constraint class, you can overwrite the file name that you want to write for the validator. So, I close this. They are coming back to the review validator class. Now, as you can see, it extends a class called as constraint validator. Now, this is a symphony class and this particular validator class is in the same name space as the constraint. Now, there is a binary function over here which is called as validator. Now, this is where your whole validation logic goes. If you remember earlier, we said the live constraints are there to define the rules and the actual validation logic goes in the constraint validators. So, this is the validation logic I have written. I will come back to this later on. Before that, we go to the model file. So, contain validation.modules. In this file, I have added a hook called as hook entity bundle created for the file. Now, if you saw the step 2 earlier, when we described the high-level demo summary, we said that we are going to attach our constraints to the fields or the entities. Now, using this particular hook, I am going to attach the validation or the constraint to the field. So, I uncomment this. Let me just quickly clear the cache and I will explain you the code in that. So, in this setting, if the entity type is node followed by the bundle check that the bundle should be moving to here and the field, you will see the URL should be there in this bundle. When all of these are set, we have to add a constraint to the field and the constraint name is youtube link. Now, this is the constraint that we have created. So, this is how you attach your constraints to the fields. Let's go to the youtube barricade class. In this class, the value function will accept the value and the value is going to be the value suffocate by the user. This is the free data or the data suffocate by the user. This is how we are extracting the link from the free data and we are passing it to a helper function where we are checking if the link is the youtube link and the helper function is basically doing the same thing. It is looking for the text youtube in the link. If the text is present, it is returning to otherwise forms and based on that we have made a check over here. If the validation fails, then we are adding a violation for this particular constraint. If you see this we have added the violation and we need to add the violation using this particular where we are accessing the property invalid link layer. This property was defined earlier in the youtube link class you see over here. We have defined this property and we are passing the value over here and value is going to be the link that the user has submitted. This is how we have defined the constraint and constraint classes. Let's go to the browser and see if the validations are working or not. I will simply go to the just a moment get the new page. We are on the home page and I will go to the add content page of the movie trailer node. I will add the mandatory data and I will add a URL which is a non-mubule URL and I will simply click on the save. If you see we have the validation working and this is not coming from the form order. If you remember we had commented the form order code earlier. So this is coming from the constraint that we have defined and added to this particular field which says the validation that is the link is invalid and you have to delete it. So now there is nothing fancy about it. The same validation was working from the form order as well. But let's go to the rest area. For the rest I will try to create this particular node through rest area. I have the exact same data. I will just simply make a change to title. So this is an intermediate and I will simply click on the send button. Before sending this please note that this is an invalid URL. I will click on send. If you see over here we have got an error message which says entity validation failed and we have this particular text URL which says the field colon URL is invalid and please enter EP URLs only. So this is a very different thing when we use form orders we did not get this area but when we use entity validations through entity API that is using simply validate the component we have written validations once but they are getting called through any media that can try to create your content from. Okay this was hiding validations to fields. Now there is one more way through which you can hide validations to your whole entity instead of a entity. To do so I simply show you the code that I have written. I close this YouTube classes. I have written two more constraints one more constraint which is YouTube summary. Now this is a YouTube summary class. It extends the constraint class. It has an annotation that has the idea of YouTube summary. It is living in this particular name space which is same as the YouTube URL one or the YouTube name one and it has a property called as invalid summary error which says value is invalid. Summary there should be more than 10 characters. Okay now as we saw earlier for every constraint you have to find a constraint validator class is well I have defined a YouTube summary validator class. It is extending the constraint validator class which is a symphony class and it is living in the same name space as the constraint. Same thing over here as well. We have a validate function which is going to contain all the validation logic for other major business rules basically and we come back to this validate function later on but let's go to the modify first. So in this module we already saw how we attached our constraints to the feeds. Now I have added the constraint to the load entity overall using this particular hook which says hook entity type order. I will simply first recommend this hook and indicate the cache. Since this is the hook you have to clear the cache. Okay so in this hook I have written that all the entity types known I am adding a constraint called as YouTube summary. So this is how you attach your constraints to the entity types. I earlier guided for feeds. Let's go to the YouTube summary later class. In the validate function earlier when we attached the constraint to the feed we got the feed value but when we add a constraint to the entity you basically get the value as whole entity. So your value is nothing but your whole entity class. Now I have added a check over here which says does this entity has a feed video has a feed name if the feed is present then get me the value of it and check if the summary is invalid to make a check I am adding a helper function over here which says is invalid summary which accepts our summary and the summary is from the user input it is making a check if the string length is less than 10 characters written through otherwise pops and when the summary is invalid we are basically adding the bytes over here and this violation is added exactly same way as the array for the YouTube cake so we are adding the constraint followed by the constraint property which is in this case invalid summary error and it accepts an argument for this value which is which is nothing but the summary value submitted by the user. Let's see this action let's go to the node add page we are on the node add page I'll add the data I'll add the YouTube URL for now so that we don't have this violation error and I'll add the summary less than 10 characters and I'll click on the save if you see this we have received an error 1,2,3,4,5 is invalid and summary less than should be more than 10 characters let's see if this thing works from the press API as well or not I'll simply go to the rest API we are adding let's say a new trailer and this is YouTube link but let's enter the invalid summary the data has been entered and I'll simply click on the same button now if you see over here we have got a validation error message which says your input is invalid and summary should be less than 10 characters so coming back this is a very important change from what we used to work in Drupal 7 we used to write all the big forms all the code for the subcode for any sort of press API view so you repeated your validations and added the validations at many places but using the constraint symphony sym with this particular symphony component you can write your validation once and use it at many places so this is certainly an argument for me it was an argument for me with a benign on it and if you have any questions please share them any questions sir? shall we move the test lower down the stack there for capturing everything no I think that's very useful we don't see no questions there thanks very much hello looks like we don't have any questions here thank you very much ok thank you thank you