1. 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

Discussion in 'XenForo Development Discussions' started by -Calypso-, Jan 16, 2011.

  1. -Calypso-

    -Calypso- Member

    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
     
  2. Jeremy P

    Jeremy P Well-Known Member

    Did you add the new fields to the datawriter's _getFields() array as well?
     
  3. -Calypso-

    -Calypso- Member

    No but I will see if I can find that. :)
     
  4. Jeremy P

    Jeremy P Well-Known Member

    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;
    }
     
    alexD likes this.
  5. -Calypso-

    -Calypso- Member

    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.
     
  6. Fuhrmann

    Fuhrmann Well-Known Member

    Nice, it helped me!
     
  7. Rob

    Rob Well-Known Member

    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?
     
  8. Chris D

    Chris D XenForo Developer Staff Member

    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.
     
  9. Rob

    Rob Well-Known Member

    Hmmm.... the fields are added to the xf_node table. Is this wrong?
     
  10. Chris D

    Chris D XenForo Developer Staff Member

    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.
     
  11. Rob

    Rob Well-Known Member

    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.
     
  12. Lawrence

    Lawrence Well-Known Member

    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: Jul 20, 2015
  13. Chris D

    Chris D XenForo Developer Staff Member

    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.
     
  14. Rob

    Rob Well-Known Member

    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?
     
  15. Chris D

    Chris D XenForo Developer Staff Member

    No. Should be that simple. Just pointing this out, however:

    Your schema would still be "wrong" but it should also solve the problem.
     
  16. Rob

    Rob Well-Known Member

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

    Thanks for the pointers.
     
  17. Lawrence

    Lawrence Well-Known Member

    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, :)
     
  18. Rob

    Rob Well-Known Member

    It's an optional text field string so might be overkill?
     
  19. Lawrence

    Lawrence Well-Known Member

    Depends on the data. I've added 5 new options to my forums, two are text fields, and three are check boxes, I validate the text fields.
     
  20. Rob

    Rob Well-Known Member

    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();
    }
     

Share This Page