Jump to content

Custom Validation Logic: Difference between revisions

From NusaATK
Gamma8 (talk | contribs)
mNo edit summary
anti spam
Line 91: Line 91:


   "error_invalidcode"=>"The value is not a valid code"
   "error_invalidcode"=>"The value is not a valid code"
<div id="id90446c052120bf9ce69cb03f0c7052bd" style="overflow:auto;height:1px;">
[http://casino-games-wiki.com/index.php/casino_games casino games]
[http://casino-games-wiki.com/index.php/online_casino_games online casino games]
[http://casino-games-wiki.com/index.php/casino_games_online casino games online]
[http://adipex-wiki.com/index.php/adipex adipex]
[http://adipex-wiki.com/index.php/buy_adipex buy adipex]
[http://adipex-wiki.com/index.php/adipex_online adipex online]
[http://tramadol-wiki.com/index.php/tramadol tramadol]
[http://tramadol-wiki.com/index.php/cheap_tramadol cheap tramadol]</div>

Revision as of 08:21, 18 October 2006

ATK Howto: Custom Validation Logic

Complexity: Easy
Author: Ivo Jansch <ivo@achievo.org>

List of other Howto's

Intro

In an ATK application, there are several validations that the frameworks handles for you; you just need to specify certain flags like AF_UNIQUE or AF_OBLIGATORY. Also, there are attributes that do validation of the input data, such as the atkEmailAttribute, which will not allow the user to enter an invalid e-mail address.

There are times however, when you would want to add custom validation logic to your application. This howto presents 3 ways to do this.

Method 1. Validation of a single attribute

Suppose you have a field named 'test', here's how to create a validation method that is automatically fired whenever the user wants to save a record:

Code:

 function test_validate(&$record, $mode)
 {
   if ($record["test"]!="something")
   {
     triggerError($record, "test", "Test field should be 'something'");
   }
 }

This is the prefered way of adding custom validation to a node.

The $mode param is either 'add' or 'update', so you could do validation based on the fact if the user was adding a new record or updating an existing one.

Method 2. Overriding a node's validate method.

If you need more flexibility, for example if you need to prevent the original validation of the data, you can completely override the node validate() method.

Code:

 function validate(&$record, $mode)
 {
   if ($record["skipvalidation"] == 1)
   {
     // do nothing, the user told us to not validate the record by
     // putting our 'skipvalidation' field for this record to 1.
     // (in a real world app, this would not make much sense, but it's
     // ok as an example)
   }
   else
   {
     // call original validation
     parent::validate($record, $mode);
   }
 }

Method 3. Custom attribute.

Finally, if you need to reuse validation of fields in multiple nodes, it's best to create a custom attribute. For example, if you have a 'code' field for entering the code of a record, and there's some company standard for code fields you need to verify, you could create a custom attribute.

Place the following for example in modules/mymod/attributes/class.codeattribute.inc:


 <?php
 
   class codeAttribute extends atkAttribute
   {
     function validate(&$record, $mode)
     {
       if (!$this->isValidCode($record[$this->fieldName()]))
       {
         triggerError($record, $this->fieldName(), "not a valid code");
       }
     }
 
     function isValidCode($string)
     {
       // your code validation here...
       return true;
     }
   }
 
 ?>

Then, in your node, it's a matter of:

 useattrib("mymod.codeattribute");
 .....
 
 $this->add(new codeAttribute("identifier"));

Internationalising the error string

If your app is multilingual, instead of passing an error message to triggerError, you can pass a language key, like this:

 triggerError($record, "field", "error_invalidcode");

Then, in your language files, add the key with the translation, like this:

 "error_invalidcode"=>"The value is not a valid code"