ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

SafeCracker ajax success/failure message

September 12, 2011 1:25pm

Subscribe [3]
  • #16 / Jan 05, 2012 8:46pm

    Dan Decker

    7338 posts

    Hi Jonny,

    I’m glad you found a solution that works for you. If you need anything else, just let us know.

  • #17 / Sep 04, 2012 9:56am

    milk_

    9 posts

    I’m experiencing the exact same problem in Safecracker using json…
    I would like to make use of safecracker tags and validation and not refer to Freeform though…

    Here’s my template code:

    {exp:safecracker
    channel="members"
    dynamic_title="Iscrizione al sito Widerange da [email]"
    class="login"
    safecracker_head="no"
    register_member="yes"
    rules:privacy="required"
    preserve_checkboxes="yes"
    json="yes"
    }
         
        <ul class="plain">
           <section class="log-data"> 
           <li>
                <label for="email">Email</label>
                <input type="text" name="email" value="" id="email" placeholder="[email protected]">
            </li>
            <li>
                <label for="password">Password</label>
                <input type="password" name="password" value="" id="password" placeholder="Password">
            </li>
            <li >
                <label for="password_confirm">{bbr-mls-confirm}</label>
                <input type="password" name="password_confirm" value="" id="password_confirm" placeholder="{bbr-mls-confirm}">
            </li>
           </section>
           <section class="log-data"> 
            <li class="half">
                <label for="First_name">{bbr-mls-nome}</label>
                <input type="text" name="First_name" value="" id="first_name" value="<?php echo $_POST['first_name']; ?>" placeholder="{bbr-mls-nome}">
            </li>
            <li class="half">
                <label for="Last_name">{bbr-mls-cognome}</label>
                <input type="text" name="last_name" id="last_name" value="{last_name}" placeholder="{bbr-mls-cognome}">
            </li>
        
            <li>
             <label for="Address">{bbr-mls-via}</label>
             <input type="text" name="Address" value="" id="Address" placeholder="{bbr-mls-via}">
             </li>         
             <li >
             <label for="City">{bbr-mls-city}</label>
             <input type="text" name="City" value="" id="City" placeholder="{bbr-mls-city}">
             </li>
              <li>
             <label for="State">{bbr-mls-state}</label>
             <input type="text" name="State" value="" id="State" placeholder="{bbr-mls-state}">
             </li>
             <li>
             <label for="Zip">{bbr-mls-zip}</label>
             <input type="text" name="Zip" value="" id="Zip" placeholder="{bbr-mls-zip}">
             </li>
             <li>
             <label for="country">{bbr-mls-country}</label>
    <select name="country_code">
             {exp:cartthrob:countries}<option value="{country_code}">{country}</option>{/exp:cartthrob:countries}
     </select>
             </li>
             <li>
             <label for="Phone">{bbr-mls-phone}</label>
             <input type="text" name="Phone" value="" id="Phone" placeholder="{bbr-mls-phone}">
             </li>
             </section>
          
          <h2 class="expand"><a href="#">{bbr-mls-fill}</a></h2>
           <div class="collapse">
           <li class="half">
             <label for="rs">{bbr-mls-ragione}</label>
             <input type="text" name="rs" value="" id="rs" placeholder="{bbr-mls-ragione}">
           </li>
      <li class="half">
       <label for="Iva">{bbr-mls-iva}</label>
             <input type="text" name="Iva" value="" id="Iva" placeholder="{bbr-mls-iva}" maxlength="13">
             </li>
           </div>
                </ul>
                
         <fieldset id="iframe-signin">
        <input type="submit" value="{bbr-mls-register}" class="zocial evernote" />
         </fieldset>
      <div id="accetto"><input type="checkbox" name="privacy"><label for="privacy">{bbr-mls-privacy}</label></div>
      
    {/exp:safecracker}

    And here’s the Javascript

    $(document).ready(function() {
      function submit(form, callback) {  
       var action = form.attr('action');
       var post   = {};
    
       form.find('input, select, textarea').each(function() {
        var $t = $(this);
    
        post[$t.attr('name')] = $t.val();
       });
    
       $.post(action, post, function(data) {
        if(typeof callback == "function") {
         callback(data);
        }
       });
      }
    
      $('#publishForm').submit(function(e) {
    
       var form = $(this);
    
       submit(form, function(data) {
        if(!data.success) {
         alert('some problems here… this is for debugging I got at least here');
         
         var html = '<div class="errors"><h3>Errors</h3><p><ul>';<br />
         $.each(data.errors, function(i, error) {<br />
          html += '<li>'+error+'</li>';<br />
         });</p>
    
    <p>     html += '</ul></div>';</p>
    
    <p>     form.find('.errors').remove();<br />
         form.prepend(html);<br />
         <br />
             }<br />
        else {<br />
         alert('ok');<br />
         <br />
        }     <br />
       });</p>
    
    <p>   e.preventDefault();<br />
      });</p>
    
    <p> });

    The form works ok when no validation errors occur… I get the alert “OK”.
    When there are some problems I debugged with the alert “some problems here… “… but then it won’t show the relevant specific errors…
    No errors are displayed in my console…

    Thanks…

     

  • #18 / Sep 05, 2012 4:33pm

    Dan Decker

    7338 posts

    hi milk_,

    Here is example JSON output:

    {"success":0,"errors":[],"field_errors":{"title":"The Title field is required."},"entry_id":false,"url_title":false,"channel_id":false}

    So you want to loop though field_errors, not errors:

    $.each(data.field_errors, function(i, error) {
          html += '<li>'+error+'</li>';
          console.log〈error〉;
         });

    Can you try that for me?

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases