HOW TO change dynamically CDataForm widget field types?

Discuss everything related to ApPHP Durecty CMF. All versions. Any questions related to the Directy CMF developing or using should be posted to this forum.

Moderators: alexandrleonenko, alexmst

administrator
Site Admin
Site Admin
Posts: 6169
Joined: Jan 7th, '09, 23:18
Contact:

HOW TO change dynamically CDataForm widget field types?

Postby administrator » Apr 24th, '15, 19:32

Sometimes, if you want to use one field to store different type of values, you may need to apply also different type of validation for such field.

Lets say you have 2 fields: the 1st is field type and the 2nd is field value.
Below the example of code, that solves this issue:

Code: Select all

$itemType = A::app()->getRequest()->getPost('item_type');

$fields = array();      
$fields['item_type']   = array('type'=>'select', 'title'=>A::t('app', 'Type'), 'tooltip'=>'', 'default'=>'ip', 'validation'=>array('required'=>true, 'type'=>'set', 'source'=>array_keys($item_types)), 'data'=>$item_types, 'htmlOptions'=>array());

if($itemType == 'email'){
   $fields['item_value']   = array('type'=>'textbox', 'title'=>A::t('app', 'Item'), 'tooltip'=>'', 'default'=>'', 'validation'=>array('required'=>true, 'type'=>'email', 'maxLength'=>'100'), 'htmlOptions'=>array('maxLength'=>'100', 'class'=>'normal'));
}else if($itemType == 'username'){
   $fields['item_value']   = array('type'=>'textbox', 'title'=>A::t('app', 'Item'), 'tooltip'=>'', 'default'=>'', 'validation'=>array('required'=>true, 'type'=>'mixed', 'maxLength'=>'40'), 'htmlOptions'=>array('maxLength'=>'40', 'class'=>'normal'));
}else{
   $fields['item_value']   = array('type'=>'textbox', 'title'=>A::t('app', 'Item'), 'tooltip'=>'', 'default'=>'', 'validation'=>array('required'=>true, 'type'=>'ip', 'maxLength'=>'15'), 'htmlOptions'=>array('maxLength'=>'15', 'class'=>'normal'));
}

Return to “ApPHP Directy CMF {developers/users}”