Skip to content

081 Easy Validation Rules for Fields in JCB

Amigo edited this page Oct 24, 2019 · 1 revision

Easy Validation Rules for Fields in JCB

I would like to demonstrate a quick Way to add a validation rule to a field I just now What's the validation rules Video or 00:00:12 Tutorial And realzsed wow that's out dated very much so Since now there already is actually a validation rules area In JCB Which really makes it extremely easy to add any type of validation rule Which You would like to do use in your Fields So Just again what does a validation rule do 00:00:36 Well when you submit a form there are fields in that form And Server side You may want to do some validation on that Specific value In that field And Validation rule really takes care of that Basically checks the value Whether it is 00:00:56 The correct value And then If it Isn't you can Pass a pass a Basic return false And if it's correct you Return true which then sets The field to continue on saving 00:01:12 And be stored into the Database The field Validation rules can also be used for other concepts But I won't be explaining much of that now I'll just show you how to set it up and it's Really easy Ok so First of course you need to understand where to add them so I'm gonna do grab any Type of field here let's let's take this name field it's very much used All over 00:01:41 so I'm gonna open the name field And and you will see that by Default There are already a set of validation rules That are created Now There is A validation rule called exists And There is A validation rule called 00:02:04 Unique field And some of this is Things that we've made Here in this Joomla install And others are part of the Joomla default validation rules So how to create a custom one with be added to click here create New validation rule Which should then Give you this little heads up and if you click ok 00:02:28 Takes you to this a new validation rule area And it's gonna say you must add a unique value rule name Which is really what you need to put into this area Here So you can say my validation Which is unique enough and will tell you here That that rule is fine Now Most the time you wanna also leave yourself a little note say This 00:02:55 Was why for reasons to understand the purpose of the validation rule But you may also now You may not know what is the PHP that you need to add here What is the How does a validation rule really look So all those default validation rules That already exist in Joomla are listed here and this is just a selection thing This value doesn't get stored 00:03:25 So it's just that you can select one and basically To use it as a base from which to start Or just to look at the different ways that it's Done so let's say we use the email one It basically loads the Joomlas default implementation or validating an email now you can improve on this and call this my email validation And that kind of thing And then use this actual validation So Let's say we gonna do 00:03:56 Not this Let's Let's Let's choose another option Let's say Exist Ok let's look at this So It's going to You'll have to obviously Read this read 00:04:12 This PHP here And Check That this is exactly what you want I'm not gonna explain this that's not what the tutorial is about You need to know your PHP coming here And then basically write out what you want here obviously They are Even ways to to go reach out to other datasets in 00:04:37 Do validation This could be a very exciting place To do some tweaks And then you would basically click Save and close which will then take you back to the name field Where We Were And if we now scroll down We will now see in this list A new validation rule That's why That's the 00:05:01 1 we just made And if I grab that validation with a just a Control See on on the PC You Can then Scroll up here see is there already and validation rule in place for this field no If you click on the plus of these 00:05:21 There should be A valid date option And you can then this paste this validation rule in there So at the moment This This field is used Like oh too many time times too many places but I'm gonna just use this as an example So it it's gonna It's gonna still behave the same way doesn't matter that 00:05:48 it is used so many places Ok I'm gonna save and close out here and I'm just gonna compile Any of these components that actually Use That field for example let's Let's do component Builder That should work I'm just gonna compile this Ok I've downloaded the package so let's go look inside that package To see what was done 00:06:18 Ok so we go to admin and models And then there is a rules folder we opened that and we see it has a file in here now called my validation And let's open that And here in my validation It's basically extended the form rule And it's added that HTML Sorry PHP 00:06:42 That we wrote basically this chunk over Here It is added it to the file for us Now This my validation Must also now be added to the field right so let's look at that So we go out of this Then we go to forms And I'm just gonna open this 00:07:05 Custom admin View XML Just any One which I know has a name field in it And I scroll down and here it is Name field And there is my validation Now you'll see at the top of this file It is actually pointing To the correct rule folder 00:07:26 So that it knows that if the rule is not default to Joomla It should come and look here To find it and so JCB has moved that rule into The correct Place already Now let's quickly Look at I just another little Small pointer here And that is the package has a config file And if we open the config file We see that it already has that default rule path 00:07:56 As well set in the config file So That means that Even if you were to use that field In the configurations of our component it already know Where to go look For that component rule Sorry that field rule Ok so that was what it did it We basically created the rule 00:08:17 And once we have created it We simply added to a field And JCB automatically Grab that code created that file Added that code to the file and everything should just work Out of the box Well that's how the new validation rule implementation Of JCB Actually works I trust this would be 00:08:41 Informative enough In helping you all Using this validation rule area Like I said before There there is a way to get to this area via the field But is as you can see here is direct access Just clicking on the validation rules And then there is my validation Here is some of the other validations 00:09:05 That we've made custom ones That a That is also in my Project which is being used So The my validation is the one we did And anything that we put in here It's basically gonna add up at Be added as class methods to that file Now 00:09:25 You can always come back and make some tweaks to it And every where with this validation rule is used Those field will automatically be updated with the changes wants to get compiled again Ok well thanks for watching and enjoy.

Clone this wiki locally