0

I'm trying to capture in a variable Safecracker's global and field errors so I can display them outside of my Safecracker form. In my Safecracker form, I have the following:

{if global_errors:count > 0}
    {exp:stash:set
        name="global_errors"
        save="save"
        scope="user"
    }
        {global_errors}
            <p>{error}</p>
        {/global_errors}
    {/exp:stash:set}

    <h2>Global Errors</h2>
    <h2>{exp:stash:get name="global_errors"}</h2>
{/if}

{if field_errors:count > 0}
    {exp:stash:set
        name="field_errors"
        save="save"
        scope="user"
    }
        {field_errors}
            <p>{error}</p>
        {/field_errors}
    {/exp:stash:set}

    <h2>Field Errors</h2>
    <h2>{exp:stash:get name="field_errors"}</h2>
{/if}

At this point, it's not even able to access the variable value right after I set it. So, my guess I am setting this incorrectly. Any ideas what's going on?

2

When you save a variable with a specific scope (in this case, scope="user") you should use the same scope when you get it.

However, if you're showing the errors within the same page request there's no need to actually save the variable at all.

It's possible in this case that the tag pairs {global_errors} and {field_errors} are removed from the template by Safecracker and then only output if there are errors. If that is the case you can capture them like this:

{field_errors}
    {exp:stash:append_list name="field_errors"}
        {stash:error}{error}{/stash:error}
    {/exp:stash:append_list}
{/field_errors}

And access them like this:

{exp:stash:get_list name="field_errors" process="end"}
    {if count == 1}<h2>Field Errors</h2>{/if}
    <p>{error}</p>
{/exp:stash:get_list}
| improve this answer | |
1

Try adding process="end" to your get tags:

{exp:stash:get name="field_errors" process="end"}
| improve this answer | |
0

Since the stash:set and stash:get are on the same parsing stage, the 15, the values aren't available yet when you're calling them.

If you really need stash, you should move the stash:get to an embed.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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