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.


public function action()
    // Get sls XML
    $xml = $this->getXML();
    // Object
    $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"));
            // Save success
            $xml->addFullTag("success","Your account has been successfully created.",true);
        // Else errors
            // Format errors
            foreach($errors as $column => $error)
                $xml->addFullTag("error", $error, true, array("column" => $column));
            // Remember user choices
            foreach($userInfos as $column => $value)
                $xml->addFullTag($column, $value, true);
    // Save sls XML updated


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