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

Unique username error when making a thread with anonymous name

Discussion in 'XenForo Development Discussions' started by bottiger, Feb 5, 2012.

  1. bottiger

    bottiger Active Member

    I am using this code to make threads.

    I pass in a userid of 0 so I can put in whatever I want for the username since it is an anonymous user. But if the username matches with someone that has already registered, I get an error message saying the username is not unique!

    Is there any way to avoid this error?

    function createThread($userid$username$forumId$subject$message)
    $writer XenForo_DataWriter::create('XenForo_DataWriter_Discussion_Thread');
    $postWriter $writer->getFirstMessageDw();
  2. Mikey

    Mikey Well-Known Member

    one way to avoid the error would be to check if the username exists in the database already, which xenforo does, so check if the error is returned, if it is, add the current datetime automatically to the end of the username (or ask the user to choose a new name)
  3. bottiger

    bottiger Active Member

    I don't understand why there is a requirement of uniqueness for an anonymous thread.

    Is it possible to bypass this requirement?
  4. Mikey

    Mikey Well-Known Member

    The anonymous thread, is it in a forum which allows guest posting? That might bypass it. It's the only way I can think of.
  5. ragtek

    ragtek Guest

    The problem is in XenForo_DataWriter_DiscussionMessage

        * Generic discussion message pre-save handler.
    protected final function _preSave()
            if (
                && !
    $this->get('user_id') && $this->isChanged('username')
    $userDw XenForo_DataWriter::create('XenForo_DataWriter_User'XenForo_DataWriter::ERROR_ARRAY);
    $userErrors $userDw->getErrors();
                if (
    $this->error(reset($userErrors), 'username');
    you need to overwrite this / or set OPTION_VERIFY_GUEST_USERNAME to false;)
  6. ragtek

    ragtek Guest

    function createThread($userid, $username, $forumId, $subject, $message)
    $writer = XenForo_DataWriter::create('XenForo_DataWriter_Discussion_Thread');
    $writer->set('user_id', $userid);
    $writer->set('username', $username);
    $writer->set('title', $subject);
    $postWriter = $writer->getFirstMessageDw();
    $postWriter->set('message', $message);
    $postWriter->setOption(XenForo_DataWriter_DiscussionMessage::OPTION_VERIFY_GUEST_USERNAME, false); $writer->set('node_id', $forumId);
    return $writer->getMergedData();

    should do it

Share This Page