Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

SmartyValidate: Can't get $validate.default.is_error working

 
Post new topic   Reply to topic    Smarty Forum Index -> Add-ons
View previous topic :: View next topic  
Author Message
[ArcanE]
Smarty Rookie


Joined: 06 Apr 2006
Posts: 8

PostPosted: Thu Apr 06, 2006 3:23 pm    Post subject: SmartyValidate: Can't get $validate.default.is_error working Reply with quote

Hi

I've got a nasty problem where i can't get {if $validate.default.is_error}DO SOMETHING{/if} to work

Here is my PHP code:
[php:1:6d0208f1bc]// Smarty validation of forms
// http://www.phpinsider.com/php/code/SmartyValidate/
if(empty($_POST)) {
SmartyValidate::connect($tpl, true); // new form, we (re)set the session data
SmartyValidate::register_validator('val_bedrijf', 'bedrijf', 'notEmpty', false, false, 'trim');
SmartyValidate::register_validator('val_voornaam', 'voornaam', 'notEmpty', false, false, 'trim');
} else {
// validate after a POST
SmartyValidate::connect($tpl);
if(SmartyValidate::is_valid($_POST)) {
// no errors, done with SmartyValidate
SmartyValidate::disconnect();
} else {
// error, redraw the form
$tpl->assign($_POST);
}
}

// DISPLAY =================================================================================
$tpl->display('_layout.tpl');
[/php:1:6d0208f1bc]

Any my template

[php:1:6d0208f1bc]{if $validate.default.is_error}
<div class="error">
<h3>Error with submitting form</h3>
</div>
{/if}


<form action="" method="post" class="profielform">

<fieldset>
<legend>Persoonsgegevens</legend>
<label for="bedrijf">Bedrijf</label>
<input type="text" id="bedrijf" name="bedrijf" />
{validate id="val_bedrijf" field="bedrijf" message="Bedrijf moet ingevuld zijn!"}

<label for="voornaam">Voornaam</label>
<input type="text" id="voornaam" name="voornaam" />
{validate id="val_voornaam" field="voornaam" message="Voornaam moet ingevuld zijn!"}

<label for="naam">Achternaam</label>
<input type="text" id="naam" name="naam" />
</fieldset>


...[/php:1:6d0208f1bc]

I don't know for sure that my PHP code is correct, i just want the form to submit to itself, therefor i just display the form outside the validation if/else logic and don't use separate tpl's for form and result pages. Is smartyValidate able to work like this?
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Thu Apr 06, 2006 8:57 pm    Post subject: Reply with quote

it might have a problem with the {validate} tags in the form after you have done a SmartyValidate::disconnect(), is that the problem you are seeing?
Back to top
View user's profile Send private message Visit poster's website
[ArcanE]
Smarty Rookie


Joined: 06 Apr 2006
Posts: 8

PostPosted: Fri Apr 07, 2006 6:54 am    Post subject: Reply with quote

Validate::disconnect clears the session vars i suppose? That might have caused it ... gonna check it today at work.
Back to top
View user's profile Send private message
[ArcanE]
Smarty Rookie


Joined: 06 Apr 2006
Posts: 8

PostPosted: Fri Apr 07, 2006 7:02 am    Post subject: Reply with quote

No success.
By removing the $validate::disconnect from the PHP page the Session var $_SESSION[smartyValidate][default][is_error] is indeed set to true but in the template {$validate.default.is_error} still gives no results either in a simple output or in an if statement.
Back to top
View user's profile Send private message
[ArcanE]
Smarty Rookie


Joined: 06 Apr 2006
Posts: 8

PostPosted: Fri Apr 07, 2006 7:48 am    Post subject: Reply with quote

One more thing i noticed, when added a {debug} function to my template i can't find the {$validate} var as a registered variable, something must go wrong with assigning the var to smarty?

This only happens when i have submitted the form, when i load the form for the first time the $validate variable is outputted in the debug window. After a submit it's nowhere to be found?
Back to top
View user's profile Send private message
wjdennen
Smarty Rookie


Joined: 23 Oct 2004
Posts: 24

PostPosted: Fri Jun 16, 2006 8:58 pm    Post subject: same problem Reply with quote

I'm having the same problem. In fact, I have a form that always seems to have validation errors, but it should not.

I assigned the $_SESSION variable to smarty and use the {debug} to see it.

Code:

Array (1)
SmartyValidate => Array (1)
  default => Array (4)
    registered_funcs => Array (2)
      criteria => Array (0)
      transform => Array (0)
    validators => Array (5)
      0 => Array (9)
        field => name
        id => svc-name
        criteria => notEmpty
        message => <span class='notice'>& Service ...
        trim =>
        empty =>
        halt =>
        transform => empty
        valid => 1
      1 => Array (9)
        field => reason
        id => svc-reason
        criteria => notEmpty
        message => <span class='notice'>& Service ...
        trim =>
        empty =>
        halt =>
        transform => empty
        valid => 1
      2 => Array (9)
        field => description
        id => svc-description
        criteria => notEmpty
        message => <span class='notice'>& Service ...
        trim =>
        empty =>
        halt =>
        transform => empty
        valid => 1
      3 => Array (9)
        field => service_status
        id => svc-status
        criteria => notEmpty
        message => <span class='notice'>& Service ...
        trim =>
        empty =>
        halt =>
        transform => empty
        valid => 1
      4 => Array (9)
        field => url
        id => svc-url
        criteria => isURL
        message => <span class='notice'>& Please e...
        trim =>
        empty => 1
        halt =>
        transform => empty
        valid => 1
    is_error => 1
    is_init =>


Note how all of the fields in the form are valid, but is_error is still set to "1".

This doesn't seem right.

{$validate.default.is_error} doesn't retun anything in the template either.

I'm perplexed. The submission should be valid.

Thanks for any help.
Back to top
View user's profile Send private message
wjdennen
Smarty Rookie


Joined: 23 Oct 2004
Posts: 24

PostPosted: Fri Jun 16, 2006 9:28 pm    Post subject: isURL Reply with quote

I think the problem might be with isURL. When I comment out the lines that register this check, things work fine.
Back to top
View user's profile Send private message
reywob
Smarty n00b


Joined: 22 Aug 2003
Posts: 3

PostPosted: Sat Aug 05, 2006 10:06 pm    Post subject: Still a valid problem... Reply with quote

I've encountered this problem and unless I'm missing something, it's a bug in SmartyValidate.

When I first load my form {$validate} is set:
Code:
Array (2)
default => Array (1)
  is_error =>
commentform => Array (1)
  is_error =>


If I hit the submit button immediately (so no fields filled out) $validate is not set. This is because SmartyValidate::_smarty_assign() is not called anywhere when the form has errors.

This can be fixed by the hack in my code:
[php:1:55092a1bb1] SmartyValidate::connect($this->smarty);
if(SmartyValidate::is_valid($data)) {
// no errors, done with SmartyValidate
SmartyValidate::disconnect();
}
### Hack for internal SmartyValidate bug
SmartyValidate::_smarty_assign();[/php:1:55092a1bb1]

This fixes the problem for the default form. However for my named form the problem still exists:
Code:
   Array (2)
default => Array (1)
  is_error => 1
commentform => Array (1)
  is_error =>


I'm not sure what the problem is here. I've tried modifying my code above to
[php:1:55092a1bb1] SmartyValidate::connect($this->smarty);
SmartyValidate::register_form('commentform');[/php:1:55092a1bb1]

but no luck.

Update
This was when I hadn't added the form="formname" to my {validate} tags. As soon as I've done that it doesn't show any error messages, or display any sign of the form being validated at all.[/b]
Back to top
View user's profile Send private message Visit poster's website
reywob
Smarty n00b


Joined: 22 Aug 2003
Posts: 3

PostPosted: Sun Aug 06, 2006 3:13 pm    Post subject: Solved it - but why this way? Reply with quote

Right, cracked the problem. It was my fault for not reading the API documentation fully, but SmartyValidate behaves differently to how I believe it should.

The conceptual problem is that registering a form with SmartyValidate does not automatically register the rules with that form (that's OK, I understand why) - but neither does it make the ::is_valid method be bound to the form.

Why was the decision made to work with everything called statically? To me it would be more natural to register a form and get a return object. Then every call after this was with respect to this form - and another object could be instantiated for another form. Clearly there's a design reason I'm missing here as SmartyValidate doesn't work that way - could someone enlighten me?
Back to top
View user's profile Send private message Visit poster's website
kaandorp
Smarty n00b


Joined: 18 Oct 2006
Posts: 1

PostPosted: Wed Oct 18, 2006 7:33 pm    Post subject: Reply with quote

I see you cracked the problem, but unfortunately I don't really understand your solution. Can you perhaps adjust the example you gave in the first post, so it will work?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Add-ons All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP