Login +

How To ? / Register a user

Suppose the Model of the table user generated:

Field "user_email" has a specific type « email ».

Field "user_password" has a filter "hash" => sha1.

Controller:

public function action()
{
    // Get sls XML
    $xml = $this->getXML();
    
    // Object
    $this->_generic->useModel("User");
    $user = new Main_User();
    $errors = array();
    
    // If form reloading
    if ($this->_http->getParam("reload_register") === 'true')
    {
        // User POST params
        $userInfos = $this->_http->getParam("user");
       
        // Check errors
        if (!$user->setUserEmail($userInfos["user_email"]))
            $errors["user_email"] = "Your email address is incorrect.";
        if ($userInfos["user_password"] != $userInfos["user_password2"])
            $errors["user_password"] = "Both passwords don't match.";
        else if (!$user->setUserPassword($userInfos["user_password"]))
            $errors["user_password"] = "You must choose a password.";
        if (!$user->setUserName($userInfos["user_name"]))
            $errors["user_name"] = "You must fill your name.";
        if (!$user->setUserFirstname($userInfos["user_firstname"]))
            $errors["user_firstname"] = "You must fill your firstname.";
       
        // If all good
        if (empty($errors))
        {
            // Insert user
            $user->setUserDate(date("Y-m-d H:i:s"));
            $user->create();
               
            // Save success
            $xml->addFullTag("success","Your account has been successfully created.",true);
        }
        // Else errors
        else
        {
            // Format errors
            $xml->startTag("errors");
            foreach($errors as $column => $error)
                $xml->addFullTag("error", $error, true, array("column" => $column));
            $xml->endTag("errors");
               
            // Remember user choices
            $xml->startTag("user");
            foreach($userInfos as $column => $value)
                $xml->addFullTag($column, $value, true);
            $xml->endTag("user");
        }
    }
       
    // Save sls XML updated
    $this->saveXML($xml);
}

View:

<fieldset class="register">
    <legend>Register</legend>
    <xsl:if test="//View/success != ''">
        <div class="success">
            <xsl:value-of select="//View/success" />
        </div>
    </xsl:if>
    <xsl:if test="count(//View/errors/error) > 0">
        <div class="error">
            <xsl:for-each select="//View/errors/error">
                <xsl:value-of select="." /><br />
            </xsl:for-each>
        </div>
    </xsl:if>
    <form method="post" action="">
        <input type="hidden" name="reload_register" value="true" />
        <div class="row">
            <label for="user_email">
                Email address
            </label>
            <input type="text" name="user[user_email]" id="user_email" value="{//View/user/user_email}" />
        </div>
        <div class="row">
            <label for="user_password">
                Passord
            </label>
            <input type="password" name="user[user_password]" id="user_password" />
        </div>
        <div class="row">
            <label for="user_password2">
                Confirm Passord
            </label>
            <input type="password" name="user[user_password2]" id="user_password2" />
        </div>
        <div class="row">
            <label for="user_name">
                Name
            </label>
            <input type="text" name="user[user_name]" id="user_name" value="{//View/user/user_name}" />
        </div>
        <div class="row">
            <label for="user_firstname">
                Firstname
            </label>
            <input type="text" name="user[user_firstname]" id="user_firstname" value="{//View/user/user_firstname}" />
        </div>
        <div class="row">
            <input type="submit" id="register_submit" value="Register" />
        </div>
    </form>
</fieldset>