Take metafield value without POST

Hi,

I’m using Kanboard with the plugin MetaMagik. I have a task creation with differents metafields. One of them, I create a button near these to create a aleatory ID automaticaly. Also, one the metafield is a select type named “Type” with options N or B. Depending on which of the options are selected, the aleatory ID do the same function but different conditional.

The problem comes that I can’t get the value of Type.

This is the changes I have done.
MetaHelper.php: For appears the button that I need and then call a function show2(which is the same as the original show function in TaskCreationController).

            if ($setting['data_type'] == 'text') {
                ($this->valueCheck($key, $values)) ?
                $html .= $this->renderMetaTextField($key, $values['metamagikkey_'.$key], $errors, $new_attributes):
                $html .= $this->renderMetaTextField($key, isset($metadata[$key]) ? $metadata[$key] : "", $errors, $new_attributes);
                if($setting['human_name'] == 'Identifyer'){
                    $title = $values['title'];

                     $html .=$this->helper->modal->medium('', t('Generar nuevo ID'), 'TaskCreationController', 'show2',array('project_id'  => $values['project_id'],
                     'column_id'   => $values['column_id'],
                     'swimlane_id' => $values['swimlane_id'],
                     'title' => $title
                     ));     

Finally, I create this function, trying prepare the values as the original function:

    protected function prepareValues2($isPrivateProject, array $swimlanesList)
    {
        $values = $this->request->getValues();
    
        $values = array(
            'swimlane_id' => $this->request->getIntegerParam('swimlane_id', key($swimlanesList)),
            'column_id'   => $this->request->getIntegerParam('column_id'),
            'color_id'    => $this->colorModel->getDefaultColor(),
            'metamagikkey_Identifyer' => substr($this->taskMetadataModel->getLastIdentifyer(), 2)+1
        );
        return $values;
    }

With this I can create the ID without any problem, but without the information what I need from the select named type

I cant just look at snippets, i need to see everything. Especially when things dont exist, and i dont knownwhat they do, like getLastIdentifier does not exist natively in KB.

But regardless, you push this button, what is supposed to happen? Fill the value, or fill and submit? I think your button, bypasses the actual submit button.

Why do you need a button? Just autofill the value, and that way you wont bypass the submit button.

getLastIdentifier is a function that return an id with a patterns that I need. For example, if I select Type = N the return could be N001AP, if I select Type = B the return could be B001NM, is only a function with a id personalizate like return

So, the button when it is pressed should call the function, and then fill the field named ID, without submit the form

Again, not being able to see all your code makes it hard to troubleshoot. Especially when words like, “should” are being used.

If you push this button, from what i currently see it calls the show function, which is NOT a submit form. Therefore, it is not submitting values, and by calling show, you have reset the form leading to a perpetual cycle of doing nothing.

You also didnt answer my questions. Leading me to a perpetual cycle of guessing.

You push this button, what is currently happening? I have to ask this, because I cant see your code, all of it.

Exactly, the form resets but appears with the ID field filled in with the getLastIdentifier function (with preset values ​​since I can’t get the selected value in type). Also, when resetting the form I lose everything I had previously written in the other fields. I understand that it is not the most optimal way, far from it.

Resume, when I press the button/link, the form resets with the field ID filled but all the others fields empty.

The button call function show2, which is exactly the same as the original show(), only changes that it call prepareValues2 in contradistinction to prepareValues in the original

Ok, so it does fill that field, but blanks everything else.

That would be because you arent sending all the values back to the show function, only your new one.

Is that the only issue, or is there something else?

Yes, this is one of the problems. The other is, I need to catch the value selected of “Type” to pass the value to the function getLastIdentifier, I suppose that I need JS to do that.

i don’t klnow what this is, is this a dropdown?

Yes, is a field named “Type” dropdown list type.

Personally, i wouldnt be doing what you’re doing the way you’re doing it, i can think of much easier and fluid process that doesnt involve a button. Seems highly inefficient to have to push a button and reload a page, with missing data nonetheless.

Maybe there is a reason im not seeing, but im pretty sure there isnt, and im pretty sure i could shoot a whole in any reason you come up with.

If it were me:

  • choose from select
  • autopopulate the id based on selection without a button.
  • yes it would include js

Yes, I just tried this, I thought it was the best option. I include an onchanged to the dropdown in the html which call the function js, but I couldn’t get it to work

It always takes me awhile to fogure out the js, but this might help, not sure im using dropdowns in here but there should be some similar.

I tried but I don’t know if i’m doing badly…I have done differents test, the most simple is this:

in FormHelper I change to add a onchange to render through renderSwimlaneField:

    public function select($name, array $options, array $values = array(), array $errors = array(), array $attributes = array(), $class = '')
    {
        $html = '<select name="'.$name.'" onchange="findmyvalue()" id="form-'.$name.'" class="'.$class.'" '.implode(' ', $attributes).'>';

        foreach ($options as $id => $value) {
            $html .= '<option value="'.$this->helper->text->e($id).'"';

            if (isset($values->$name) && $id == $values->$name) {
                $html .= ' selected="selected"';
            }
            if (isset($values[$name]) && $id == $values[$name]) {
                $html .= ' selected="selected"';
            }

            $html .= '>'.$this->helper->text->e($value).'</option>';
        }

        $html .= '</select>';
        $html .= $this->errorList($errors, $name);

        return $html;
    }

Then at /task_creation/show, I add script js at the begin of the document:

<script>
function findmyvalue()
{
var myval = document.getElementById("form-swimlane_id").value;
alert("try");
alert(myval);
}
</script>

And nothing appear when I change the selected value, none of the alerts

  1. Dont try to add js via html. Use your own js file and attach it.

  2. Dont use events with the html, use js.

1 Like