Submind YouTube summaries
Thumbnail for Decoupling Entity-validations from Entity-forms - kunalkursija

Decoupling Entity-validations from Entity-forms - kunalkursija

Watch on YouTube

Video summary

The video addresses a common architectural challenge in Drupal development where entity validations are tightly coupled with form definitions, often leading to code duplication and maintenance issues. The speaker demonstrates this problem using a standard profile installation of Drupal 8 featuring an Entity called "Movie Trailer" that includes fields for titles and URLs. By adding custom validation logic directly into the form layer—such as restricting URL inputs or enforcing character counts—the developer creates specific error messages like "link is invalid." However, this approach fails when attempting to create content via REST APIs because the API bypasses the traditional form submission process entirely. Consequently, valid data submitted through an API call returns a generic validation failure rather than the specific business logic errors intended for that field, highlighting the disconnect between how forms and non-form interfaces handle constraints. To resolve this decoupling issue, the presenter introduces a new methodology using Symfony components within Drupal 8 to define reusable constraint classes separate from any specific form or entity type. This approach involves creating dedicated "Constraint" classes that encapsulate all validation logic, such as checking if a video URL exists or ensuring text length exceeds ten characters. These constraints are then registered with the system by defining corresponding validator classes and mapping them through an annotation-based configuration rather than hardcoding rules into forms. By separating the definition of these business rules from their application layer, developers can ensure that validation logic remains consistent whether content is created via a user interface or programmatically through REST services, eliminating the need to write duplicate code for different entry points. The implementation process involves defining constraint classes with unique identifiers and specific error messages, which are then attached to entity fields using annotations in the form definition file. The speaker walks through modifying existing files to inject these constraints into the validation chain, showing how the system now recognizes rules defined outside of forms. When a user or an API client submits data that violates these independent constraints—such as entering a short string where a long one is required—the application correctly intercepts the input and returns the precise error message associated with that specific rule. This unified handling ensures that validation errors are not lost when switching between different creation methods, providing a robust and maintainable solution for complex entity management scenarios in Drupal projects.
Read the full video transcript
[Music] laughs stop it [Music] or satisfied not a big reader so a big time soon so coming to today's topic and so by editions since the woods since the reading envoy invite with variations to forms I mean they have been a very it was a very dangerous part of the adventure I mean found remain a video so who are the validations and improving the formal relations or though the the reason for my relations are people in although many have worked with them is by using these particles may be Drupal 7 or movie 8 we have been doing this piece I think of valuing the book or the form wider hook and adding the custom buying it kinda that's how we all have been doing custom variations on the form offs or the entity box but if you have worked it was leave it there you will know that there is a design - a vintage at my inators using the for my pedals and identifications now I simply walk you through the disadvantage to them so that we just and above the double side first I have a little movie eight point eight point three question of faith but this is a standard 2d profile installation and I have created a Content ID called a movie later and it has two fields in it about comparing the title we have so we do somebody V which is my text a no fee and we have a big Lily were made and what is being free if you edit this particular scene you will that is an imprint you wait no this time this thing is going to accept external links on you and I have a late entry soil that says you on this week except you you you you is me you come out of it go to the I content page of the only traitor now as you can see over here sister create content age of the type movie trailer it has three leaves that went really well and feed yourself without you for baby the content over here that we take you the code editor so I have created a model and that customer Dean is called as content validation this money is already in every and it has in 545 and some more files for this particular on demo I first go to the modify and show you that I have written to hide a violation on the link field so I have written this particular book it is going to us football baseball I need water now this will basically take the no time and if we have a unit handle our customary 800 is this which in turn basically knows for the P divided by Q in the submitting data and infinite check if the submit anyone that means that gets you d if the text is not present then it is going to throw me when and of saying like this particular of e1 is invalid and you are supposed to hate that he knew you is money so this is the error you get if you can try to enter anyone other than Joey T so this is the value student yeah I mean let's see if this validation voice don't do that order to check that we go to the cyber name and I will try to I either than and over here let's say that and then would be fatal movie so I I early well and in other than the URL is Radio 1 and highlight some and let's get the same by the now okay so we gotta head up it says on the link is invalid and you were supposed right you did you ask me it is his fourth in assists a game as for the won't be audited now let's try to do the same in money but let's try to do this using rest India let's try to create the content in this together now in the you have all of the information I have I ate four more names over here on the board I haven't even done the rest modules and I've also nominated Sui one minute on that so I am working with the rest 80s to show you the demo I try to create the content now I know the postman you will not work I am trying to create the content earlier that type will be movie later then the iDVD sewing and bring the video URL s is no please don't type this is not in DUI and I am underneath the video surveillance let me take the same but now for the stand but in a way I keep on saying if you observe carefully we got their stance on the solo there we got the know right now this is not as desire this is not what we wanted actually and everything you not need you to wear it I mean this is not what the business wants and there should have been an Terror but we not get in because we have I hate your valuation on the fall but user is trying to create the content to a jail now let's go to the disadvantage so this point is the validation - oh my god you get your money you owe me and anything or any other mode of no variation then still be able to create on bypass the valuations in our case we were able skip into my first evaluation so that's in there so with this append is a package and this is something you know this is many people let's the world okay this before proceeding further I would like to introduce myself he's certain escape this potato pie so I am NOT a procedure I am working with isolating either engineer and I am based out of Mumbai India I have been working it on the world since last seven and a half years and I have mainly devoted to way back and some time spending and these are some cases they even reach out to me so is about me and 24 other now we saw that I said my niche about forms or the form allegations but we had this problem in group inside but no belated certainly so it is problem using a noticing symphony on weekend right valuation was using simply value the components and we can better inform so a TDS validated without repeating of it for like in 2007 video of the valuation for Falls if you wanted to evaluate our entities when they were created using the statement and we have to write validations for the rest areas with that was like repeating your valuation sigh 20 places for using this this is like writing your variation ones and feels intimate way to eat this now this particular component these are two main concepts like how you can create for use this particular component a through bill the very first concept is but there is a class boy in distance range now it is a class strong Symphony by a gator component using this us you have to define rules by promise can be life is my field none is it to open that way and the very important one etcetera so even you can make multiple tours over here using the best-trained class and simple evaluate the component States after defining the constraints you have to write constraint parameter class as well so this particular class constraint parameter it is going to contain all the logic on the package valuation logic to support your constraints so if you have a constraint point as is like you fo you are going to have class or the constraint finding it a class for - particular constraint now this is a even seem all this in action when is it again so I football now one thing to note away it is constraints and restraint validators these four these are what the classes are providing best behavior component and throughway recognizes them only even though when delta force feed ready for it so in Drupal the inclination part of the indication between group fitness symphony my leader is that you have to be fine constrain different parameters as planets out of which your constraint plus should be and really so if your class has an animation of constraint then you will recognize your now proceeding further I should do it in this is a very high-level summary of demo the step one is in deviating constraint classes and the constraint valuated classes for you better step two we will be adding these constraints for the validation that strange to many DB's all the fifties and the spec tree is very clear out about of Athena let's talk about the slides and I go to the editor I'll simply comment this first also I am commenting the four medications I so I make the combinations now if you see no yeah then you have to follow that ideation and constrain now this is so please on this is eternity in space during your fence paintings and stained glass is done between the very first define history link now this is a constraint cuts and you can't recognize them when they extend the glass holder so I have a class for you doing it extends the class constraint and you can see the idea in the namespaces which is the wind / model name / the mean value inter constraint and this class should be advocating damnation is this will say side very fast train which accepts two parameters for the first one is ID this is our constraint ID now this is should this should be when unique ID for the constraint you know what is it is you do gain this is exactly same as your classmate message and a fight followed by the label it accepts a latent ability to gain and the markets now context can be anything any any text in our case it is variation now in this class we are basically defining a property called as embodied in Vince's value is invalid please enter it you two are sweet so this is the constraint that we have created now after defining the constraint we have defined of constraint validated to us now in our case we have defined the constraint as made you blink and our constraint for editor classes you do it by later I am opening this file so so there is a prerequisite over here that the way expects from that in the in your classes as constraint ordered by the next valuator so if you saucy your way up this is of constraint name and sister body so this is the baby today for finding my best buddy the classes if you want to have some different demo here then in that is like to go to your vents pain contain bus and idle function over here where you have to override that particular political traction on that function name is for that you have to go through constraint class and this is a function point x value you get back if you like this but in your function in your constraint class you can overwrite the file name that you want to write for the body notices coming back to the EU value plus as you can see it extends a class called as strain value this is a mini plus and this particular in the same space as the first rate now this is a mind any function over here which is going to validate this is where both validation logic goes remember the same realize that strange or get we defined the rules and actual validation and only goes into husband is so this is the validation was in traffic and I'll come back to this later before that we go to the modified so again by nationhood one is in this file I have added book called s and open T but then created provider now if you saw the step two earlier when we respect the tile every them somebody say that you are going to attach our constraints to the keys all the degrees now using this particular book I am going to attach the validation for the constraint mundo field so I uncommon this it Mises will get here the cash so in this particular function I am checking if the any type is known for them by the vibration type the one mentioned we move in together and the field in video is from either in this one and then all of these are safe we have to either explain to the people and the constraint name is if you link now this is the constraint that we have created so this is how you attach your constraints to delete this load2learn embodied in the truss in this class the valued function will accept the value and the value is going to be the values of the gate items this is the field data or the data submitted by the user and this is obvious writing the name from the 3d data and we are asking it to end the function function is doing the same thing it is to be further test you do in the ink if the text is present it is returning to other response and seminar based on that we have made a check over here if the validation thing is then you are adding a violation for this particular strain if you see this yeah I ain't avoid vibration and we need to hide the violation I using this particular there we are accessing the property environment where this property deforested finds earlier in the univalent class you see where we have defined this property and we are passing the value over and value is going to be technique like they use a recipe so this is how we have defined the vanish the constrain interesting process now let's go to the browser and if the valuations are working so I think people okay so you have a tonight go through the content age of the movie later on I am angry data and I are forever and which is a not interviewer or unless engaging on the same so if you see we have the valuation working and this is not coming from the form like that if you remember me I communicate the for my decoder here so this is something from the translate that we have in mind and I think this particular thing which says the validation that is the link is invalid and you have to so now there's nothing fancy about it on the same vibration was working from the comment that has been but let's go to the rest in here for the rest I'm back to being this particular going to rest in here I have to exact same data I just interchange to patent so this is any dummy data and I'm just simply click on the same button o before sending this please note that is is that invalid key while I click on the same okay if you see how I know we have got an error message which says any validation fail and we have this particular test over and says the field codon you are and so this is a very different thing onl when they move forward it does we not get this area but when we use any valuations to a DD area that is a using simply very determined we have the validations ones but they are getting point immediately try to be in your perfect calm this was I invitations rupees now this one move it to which you can hide valuations to your own and instead of opening a of a to know so I showing though or that I have written and wrote this into classes now I haven't into work the constraints of sorry one more constraint which is you do summary now this is give somebody class it explains it explains the constraint plus it has a animation and the animation has the idea of giving somebody it is living in this particular namespace which is same as the Human Development League enable and it has of property kindness invited somebody this is value is invalid somebody that should be more than 10 characters now as we saw earlier for every constraint you have to define a constraint by later classes but I have to find anything so we might need a bus it is extending the strength value needed Plus which is for Symphony bus and it is giving in the same namespace as the constraint same thing or whatever said we have a valued function which is going to contain on the validation logic for the video instruments basically and we come back to this very defining a function later on but let's go to the horrifying first so in this model we already saw how I asked our constraints to the piece I have identified strange odors known entities over oil using this particular book which says hook and point and simply first and amend this code and it gave the - since is though you have to clear the cache okay so in this book I have been tied for the entry types no I am I enough strange boy s Union summit so this is Nadia dice your constraints to the any details I hope you're planning for peace let's go to the do somewhere by beta plus in the binding function earlier when he attached the constraint to the field we got the feeling body but and yet I get to constrain to that led you basically take the value is only a VD so you're back in something but your own and E Plus now I have I didn't check oil which says does this entity has a field video as a big thing we decide if if the Vedas present and get me no value of it and check if the summary is invalid to make a check I am hiding the function over here this is is invalid summary which exits over somebody and the sum is calm the only user input it is making a check if the string is this and in fact has written to the white spots and window somebody's invited we are basically adding the white and this foundation is again exactly same day as we added for the unique so we are adding the constraint followed by the constraint property which is in this case embody somebody there and again such an argument for it is value which is which is nothing but the summary values of the guitar the user okay let's see this English let's go to the note I wish I said we reload the page okay we are often what I faced I like doneita i hydrate in kindred and Phenom so that we don't have this by initiative and I chose somebody in the stand and character and I click on the scene you see this we have the same antenna which says your Tuesday but do people fight is inviting I'm somebody men should be more than 10 actors now let's see this if this thing's works from the REST API as well or not I'm simply go to Donna state yet the idea adding let's say unknown fellow on and this is you doing but let's enter the invalid sorry - in anger and I simply click on the same button now if you see over here we have whatever valuation error message this is your input is invalid and suddenly in 2010 cactus so yeah coming back this is very important change strong what is to walk into this Evan we used to read all the big bones and readable phones order of code for the vote for any sort of prescient gave you so you will be danger variations and I ate the variation that might be cases but using the constraint Seneca the different intercept any component on the right device ones and you need activity cases this is your moment for me [Applause] just just move the test lower than the stack now I think that's very useful we we don't see how many questions there thanks very much it's not time to have any questions here thank you very much okay thank you everyone