Jump to content

Javascript: Difference between revisions

From NusaATK
Samk (talk | contribs)
Samk (talk | contribs)
No edit summary
Line 1: Line 1:
{{howto|Medium|[[Sam Kapilivsky]]}}
{{howto|Medium| [Ivo Jansch] edited by [[Sam Kapilivsky]]}}


This howto demonstrates some Javascript features that are built into ATK.
This howto demonstrates some Javascript features that are built into ATK.
Line 12: Line 12:
$attr->addOnChangeHandler("if (newvalue=='no') { hideAttrib('myAttribute'); } else { showAttrib('myAttribute'); }");  
$attr->addOnChangeHandler("if (newvalue=='no') { hideAttrib('myAttribute'); } else { showAttrib('myAttribute'); }");  
</syntaxhighlight>
</syntaxhighlight>
== Validation ==
The best way to add javascript validation is to put your validation functions in a file. Suppose you have a module called 'users', you can put the routines in modules/users/javascript/validation.js for example.
Make sure your routine returns true on succesful validation, and false on failure. Also, you could put an alert() in it with the error message. For the sake of example, let's assume your javascript function is called 'checkSomething()'.
Then, there are several ways to hook up your javascript. Suppose you want to add the validation to the edit page. The easiest way to do this is to override the action, like so:
<syntaxhighlight lang="php">
function action_edit(&$handler)
{
  // first  make sure the javascript is loaded...
  $page = &$this->getPage();
  $page->register_javascript(moduleDir("users")."javascript/validation.js");
  // then make sure our routine is called when the user tries to submit the
  // form.
  $page->register_submitscript("checkSomething();");
  // then continue with the original editing functionality.
  return $handler->action_edit();
}
</syntaxhighlight>
The framework takes care of the rest. Any registered submit script that returns false will cause the form not to post.

Revision as of 22:02, 14 November 2008

ATK Howto: Javascript

Complexity: Medium
Author: [Ivo Jansch] edited by Sam Kapilivsky

List of other Howto's

This howto demonstrates some Javascript features that are built into ATK.

Hiding or showing an attribute or relation depending on the value of another attribute

If you have an attribute that should hide or show depending on the value of another attribute, you can use the following construct:

&$this->add(new atkAttribute('myAttribute',........ 
$attr = &$this->add(new atkBoolAttribute(........ 
$attr->addOnChangeHandler("if (newvalue=='no') { hideAttrib('myAttribute'); } else { showAttrib('myAttribute'); }");

Validation

The best way to add javascript validation is to put your validation functions in a file. Suppose you have a module called 'users', you can put the routines in modules/users/javascript/validation.js for example.

Make sure your routine returns true on succesful validation, and false on failure. Also, you could put an alert() in it with the error message. For the sake of example, let's assume your javascript function is called 'checkSomething()'.

Then, there are several ways to hook up your javascript. Suppose you want to add the validation to the edit page. The easiest way to do this is to override the action, like so:

function action_edit(&$handler) 
{ 
  // first  make sure the javascript is loaded... 
  $page = &$this->getPage(); 
  $page->register_javascript(moduleDir("users")."javascript/validation.js"); 

  // then make sure our routine is called when the user tries to submit the 
  // form. 
  $page->register_submitscript("checkSomething();"); 

   // then continue with the original editing functionality. 
   return $handler->action_edit(); 
}


The framework takes care of the rest. Any registered submit script that returns false will cause the form not to post.