On the fly add forms field and handle the data in custom components

Good day,
I would like to ask how to handle this on the fly add fields on a form. Basically I have a career form that can add extra fields. My question is

  1. how can I use an array here to handle the data and display as well if exist?
  2. how can I capture the data using snippets and save the multiple object data in the custom table?

I use the classextender extra by bobray and barrow the updateprofile snippets.
Heres my sample code for create, update, delete an object
how can i do multiple inserts here using loop and array?

   <?php
/*Add in our package*/
    $base_path = !empty($base_path) ? $base_path : $modx->getOption('core_path').'components/careerservices/';
    $cs = $modx->addPackage('careerservices', $base_path.'model/');
    
    $data = null;
    $user = null;
    $fields = array();
    
    $submit = isset($_POST['login-updprof-btn']) && ($_POST['login-updprof-btn'] == 'Save');
    
    if (isset($modx->user) && ($modx->user instanceof modUser)) {
        
        /*get user id to User Object*/
        $user =& $modx->user;
        $userId = $user->get('id');
        
        /*Getting the existing data*/
        $data = $modx->getObject('csEducation', array('user_id' => $userId), false);
        
        /*Check if the $data*/
        if($data) {
            $fields = $data->toArray();
        } else {
            /*Create data if no data exist upon submit*/
            $data = $modx->newObject('csEducation');
            if($data) {
                $data->set('user_id', $userId);
                $fields = $data->toArray();
            }
        }
        
    }
    
    /*Convert any nulls to ''*/
    if(!is_array($fields) || empty($fields)) {
        return '';
    }
    
    
    if(!empty($fields)) {
        
        /*If the user_id, is no data yet*/
        foreach($fields as $key => $value) {
            if(empty($value) && ($value !== '0')) {
                $fields [$key] = '';
            }
        }
        /*Place the data of user_id if exist*/
        
            $modx->setPlaceholders($fields);
        
    }
    
    /*Sanitize and persistent the data*/
    if ($submit) {
        $modx->request->sanitizeRequest();
        $dirty = false;
        foreach($fields as $key => $value) {
            if(isset($_POST[$key])) {
                if($value !== $_POST[$key]) {
                    $data->set($key, $_POST[$key]);
                    $dirty = true;
                }
            }
        }
        
        if($dirty) {
            $data->save();
        }
        
    }
    
    return '';

Little help is appreciated, thanks!

1 Like

Ok one note is to use three single ticks on the lines above and below your code, then it will format properly.

So I think with ClassExtender you need to make the db in advance, I doubt you could do it on the fly. You could however save this data to another place, as a tv for example.

If your ClassExtender db has extra fields, then you could allow users to populate those fields, but the number would be limited by the db. You could make a lot of extra fields perhaps…

In the if ($submit) { section, you can add your own logic to match your requirements.

If your fields in the frontend for example all have a name of extra_field[] (note the square brackets), then in the snippet you’ll have an array in $_POST['extra_field']. You can manipulate, sanitise, process that any way you’d like, and then perhaps write it to a field in your table as JSON or to a separate table.

1 Like

I remember in the old Discuss forum extra there was a bit of js code that allowed for multiple attachments on posts…there was a limit but not sure if it could be removed…this requirement sounds similar with an open requirement for number of fields, then save them anywhere you want (except as we said in the ClassExtender db)

I would expect the CE db could be extended with a custom snippet, but I don’t know that the schema really intends for multiple fields to be added on the fly, much easier to save the data elsewhere rather than modifying a database.