Calculate a default value

  • Views Views: 13,870
  • Last updated Last updated:

Navigation

      Access element (+)
      Birthday element
      Button element
      Calculation element
      Captcha element
      Checkbox element
      Colour Picker element
      Count element (+)
      Database join element
      Date element
      Digg element
      Display text element
      Dropdown element
      Facebook Like element
      Field element
      File Upload element
      Folder element
      Google Map element
      Image element
         Image databese join
      Internal id element
      IP element
      J!Date element
      Kaltura element
      Link element
      Notes element
      OpenStreetMap element
      Picklist element
      Radio Button element
      Rating element
      Sequence element
      Slider element
      Tags element
      Textarea element
      Thumbs element
      Time element
      Timer element
      Timestamp element
      Total element
      User element
      User group element
      Video element
      View level element
      YesNo element
      Youtube element
      Akismet validation
      Is Email validation
      Is Not validation
      Is Numeric validation
      Not empty validation
      PHP validation
      Rsa id
  • Most (but not all) element types have a default field (to set the default value for a new record). These Elements typically also provide an Eval Default checkbox by which the default can become a calculated field.

    Here are some common examples of how you might use this capability to do some interesting things:

    Basic example of return​
    You should always return a string or numeric value you want to use as the default. In this example we are returning the string 'default'.
    PHP:
    return 'default';

    Set to todays date but at 8pm / 20hrs​
    PHP:
    $d = JFactory::getDate();
    $d->setTime(20, 0, 0);
    return $d->toSql();

    Get the element's filter value​
    Say you filtered a list on an element, then went to add in a new record. Wouldn't it be great if the filtered element's value matched the selected filter? To achieve this enter the following in the element's default field:
    PHP:
    return FabrikHelperElement::filterValue(8);

    Replace the 8 in the above example with your element's id.

    Set to a query string value​

    Element values can be set on the query string, without using the Default mechanism, by simply appending &table___element=value on the query string on a form's normal URL. However, sometimes you may need to pass a query string value to a read only element (one which has "Form Access" set to prevent the currently logged on user from editing it).

    If an element is read only, you won't be able to set the value via the query string. But because default values are still applied when creating a new form, even for 'read only' Elements, you can use this trick with an eval'ed default:

    PHP:
    $app = JFactory::getApplication();
    return $app->input->getString('query_string_key');

    This would set the element's default value to whatever &query_string_key=value is set to on the URL. Obviously modify this to use getInt() if you specifically want an integer.

    One example way of using this trick is if you are using the Relate Data feature on a List to "Add" a form to a related list, but you wanted to keep the foreign key (FK) on the related list as read only. As the related data Add feature uses the simple query string method to set the FK on your new related form's value to that of the PK (rowid) on the 'parent' list, you would ...

    PHP:
    return $app->input->getInt('relatedtable___fk_element');

    on your related table's FK element on the related form.
Back
Top