-
-
Notifications
You must be signed in to change notification settings - Fork 176
045 Adding your own rule validation to a field in JCB
00:00:00 (Click on these time links to see Youtube video)
I would like to demonstrate how to add rules to the Model, basically to a Field. The custom rules are used to test a value before submitting it to the database. This is quite a good feature to have in your Component. 00:00:32 For this demonstration the Hello World Component is going to be used and a rule is going to be added to one of its Fields. Open Greeting (first) as the Admin View. To change the Field in it this(See video) 'Greetings' are used. 00:01:11 The Field attributes do not have the validation option switch in place. We might start shipping JCB with this new validation property which is not new, it has just not been added yet. If you have a version of JCB that does not have it set, then whatever Fieldtype which is selected and can be edited by going to the green button, or you could edit the Fieldtype by going to Fieldtypes. 00:01:47 Close out of Edit the Admin View again then go to Fieldtypes in Admin View.
A Fieldtype may be selected to add this validation too, for example, the Textarea. It has a filter in place. A validation is added just below it. Click a plus. It is 'adjustable', it is not 'required' and it is not 'translatable'. Then give it a little description. 00:02:22 We got it all set. A value would not be added because if you do not want to use it, leave it empty. Save and close. With the Textarea been done, go back to the Admin View Greetings and again open that Greetings(Textarea) field. Now, this 'validate' property is available to us and can be copied and added in the XML field definition.
Now we want to have a Custom validation here. There are built-in Joomla validations as well. But you can also set up a Custom validation rule and this is known as 'form rule'. This is what we want to demonstrate. We are going to call our rule 'strlenTen'. The string must be longer than 10 characters. 00:03:36 I am going to copy this 'strlenTen' again. Save and close. So now that the validation is set. JCB does not have an area for you to write the rule and have it included. So you have to write the file yourself, and put it into the custom folder of JCB and include it in the component that it can place it in the correct place.
JCB creates a folder called 'rules' and in the forms every XML has a field path and a rule path so you can add a rule to this folder(models/rules')
00:04:41 (Please follow on video)
First, create a rule. This rule 'JFormRule' is added. In the folder structure, in the Admin area, Components, Component Builder, and there is this folder called 'Custom'. In this folder, I have created 'strlenTen' file and placed this code here, it is a basic JFormRule. Now that our file is in place, it should be added to the component. Go to the components and open Hello World (v2) component. 00:06:03 Go to Settings and there at 'Adding Custom Files and Folders, click on 'Create component files and folders for this Joomla Component'. Click on that, and 'OK'. In 'A New Component Files & Folders' we want to add one file. Since we have added it to that custom folder, everything in at custom folder will come up as a file that you can select. So there is the 'strlenTen' file. Now to show the path that leads to the place where we want it to place the file.
In the back end of the component, go to models, forms, and this greeting form is targeted. It has '/models/rules'. Copy that. Remember that it is the Admin area and then '/models/rules'. 00:07:07 This Admin is the way the package is build. The target place should be the place inside the package. This is always the case for both Folders and Fields. In the Folder you want it to add it to the Admin models rules folder. Then you need to decide whether you want this file to be updated. At this stage that files does not need any updating.<<<<<<<<
But we can change that. You can see I've just added this new licence tag to it(see video). And I've added this ###BOM###. When the file gets taken it will take this part away and replace this with your components licence and so the file will become branded like every other file in your component. 00:08:06 Even if you use it in areas or components other than the one you originally made it for, it will dynamically looked like it was made for it because it add that components data at the header of the file. Even if you had the components name somewhere else in this, you can replace it with the well-known ###component### and it will be placed with the component name. We've got our file in place. You wanted to be updated. Let's compile the component and see it in action. In 'A New Component Files & Folders' just tick this as yes that the file should be updated. 00:08:54 Then save and close. Close out of the Component again. There's a shortcut to that file area underneath hello_world that you can use. We go to the Compiler and we compile our component. Let's install our Component. Now let's go look at the code to see what happened. In our components admin area, if we open the rules, we see there is the file 'strlenTen', it was placed into rules folder. If we open 'strlenTen' file we see 00:09:39 it added our copy notice to the header of the file. Let's also look at our greeting.xml. We see the validation is there strlen.Ten so Everything so far so good.
Now let's go test this. We open the component in the back end. We go to Greetings. We click New. As you remember we set that Greetings needs to have a length of at least 10 characters. Let's test this. We add Greeting which isn't enough and we click Save. It tells us Invalid Field: Greetings. Problem. 00:10:36 There's one little change I had to make to get this to work and I realize I'll need to tell you. Let's go look at this for some reason they want this file all lowercase. II had to change strlen.Ten to the T also in lowercase. Let's go do this also with the file in our custom folder. Then inside of the actual file they also want the T here to be lowercase. I suppose because they are using the upper case camel rule to sort of break-up the name. That was what I had to do to get it to work. 00:11:20 I also need to update the component to include this file since the name is changed, it will no longer know where it is. We can go directly to that file area with thhowe shortcut underneath hello_world and we see it is no longer selected, so we scroll down, select it again and save and close. Now that we've fixed the little glitch which I created myself, let's go back to see this again in action. 00:11:57 And save. And Invalid field: Greetings. That isn't very helpful. Maybe you would like to also show a custom message so that the user would at least know more about what is going on here. Before we do that let's add enough values and see if that it actually saves. It's saved successfully. Close. Our Greetings is been saved and our value here(Greetings) is more than 10 characters, so it was valid.
Now let's go add a custom message to our validation. Remember whatever you do to the live version of this file, will not necessarily become part of the next compilation. Adding a custom message you need to go back to component builder, custom, and to that actual file(strlenten.php) and add it in there(see video) then recompile your component and it will automatically update the live demonstration. The other way to test before doing it live or before adding it to the core of your component, is to go to the live file which as we saw, is 00:13:17 in com_hello_world, models, rules, strlenten.php. Now let's add our custom message in here just to test. We using the element->addAttribute to the element message: Field must have more than 10 characters! That is one way of doing this of course there is another way.
Again this takes us back to JCB and we go to the Fields. We open Greetings and we look for the attribute message. It doesn't have message. We need to add this attribute. We'd click on Field Type and we scroll down. Let's place this under Description. We say Message. It's changeable, 00:14:21 it is translatable and it's optional. We don't make it compulsory. We got all set. Save and close. Let's add this message in Field Information to our XML field. Before we save this and go this path, let's first test our live 00:15:00 change we made to the file and see if that also works. In the file we added attribute to the element and it return false. Good, let's go test this. We again open Name item and we take away a few characters and click save. We get a Warning message that it needs more than 10 characters. That option also works very well. 00:15:34
Now lets use the other one which I suppose is more convenient, unless you are checking multiple things and you would like to give multiple error messages based on those results then this implementation is the better option. But if you just got one message and it just needs to explain to the user one issue, then using the XML path I suppose is more convenient. In our custom file we haven't added that change. Let's go and compile our component and install. First we need to save and close out of this Field that the changes be captured. Then go to the Compiler 00:16:20 and compile our component, install. Lets go look at the code. In the code we see our custom message is again not showing up. The greeting.xml we now have this translated message='COM_HELLO_WOLRD_GREETING_GREETING_MESSAGES'. Let's go test this. Again we open Name Field and first save it the way it is. 00:16:56 Lets take away two characters and save again. We get the expected message. This time around that message can be translated and as well it is in the XML file, it's not part of the actual function. We have now successfully added our own validation rule to one field within a view and we were able to include that rule into our component on compilation using adding custom files.
- Home
- Beta Testing
- Custom Code
- PHP Settings
- Demo Component
-
Tutorials
- Hello World JCB
- Intro JCB Guide
- JCB Installation Steps
- Planning Components
- Field Type Overview
- Basic Fields Creation
- Admin View Management
- Advanced Field Usage
- Admin Component Integration
- Component Setting Customization
- Scripting Components
- Component FTP Options
- Dynamic Get Method
- Site View DynamicGet
- Site View Templates
- Template Setup Guide
- Layout Configuration Steps
- Custom Admin Management
- Adding Site Views
- Custom Admin Integration
- MySQL Demo Tweaking
- Global JCB Settings
- Custom Time Field
- User Helper Integration
- Email Helper Usage
- Message Store Email
- List View Unescape
- Export Import Customization
- Overwrite Custom Fields
- List Field Filtering
- Automatic Code Import
- Manual Code Implementation
- Component Export Import
- Custom Admin Buttons
- Translation Management
- Site View Permissions
- Component SQL Updates
- Site Edit Configuration
- JCB Backup System
- Helper Structure Integration
- JCB v2.5 Upgrade
- Tab Setup Guide
- JCB v2.6 Release
- Extended HelloWorld
- Field Rule Validation
- Community Snippets Intro
- Snippet Forking Tutorial
- Pull Request Snippets
- Library Manager Area
- Excel-based Translation
- Dynamic Router Details
- Database Auto Updates
- Subform Quick Demo
- VDM Package Import
- Dynamic File Inclusion
- File Field Upload
- Drag-n-Drop Upload
- Quick HelloWorld JCB
- Non-database Fields
- Dashboard Customization
- Menu Prefix Toggle
- Community JCB Packages
- Collaborative JCB Workflow
- JCB Package Install
- JCB JAB18 Event
- Convenient New Fields
- Component Language Strings
- Library Functionality Anticipation
- Join Field Relations
- License Template Change
- Code Reusability
- Local Dev Environment
- Extended Field Types
- Joomla Custom Fields
- Custom Field Expansion
- Site View Listing
- Run Expansion Method
- Form Site View
- Field URL Update
- Additional Helper Methods
- Field Validation Rules
- New Placeholder Feature
- Component Config Params
- Per-field Default Values