• 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

bottiger

Active member
#1
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?

PHP:
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);
    $writer->set('node_id', $forumId);
    $writer->preSave();
    $writer->save();
    return $writer->getMergedData();
}
 

Mikey

Well-known member
#2
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)
 

bottiger

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

Is it possible to bypass this requirement?
 

Mikey

Well-known member
#4
I don't understand why there is a requirement of uniqueness for an anonymous thread.

Is it possible to bypass this requirement?
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.
 
R

ragtek

Guest
#5
The problem is in XenForo_DataWriter_DiscussionMessage

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

ragtek

Guest
#6
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);
$writer->preSave();
$writer->save();
return $writer->getMergedData();
}

should do it