2

I have a safecracker form that allows users to enter a url_title. If url_title is already used, by default, a number gets added to url_title so it’s unique. But instead of having this automated behavior where number gets added to url_title, Is there a way i can notify the user that the url_title typed is already taken so user can enter a new one?

2 Answers 2

2

You could build a custom extension that queries the database (I think exp_channel_titles) for the url_title supplied, then return an error to the user if it already exists. You would probably want to look into the safecracker_submit_entry_start hook.

Alternatively, you could use JavaScript and perform some validation on the front end that passes the url_title to an AJAX request that ultimately does a query like the option above. You could take that response and determine whether to prevent the form submission and notify the user if that url_title is taken or continue as normal if the url_title is not taken.

1

Alternatively, you could use JavaScript and perform some validation on the front end that passes the url_title to an AJAX request that ultimately does a query like the option above. You could take that response and determine whether to prevent the form submission and notify the user if that url_title is taken or continue as normal if the url_title is not taken.

That's sort of what I do.

I also check for a maximum character length of 50 (EE has a url_title char length of 75 I believe).

I use the following in a template with php on input to check if the url_title exists

*(Assuming segment_3 contains your url_title):*

<?php
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')){
    $seg3 = $this->EE->uri->segment(3);
    if(isset($seg3) && ($seg3 != '')){
        $shopurl = utf8_decode($seg3);
        $length = strlen(utf8_decode($shopurl));
        $shopurl = $this->EE->db->escape_str($seg3);
        $query = $this->EE->db->query("SELECT count(url_title) AS count FROM exp2_channel_titles WHERE channel_id = '17' AND url_title = '$shopurl'");
        $count = $query->row('count');
        if($length >= '51'){
?>
    <div id="shopUrlMessage" class="userMessageSmall userMessageError shopUrlTaken">
        <p>
            Please choose a shorter name.
        </p>
    </div>
<? } if($length < '51' && $count != '0'){?>
    <div id="shopUrlMessage" class="userMessageSmall userMessageError shopUrlTaken">
        <p>
            Sorry that name is taken.
        </p>
    </div>
<? } if($length < '51' && $count == '0') {?>
    <div id="shopUrlMessage" class="userMessageSmall userMessageSuccess">
        <p>
            Hooray! That name is available.
        </p>
    </div>
<? } }
} else {
    exit('No direct script access allowed');
}
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.