Welcome to the Cookbook

loading...

7.1.2.6 observeField

observeField(string $field, array $options)

Observes the field with the DOM id specified by $field (every $options['frequency'] seconds) and makes an XMLHttpRequest when its contents have changed.

When no frequency or a small frequency interval (between 0 and 1) is specified, a prototype Form.Element.EventObserver will be used instead of a Form.Element.Observer. The Form.Element.EventObserver is not timed and will execute at the same time the value of the element has changed.

<?php echo $form->create( 'Post' ); ?>
<?php $titles = array( 1 => 'Tom', 2 => 'Dick', 3 => 'Harry' ); ?>	
<?php echo $form->input( 'title', array( 'options' => $titles ) ) ?>
</form>

<?php 
echo $ajax->observeField( 'PostTitle', 
    array(
        'url' => array( 'action' => 'edit' ),
        'frequency' => 0.2,
    ) 
); 
?>
  1. <?php echo $form->create( 'Post' ); ?>
  2. <?php $titles = array( 1 => 'Tom', 2 => 'Dick', 3 => 'Harry' ); ?>
  3. <?php echo $form->input( 'title', array( 'options' => $titles ) ) ?>
  4. </form>
  5. <?php
  6. echo $ajax->observeField( 'PostTitle',
  7. array(
  8. 'url' => array( 'action' => 'edit' ),
  9. 'frequency' => 0.2,
  10. )
  11. );
  12. ?>

observeField uses the same options as link

The field to send up can be set using $options['with']. This defaults to Form.Element.serialize('$field'). Data submitted is available at $this->data inside your controllers. Callbacks can be used with this function.

To send up the entire form when the field changes use $options['with'] = Form.serialize( $('Form ID') )