Forum for discussing general topics related to Couch.
4 posts Page 1 of 1
Please refer following code:

Code: Select all
<cms:form enctype="multipart/form-data" method='post' anchor='0'>
            <cms:if k_success >
                <!--
                    The 'member_process_registration_form' tag below expects fields named
                    'member_displayname', 'member_name' (optional), 'member_email',
                    'member_password' and 'member_password_repeat'
                -->

<cms:set submitted_values=k_success />
               
                <cms:member_process_registration_form
                    _send_mail='0'
                />
               
               
               
               
                <cms:member_process_registration_form
                      member_fullname=frm_member_fullname
                      member_displayname=frm_member_displayname
                        company_name=frm_company_name
                    />
                   
                   
                 
               
                <cms:if k_success >
               
                   <cms:php>
                            require("../PHPMailerAutoload.php");

                            $mail = new PHPMailer();
                             
                            $mail->IsSMTP(); // send via SMTP
                            $mail->Host = ""; // SMTP servers change to localhost
                            $mail->Smtp_port ="";       // change smtp port
                            $mail->SMTPAuth = true; // turn on SMTP authentication
                            $mail->Username = ""; // SMTP username
                            $mail->Password = ""; // SMTP password
                             
                            $mail->From = "";
                            $mail->FromName = "test";
                            $mail->AddAddress("<cms:show frm_member_email />","<cms:show frm_member_displayname />");
                            $mail->AddReplyTo("","");
                            $mail->addBCC("");
                             
                            $mail->WordWrap = 50; // set word wrap
                             
                            $mail->IsHTML(true); // send as HTML
                             
                            $mail->Subject = "New Registration Account";
                            $mail->Body = "
                                        <p>Thank you for registrated a account on our company!<br />Here's your enquiry details:</p>
                                        <table>
                                        <tr><td>Full Name:</td><td><cms:show frm_member_fullname /></td></tr>
                                        <tr><td>Login Username:</td><td><cms:show frm_member_displayname /></td></tr>
                                        <tr><td>Email:</td><td><cms:show frm_member_email /></td></tr>
                                        <tr><td>Username:</td><td><cms:show frm_member_name /></td></tr>
                                        <tr><td>Company Name:</td><td><cms:show frm_company_name /></td></tr>
                                        </table>
                                        <p>Regards,<br />NSY</p>
                                        ";
                            $mail->AltBody = "Thank you for contacting us! We get back to you as soon as possible.";
                           
                           
                            if(!$mail->send()) {
                                echo 'Message could not be sent.';
                                echo 'Mailer Error: ' . $mail->ErrorInfo;
                            } else {
                                echo 'Message has been sent';
                            }
                            </cms:php>
                           
               
                    <cms:set_flash name='success_msg' value='1' />
                    <cms:redirect k_page_link />
                </cms:if>
            </cms:if>

               
                <cms:if k_error >
                    <p class="notice"><font color='red'>
                    <cms:each k_error >
                        <cms:if item='<b>Name:</b> Page already exists by this name' >
                            <cms:set item='<b>Login Username:</b> Login Username already exists' />
                        </cms:if>
                        <cms:show item /><br />
                    </cms:each>
                    </font></p>
                </cms:if>
               
                <div class="member-form">
               
                    <p>* All fields must be filled in. *</p>
                   
                    <div>
                    <label>Your Full Name</label>
                    <cms:input name='member_fullname' label='Full Name' type='text' required='1' />
                    </div>
                                                                       
                    <div>
                    <label>Login Username</label>
                    <cms:input name='member_displayname' label='Login Username' type='text' required='1' />
                    </div>
                   
                    <label>New Password</label>
                    <cms:input name='member_password' label='Password' type='password'  required='1' /><br />
                   
                    <label>Repeat Password</label>
                    <cms:input name='member_password_repeat' label='Repeat Password' type='password'  required='1' /><br />
                   
                    <label>Email</label>
                    <cms:input name='member_email' label='Email' type='text' required='1' /><br />
                   
                    <label>Company Name</label>
                    <cms:input name='company_name' label='Company Name' type='text' required='1' /><br />
                   

                    <button type="submit" name="submit" value="Create" class="submit">Create</button>
                    <button type="button" ONCLICK="window.location.href='../html/ourproducts.php'" value="Back" class="submit">Back</button>
            </div>
               
            </cms:form>


What should I do to make sure that the input field is not repeatedly register by user? for example the only a unique login username can exist in database, the system will prompt the error message like "Login name already exists".
Hi,

I see in your code that you are using 'cms:member_process_registration_form' twice -
Code: Select all
<cms:member_process_registration_form
    _send_mail='0'
/>

<cms:member_process_registration_form
    member_fullname=frm_member_fullname
    member_displayname=frm_member_displayname
    company_name=frm_company_name
/>

You should use it just once (combining the parameters ) -
Code: Select all
<cms:member_process_registration_form
    member_fullname=frm_member_fullname
    member_displayname=frm_member_displayname
    company_name=frm_company_name
    _send_mail='0'
/>

That said, coming to your question - I don't think I could understand it properly.
Which field do want not to be duplicated? Could you please elaborate.

Thanks.
Let' say I got a input field name "company_name", the CMS only allow one company to register, so when the same company trying to register twice, the CMS will show error message like "company name is existed or registered". The current text field only detect if the text field is empty or not ("required = 1").

I can't seem to find this kind of validation tutorial in the documention file, maybe you can just give me the link, I will take a look.
Hi,

Checking for duplicate values etc. is what validators are for.
You'll remember that all <cms:editable> and <cms:input> tags accept a parameter named 'validator' e.g. -
<cms:editable name='test_field' validator='integer' />

Couch ships with several built-in validators (complete list here - http://docs.couchcms.com/tags-reference ... #validator) however none of them would be suitable for our particular requirement.

But that is nothing to worry about as validators are simple PHP functions and so we'll code our own.
Edit your 'couch/addons/kfunctions.php' file and place the following function within it -
Code: Select all
function my_duplicate_validator( $field, $args ){
    global $FUNCS;

    if( $field->modified ){
        $company_name = trim( $field->get_data() );

        if( $company_name ){
            // Create Couch script.. we'll use the 'cms:pages' tag to return count of existing pages with this company_name
            $html = "<cms:pages masterpage='".$field->page->tpl_name."' custom_field='company_name==".$company_name."' count_only='1' />";

            // Pass on the code to Couch for execution using the 'embed' function
            $count = $FUNCS->embed( $html, $is_code=1 );

            if( $count ){
                return $FUNCS->raise_error( 'The company-name already exists' );
            }
        }
    }

    return true;
}

That creates our validator.
To add it to the 'company_name' editable region, edit your member template and modify the region to make it as follows -
<cms:editable name='company_name' type='text' validator='my_duplicate_validator' />

If you take a look at the code within the validator function, you'll notice that it is basically piggybacking on <cms:pages> tag to find any page with the name supplied in the field. Something like this -
<cms:pages masterpage='members.php' custom_field="company_name==xyz" count_only='1' />

Now whenever <cms:member_process_registration_form> tries to create a member account (i.e. cloned page of members template), our validator kicks in and first validates if a page by the company name does not already exist. If it does, an error message is thrown and the page creation process is aborted.

Hope it helps.
4 posts Page 1 of 1