DescX
DescX

Reputation: 334

Smarty dynamic error list

Im new to Smarty in combination with PHP and I really like it. Unfortunatly im running into a problem while validating fields after a $_POST has been done.

I've made an array called $errors and use that to save error messages in, for example: $errors[] .= "Wrong email";. My problem is in sending the $errors array over to the template so I can use it to display the error messages.

My question: How do you 'transfer' the $errors variable over to the template file so you can use it there with, for example {foreach}. I was planning on doing something like

{if $hasErrors} 
    {foreach from=errors item=error}
          <li>{$error}</li>
    {/foreach}
{/if}

Thanks in advance

Upvotes: 0

Views: 114

Answers (1)

Brad
Brad

Reputation: 163436

You can assign arrays to your template like you can any other variable.

$smarty->assign('errors', $errors);

Also, when building your array initially, drop the concatenation operator and just use:

$errors[] = 'Wrong email';

Finally, be sure to initialize your array before attempting to add elements to it, or assigning it.

$errors = array()

EDIT: Now that you've included the additional information, I think the problem has to do with your Smarty tag syntax. Try this line instead, adding a $ to your variable:

{foreach from=$errors item=error}

Upvotes: 1

Related Questions