0

Here is my typical form

        $errors = array();

        if ($this->request->post('submit')) { // <----- I don't like this line
            $post = Validation::factory($this->request->post())
                ->rule('email', 'not_empty')
                ->rule('email', 'email')
                ->rule('password', 'not_empty');

            if ($post->check()) {
                // ok, do something
            }

            $errors = $post->errors(true);
        }

        $this->template->content = View::factory('auth/register')
            ->set('errors', $errors);

As you see - I check if there is a submit element which means that we have actually posted form, not just requested for the first show.

If we remove that condition - we will have validation errors for the first page request. The errors about empty email and password form. Which is actually just incorrect.

So how do you solve this issue?

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • and what exactly is the issue? you want to check for POST without checking for POST ? – Kemo Jun 01 '11 at 12:42
  • @Shakti Singh: it is subjective. I don't like it and expect it is some hack to avoid it ;-) – zerkms Jun 01 '11 at 12:42
  • @Kemo: yes. I'm asking about common practice. I don't believe everyone put same line for every form validation – zerkms Jun 01 '11 at 12:43
  • @zerkms: subjective, I can understand. Hopes there should be an alternate but I don't think there is. Good Luck – Shakti Singh Jun 01 '11 at 12:47
  • Also, why dont redirect user after validation (even it fails) to prevent confusing page refresh? – biakaveron Jun 01 '11 at 13:21
  • @biakaveron: I do so. This is not the topic so I removed all irrelevant code. So redirection is inside `// ok, do something` piece – zerkms Jun 01 '11 at 13:33
  • @zerkms I mean if validation _fails_, you will show registration view _without_ redirection. – biakaveron Jun 01 '11 at 13:44
  • @biakaveron: yes, and that is correct. No redirect needed to be performed if *validation failed*. Otherwise you need some storage to keep entered values. Don't see any reasons for that. – zerkms Jun 01 '11 at 13:47

2 Answers2

3

This is how I'd do it, except for the condition:

if (Request::POST === $this->request->method())

would be more suitable. There is no way to "skip" the POST check without having consequences (like the errors in your case).

We had a discussion on this topic, 5.3 will probably add more features. Something like:

$this->post(function(){
    // do POST-specific stuff 
})
->get(function(){
    // do GET-specific stuff
});
Kemo
  • 6,942
  • 3
  • 32
  • 39
  • @zerkms for a couple years now, yes – Kemo Jun 01 '11 at 12:50
  • @Kemo: so you put `if (Request::POST === $this->request->method())` everywhere before post validations? PS: what is `$this->post()`? – zerkms Jun 01 '11 at 13:07
  • @zerkms: yes, that's the correct way for doing it "HMVC compatible" way, altough it seems quite long. You can also use if ($this->request->post()) to check if POST array will evaluate to TRUE, though that only checks if there is some POST data, not if the request type is POST. And concerning $this->post() etc., that could become the way of doing this using closures when Kohana gets to 5.3 – Kemo Jun 01 '11 at 13:12
  • @Kemo: uhm, what `$this` object supposed to have `post()` method? – zerkms Jun 01 '11 at 13:32
  • @zerkms: that would be the controller scope. Notice that this hasn't yet been implemented (though it can be on app level, easily), it's just one of the ideas. – Kemo Jun 01 '11 at 13:34
1
if ($post = $this->request->post())
{
    $post = Validation::factory($post);
    ...
}
biakaveron
  • 5,493
  • 1
  • 16
  • 20