Often you want certain form fields appear or disappear based on value of other fields. While in some cases it requires field re-loading, most of the cases can be dealt with by simple conditional rule.

This example shows how you can use univ().setConditionalShow() on different elements.
- I would like to upload my photo

Following fields are implemented in 3.9.2/trunk

NDAContract
<?php
 
// You might need to use $p=$this; if you are inserting this code into page/*.php

$f=$p->add('Form');
$f->addField('line','address1','Address line 1')
  ->
js(true)->univ()->bindConditionalShow(array(
    
''=>array(),
    
'*'=>array('address2')
  ),
'dl')->autoChange(1);
$f->addField('line','address2','Address line 2');

$interests=array('S'=>'swimming','R'=>'running','W'=>'watching birds');
$f->addField('dropdown','interest','Your interests')
  ->
setValueList($interests)
  ->
js(true)->univ()->bindConditionalShow(array(
    
'S'=>array('swimming_info'),
    
'R'=>array('running_info'),
    
'W'=>array('birds_which','birds_where'),
  ),
'dl');

$f->addField('line','swimming_info','How far can you swim?');
$f->addField('line','running_info','How far can you run?');
$f->addField('line','birds_which','What type of birds?');
$f->addField('line','birds_where','Where do you watch them?');

$f->addField('checkbox','has_photo','I would like to upload my photo')
  ->
js(true)->univ()->bindConditionalShow(array(
    
''=>array(),
    
'Y'=>array('upload')
  ),
'dl')->autoChange(1);
$f->addField('upload','upload','My Photo');

$f->addSeparator('Following fields are implemented in 3.9.2/trunk');

$docs=array('N'=>'NDA','C'=>'Contract');
$f->addField('checkboxlist','checkboxlist','Documents provided')
   ->
setValueList($docs)
  ->
js(true)->univ()->bindConditionalShow(array(
    
'N'=>array('upload_nda'),
    
'C'=>array('upload_contract'),
  ),
'dl');
$f->addField('upload','upload_nda','Upload NDA');
$f->addField('upload','upload_contract','Upload Contract');

$docs=array('r'=>'red','b'=>'blue','o'=>'Other..');
$f->addField('radio','radio','Favorite Color')
  ->
setValueList($docs)
  ->
js(true)->univ()->bindConditionalShow(array(
    
'o'=>array('other_color'),
  ),
'dl');
$f->addField('line','other_color','Specify Color');
 
if(
$f->isSubmitted()){
  
$f->js()->univ()->alert('very interesting')->execute();
}



blog comments powered by Disqus