Tuesday, April 3, 2012

CakePHP helper to suggest input

What's it do?
makes it easier to make a gray suggest input box. Just use drop it as InputHelper.pm in View/Helper/ and call it where and how you need it. call it just like Form->input but add a sample text for the box.
$this->Input->input(fieldname, options array, 'sample text');

As an example, see what happens when you click in , click out, and change the content this box.




<?php App::uses('FormHelper', 'View/Helper');
/* helper to make default input help that disappears when clicked */
/* usage: $this--->Input-&gt;input */
class InputHelper extends FormHelper {
public function input($fieldname, $options=array(null), $fill) {
$options['class'] = ' dimmed';
$options['value'] = $fill;
$options['onClick'] = "if (this.value=='".$fill."') {this.value='';this.style.color='black'}";
$options['onBlur'] = "if (this.value=='') {this.value = '".$fill."'; this.style.color='gray'}";
$out = parent::input($fieldname, $options);
$out .= parent::hidden($fieldname . "_hidden", array('value' =&gt; $fill));
return $out;
}
}
?>

What's with the hidden? If you want to know what the original content was, you can test against this in your Controller to see if fieldname value is equal to fieldname_hidden value and don't store if it matches.

No comments:

Blog Archive