Jump to content

AtkDummyAttribute: Difference between revisions

From NusaATK
Sams90 (talk | contribs)
No edit summary
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
it is possible to use this attribute among other things to give a dynamic non edit field. a none dynamic string from other attributes should use [[atkParserAttribute]] instead.
{{attribute}}


for example:
The atkDummyAttribute can be used to add arbitrary text/html to a form. It's called 'dummy' because it's an attribute that is not persisted in the database in any way.


= Basic usage =
Using an atkDummyAttribute is fairly easy. Just add it to a node and pass the desired text as the second parameter.
<syntaxhighlight lang="php">
  $this->add(new atkDummyAttribute("explanation", "Enter your mother's maiden name in the below field only if your mother agrees"));
</syntaxhighlight>
Note that dummy attributes by default do not have a label (as its regular usecase normally doesn't require one). In the occasion that you do want the label to be visible, you can add the [[Attribute flags|attribute flag]] AF_DUMMY_SHOW_LABEL.
A slightly more sophisticated version of the atkDummyAttribute is the [[atkParserAttribute]]. It has the same api, but features the substitution of template variables in the text.
= Advanced usage =
If the contents of the dummy attribute depends on the currently edited record, and the [[atkParserAttribute]] isn't flexible enough, then you can use an atkDummyAttribute in combination with a [[display override]] to add arbitrary content to a screen.
In the below example, we use this principle to hide both the 'first_name' and 'last_name' columnts, and instead show a dummy attribute that provides a concatenated version of the full name.
<syntaxhighlight lang="php">
   $this->add(new atkAttribute("first_name", AF_HIDE_LIST|AF_FORCE_LOAD));
   $this->add(new atkAttribute("first_name", AF_HIDE_LIST|AF_FORCE_LOAD));
   $this->add(new atkAttribute("last_name", AF_HIDE_LIST|AF_FORCE_LOAD));
   $this->add(new atkAttribute("last_name", AF_HIDE_LIST|AF_FORCE_LOAD));
   $this->add(new atkDummyAttribute("name_citation"));
   $this->add(new atkDummyAttribute("name_citation"));


   function name_citation_display($record) {
   function name_citation_display($record)  
     if (strlen($record["last_name"]) <> 0) {
  {
     if (strlen($record["last_name"]) <> 0)  
    {
       $string = $record["last_name"] . ", ". $record["first_name"];
       $string = $record["last_name"] . ", ". $record["first_name"];
     } else {  
     }  
    else  
    {  
       $string = $record["first_name"];
       $string = $record["first_name"];
     }
     }
Line 16: Line 39:
     return $string;
     return $string;
   }
   }
</syntaxhighlight>
Note the AF_FORCE_LOAD flag; without it the fields won't be in the SQL query for this screen. Since we need them anyway in the display override, we apply AF_FORCE_LOAD to make sure the values are always loaded even if they are hidden.
= FAQ =


Note the AF_FORCE_LOAD without it the fields wont be in the returned result if AF_HIDE_LIST is used
== Where are the attribute labels? ==


from a table of:
The atkDummyAttribute defaults to no label.  If you wish to reinstate the label, configure it as follows:
{| border=1
 
|-
<syntaxhighlight lang="php">
| John
  public static function meta(atkMetaPolicy $policy) {
| Doe
    $policy->add("additional_price", "atkDummyAttribute", array(null, AF_DUMMY_SHOW_LABEL));
|-
  }
| Jane
</syntaxhighlight>
|
|-
|}


This should produce:
(Do not attempt to set the flag using the third argument to add(), and do not attempt to remove the label afterwards; it must be passed through to the atkDummyAttribute constructor, as above.)


Doe, John
= See Also =


Jane
* [[Usage without DB I/O Polymorphism]] - prevent an attribute from attempting to access the database; useful if you want to create a dummy "structured" attribute, such as a date attribute
* [[AtkExpressionAttribute]] - value is an SQL expression
* [[AtkParserAttribute]] - value is derived from other attributes

Latest revision as of 21:47, 18 August 2008

Attribute: atkDummyAttribute

API docs | Flags

List of other attributes

atkDummyAttribute screenshot

The atkDummyAttribute can be used to add arbitrary text/html to a form. It's called 'dummy' because it's an attribute that is not persisted in the database in any way.

Basic usage

Using an atkDummyAttribute is fairly easy. Just add it to a node and pass the desired text as the second parameter.

  $this->add(new atkDummyAttribute("explanation", "Enter your mother's maiden name in the below field only if your mother agrees"));

Note that dummy attributes by default do not have a label (as its regular usecase normally doesn't require one). In the occasion that you do want the label to be visible, you can add the attribute flag AF_DUMMY_SHOW_LABEL.

A slightly more sophisticated version of the atkDummyAttribute is the atkParserAttribute. It has the same api, but features the substitution of template variables in the text.

Advanced usage

If the contents of the dummy attribute depends on the currently edited record, and the atkParserAttribute isn't flexible enough, then you can use an atkDummyAttribute in combination with a display override to add arbitrary content to a screen.

In the below example, we use this principle to hide both the 'first_name' and 'last_name' columnts, and instead show a dummy attribute that provides a concatenated version of the full name.

  $this->add(new atkAttribute("first_name", AF_HIDE_LIST|AF_FORCE_LOAD));
  $this->add(new atkAttribute("last_name", AF_HIDE_LIST|AF_FORCE_LOAD));
  $this->add(new atkDummyAttribute("name_citation"));

  function name_citation_display($record) 
  {
    if (strlen($record["last_name"]) <> 0) 
    {
      $string = $record["last_name"] . ", ". $record["first_name"];
    } 
    else 
    { 
      $string = $record["first_name"];
    }
  
    return $string;
  }

Note the AF_FORCE_LOAD flag; without it the fields won't be in the SQL query for this screen. Since we need them anyway in the display override, we apply AF_FORCE_LOAD to make sure the values are always loaded even if they are hidden.

FAQ

Where are the attribute labels?

The atkDummyAttribute defaults to no label. If you wish to reinstate the label, configure it as follows:

  public static function meta(atkMetaPolicy $policy) {
    $policy->add("additional_price", "atkDummyAttribute", array(null, AF_DUMMY_SHOW_LABEL));
  }

(Do not attempt to set the flag using the third argument to add(), and do not attempt to remove the label afterwards; it must be passed through to the atkDummyAttribute constructor, as above.)

See Also