By default, PHPMaker will allow your users to input the blank keyword in Search Panel of the web application that generated by it, so the search results will return all records, even we have enabled the Requires Search Criteria from the Tables setup. For some conditions, we do not want to display all records and force users to input the certain keyword of the Basic Search in Search Panel. So, the short question is, how should we handle it?
This following trick will show you how to prevent users to input the blank keyword in Basic Search of Search Panel in websites that generated by PHPMaker. I also will show you how to force your web application users to input the minimum keyword. For example, in the following example, we will force your users to input at least three characters for the search keyword. In addition, this following trick also handles the possibility of users will input the empty string, even the empty string comply with the minimum length of search keyword. If we do not handle this possibility, then the cheating users will be able to display all records from the table.
This is very useful for those of you who want to create a web application which has the main or default page as the List page that contains the Search Panel, and you do not want your web application users displaying all records from the certain table.
Updated on November 29, 2012: This customization has been implemented in PHPMaker version 9.1.0, it matches to each other, and as a result, it works properly.
Updated on February 10, 2013: This customization has been implemented in PHPMaker version 9.2.0, it matches to each other, and as a result, it works properly.
[hidepost]
- Open your PHPMaker project (.pmp) file using PHPMaker application.
- Click on Tables icon in the toolbar, and then from the Database pane, click on Tables under the database name. Make sure you are only seeing now one tab named Tables in the right side of your PHPMaker application.
- Click on one of your desired tables from the Tables tab in the right pane of PHPMaker application (this will highlight the selected table), and then give checked mark at the Requires Search Criteria item that belongs to the selected table.
- Go to Database pane in the left side of your PHPMaker application, and then click on the same table name as you did in the previous step above. Make sure you have already seen now the three tabs appear in the right side of your PHPMaker application: Fields, Table, and Code (Server Events, Client Scripts and Custom Templates).
-
Now let's click on Code (Server Events, Client Scripts and Custom Templates) tab, and then expand or go to the following location: Server Events -> Table-Specific -> List Page -> Form_CustomValidate, aftewards, copy and paste the following code into the Form_CustomValidate function (Warning: this is only for PHPMaker version < 9.1.0):
global $Language; // If only contains space string, reject it! if (preg_match('/^\s+$/', $this->BasicSearchKeyword) == 1) { $CustomError = $Language->Phrase("KeywordIsEmpty"); return FALSE; } // If the keyword is too short! if ( (strlen(trim($this->BasicSearchKeyword)) > 0) && (strlen(trim($this->BasicSearchKeyword)) < 3) ) { $CustomError = $Language->Phrase("KeywordIsTooShort"); return FALSE; } else { return TRUE; }whereas if you are using PHPMaker version >= 9.1.0, then insert the following code:
global $Language; // If only contains space string, reject it! if (preg_match('/^\s+$/', $this->BasicSearch->Keyword) == 1) { $CustomError = $Language->Phrase("KeywordIsEmpty"); return FALSE; } // If the keyword is too short! if ( (strlen(trim($this->BasicSearch->Keyword)) > 0) && (strlen(trim($this->BasicSearch->Keyword)) < 3) ) { $CustomError = $Language->Phrase("KeywordIsTooShort"); return FALSE; } else { return TRUE; }Please note that in started from PHPMaker version 9.1.0, BasicSearchKeyword has been changed become: BasicSearch->Keyword.
As you can see from the code above, if users input the empty string, no matter how long the characters are, then your web application will reject it, and display the message to your users that inform them that the keyword is empty. In addition, if users input the keyword less than three characters, you web application also will reject it and display the message to your users that inform them that the keyword is too short.
-
The next step, we will add two new phrases in the .xml language file. To do this, open your C:\Program Files\PHPMaker 9\languages\english.xml file, and then find this code:
</global>
before the line of that code, please insert the following code:
<phrase id="KeywordIsEmpty" value="Keyword has not been entered!"/> <phrase id="KeywordIsTooShort" value="Keyword is too short!"/>
-
Do the same step as the previous step above for your another .xml language file. For example, I am also using Indonesian language for my web application, then I will add those phrases to my indonesian.xml file as following (adjust it with your another languages):
<phrase id="KeywordIsEmpty" value="Kata kunci belum dimasukkan!"/> <phrase id="KeywordIsTooShort" value="Kata kunci terlalu singkat!"/>
- Finally, re-generate your script files using PHPMaker as always, and enjoy the results.
[/hidepost]
Leave a Reply
You must be logged in to post a comment.