• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

The field 'X' was not recognised

#1
I've added a couple new fields to the node table in an attempt to add something to the forum view, however when I attempt to save it I get the js error "The field 'X' was not recognised". I been looking where to fix this but can't seem to run it down.

File: library->XenForo->DataWriter.php

recognised should also be "recognized" on line 595, also it is hard coded, should it be in some sort of admin_lang or something? Not sure..

Thanks
 

Jeremy P

Well-known member
#4
XenForo_DataWriter_Node..

Create a code listener for load_class_datawriter, extending XenForo_DataWriter_Node..

PHP:
protected function _getFields()
{
    $fields = parent::_getFields();

    $fields['xf_node'] += array(
        'yourField' => array(), // Fill the array with the relevant information
    );

    return $fields;
}
 
#5
Nice, that was it. The extra fields had to be added to the xf_forums table, not the xf_nodes table although they both displayed the field data correctly.
 

Rob

Well-known member
#7
I've got a really strange issue here....

I'm extending XenForo_DataWriter_Node and am setting the fields inside _getFields().... however, when tabbing out of the field, when editing a forum, I get the error "The field 'newthread_button_title' was not recognised."

I've set the listener on the load_class_datawriter and am extending the classes and can't see a reason for the error, or for the data not being saved.

Listen.php
PHP:
<?php

class NewThreadButtonText_Listen
{
    public static function extendControllers($class, array &$extend)
    {
        if ($class == 'XenForo_ControllerAdmin_Forum') {
            $extend[] = 'NewThreadButtonText_ControllerAdmin_Forum';
        }
    
        if ($class == 'XenForo_DataWriter_Node') {
            $extend[] = 'NewThreadButtonText_DataWriter_Node';
        }
    }
}
Node.php
PHP:
<?php

class NewThreadButtonText_DataWriter_Node extends XFCP_NewThreadButtonText_DataWriter_Node
{
    protected function _getFields()
    {
        $existingFields = parent::_getFields();
        $existingFields['xf_node'] += array(
            'newthread_button_title' => array(
            'type' => self::TYPE_STRING,
            'default' => ''
            ),
        );

        return $existingFields;
    }

    protected function _preSave()
    {
        if (XenForo_Application::isRegistered('newthread_button_title'))
        {
            $this->set('newthread_button_title', XenForo_Application::get('newthread_button_title'));
        } else {
            $this->set('newthread_button_title', '');
        }
        return parent::_preSave();
    }
}
It's bugging the hell out of me. @Andy B has had a look too and I cant get to the bottom of this :S

Any ideas?
 

Chris D

XenForo developer
Staff member
#8
If the field belongs to the xf_forum table (which it should do if its value is edited while editing a forum) then it should be the XenForo_DataWriter_Forum datawriter you're extending.
 

Chris D

XenForo developer
Staff member
#10
That said, it's not a totally invalid approach. It doesn't actually totally explain the error.

I think @Jon W has seen your code and suggested a more likely cause/solution in your add-on thread.
 

Rob

Well-known member
#11
yeah... this is bugging me. I took all the hints out and extended based on the $class name in the listener, just to rule that out.
Gawd, this is bugging me.

I'm gonna roll back to an earlier version and start again, lol.
 

Lawrence

Well-known member
#12
I'm extending XenForo_DataWriter_Node and am setting the fields inside _getFields().... however, when tabbing out of the field, when editing a forum, I get the error "The field 'newthread_button_title' was not recognised."
If the fields are added to the xf_forum table you can extend the actionValidateField function in the controller admin forum class. This will eliminate that error (or you can just add a field validation method for node.php by extending it, as it doesn't have a field validation check to over-ride and the form requires it).

PHP:
class Xxxxxx_Yyyyyy_Listener_Proxy_ControllerAdmin_Forum extends XFCP_Xxxxxx_Yyyyyy_Listener_Proxy_ControllerAdmin_Forum
{
   public function actionValidateField()
    {
        $parentResponse = parent::actionValidateField();

        $fieldName = $this->_input->filterSingle('name', XenForo_Input::STRING);
        if ($fieldName == 'yourField' OR $fieldName == 'yourOtherField')
        {
            $extendedResponse = $this->_validateField('Xxxxx_Yyyyyy_DataWriter_Forum', array(
                'existingDataKey' => $this->_input->filterSingle('node_id', XenForo_Input::UINT))
            );
            return $extendedResponse;
        }
        return $parentResponse;
    }
}
 
Last edited:

Chris D

XenForo developer
Staff member
#13
I just spotted the "when tabbing out of a field" bit. I had it in my head this was all happening on save.

In which case, yes, I would consider moving the field to xf_forum (which is most correct anyway), and then the validate field method in the forum datawriter won't give this error.

Just to explain it in some detail:

The forum_edit form contains this:
PHP:
<form action="admin.php?forums/save" class="xenForm formOverlay AutoValidator" data-fieldvalidatorurl="admin.php?forums/validate-field" data-redirect="on" method="post">
Notice the field validator URL. This is the code that Lawrence is referring to.

At the moment, whenever a field is edited, that method is called to verify the contents of the form so far. This is the kind of thing that notifies users that their chosen username is already taken on registration without submitting the entire form.

Because your field is in the Node DW, the validator sees a value for a field named "newthread_button_title" and because it only checks against the Forum DW it fails and gives the error.

Lawrence's suggestion proposes modifying that validator - I don't personally think this is a great idea.

Lawrence's other suggestion (and mine) is to move the field to the Forum DW. IMO this is most correct. Think about the nodes we have by default. The only other one is Categories. You can't create threads in a Category so having the field available to categories doesn't seem logical. If some add-on came along and implemented something like "Article" nodes, again, there's no need for your field to be there, either.

However: there is one more solution:

In the field you have added to the forum_edit template, give it a class of "OptOut". That will actually prevent the error in its entirety without having to change much within the add-on itself.
 

Rob

Well-known member
#14
You are correct of course. I'll refactor the mod and use the forum table instead. Will I still need to extend the validator classes?
 

Chris D

XenForo developer
Staff member
#15
No. Should be that simple. Just pointing this out, however:

However: there is one more solution:

In the field you have added to the forum_edit template, give it a class of "OptOut". That will actually prevent the error in its entirety without having to change much within the add-on itself.
Your schema would still be "wrong" but it should also solve the problem.
 

Rob

Well-known member
#16
Yeah, I'll start over and do it properly. No point in that field being there on all node types. ;)

Thanks for the pointers.
 

Lawrence

Well-known member
#17
You are correct of course. I'll refactor the mod and use the forum table instead. Will I still need to extend the validator classes?
In my opinion you should always do so when you are adding extra data to the forums table. Extending the validation process is quick and easy to do, :)
 

Rob

Well-known member
#20
Do you have an example to add validation to a simple text field?

On a seperate point, I now need to use Forum.php instead of Node.php .....
Is there a _presave method on the "forum"?

I had this in the old file
PHP:
protected function _preSave(){
   if (XenForo_Application::isRegistered('newthread_button_title')) {
      $this->set('newthread_button_title', XenForo_Application::get('newthread_button_title')); 
   } else {
      $this->set('newthread_button_title', ''); 
   }
   return parent::_preSave();
}