Newbie - extend Guestbook

classic Classic list List threaded Threaded
3 messages Options
Roy
Reply | Threaded
Open this post in threaded view
|

Newbie - extend Guestbook

Roy
Just getting back into programming after a 30 year break, so this old man is having trouble getting his head around OOP and frameworks I've worked my way through guestbook, converted it to MySQL, changed the Captcha for an image and added pagination. Now I would like to add a feature where the guest first enters only an email address and a Captcha If there is already an entry for that email then the contents of the Comments field are retrieved and displayed while the guest is informed that only one entry per guest is allowed - but is given the opportunity to update and save the comment If there is no entry then a blank comment field is presented. I'm not sure how to structure this. Single form with hidden fields? different forms? one controller function for all? different controller functions? I could knock this up in minutes with procedural programming, but that's not the object of the exercise Some guidance would be appreciated
Reply | Threaded
Open this post in threaded view
|

Re: Newbie - extend Guestbook

Marco Pivetta
Hi Roy!
I'd say you can use two differently configured forms or add the captcha to
the form only when needed (after all, the captcha is just a form element).
You'd then probably decide which version of the form to use (and then
validate) in the controller based on the result on some check you do
against either the persistence layer or a service layer (if you want to
hide persistence to the controller).

Something like following (pseudocode):


class MyController extends SomeController {

    public function doStuffAction() {
        $baseForm = new FormWithTextareaNameEmailAndSubmit();
        $baseForm->setData($this->getPost());

        if (!$baseForm->isValid()) {
            return $this->failure();
        }

        $email = $baseForm->getValue('email');

        // could be an application resource in zf or directly the DB
connection if you don't want to go that deep
        $service = $this->getService('commentService');


        if ($service->hasCommentsFor($email)) {
            $baseForm->addField(new CaptchaField());

            if (!$baseForm->isValid()) {
                return $this->failure();
            }
        }

        $commentService->addComment($baseForm->getData());

        return $this->success();
    }

    public function success()
    {
        // return whatever needed to produce a success output
    }

    public function failure()
    {
        // return whatever needed to produce a failure output
    }
}

This is framework agnostic, but assuming you are using an MVC controller it
should work :)

Marco Pivetta

http://twitter.com/Ocramius

http://marco-pivetta.com



On 7 September 2012 19:22, Roy <[hidden email]> wrote:

> Just getting back into programming after a 30 year break, so this old man
> is
> having trouble getting his head around OOP and frameworks
>
> I've worked my way through guestbook, converted it to MySQL, changed the
> Captcha for an image and added pagination.
> Now I would like to add a feature where the guest first enters only an
> email
> address and a Captcha
> If there is already an entry for that email then the contents of the
> Comments field are retrieved and displayed while the guest is informed that
> only one entry per guest is allowed - but is given the opportunity to
> update
> and save the comment
> If there is no entry then a blank comment field is presented.
>
> I'm not sure how to structure this. Single form with hidden fields?
> different forms? one controller function for all? different controller
> functions? I could knock this up in minutes with procedural programming,
> but
> that's not the object of the exercise
>
> Some guidance would be appreciated
>
>
>
>
>
> --
> View this message in context:
> http://zend-framework-community.634137.n4.nabble.com/Newbie-extend-Guestbook-tp4656757.html
> Sent from the Zend MVC mailing list archive at Nabble.com.
Roy
Reply | Threaded
Open this post in threaded view
|

Re: Newbie - extend Guestbook

Roy
Hi Marco,
Thanks for the response
In the Guestbook example there is the added complexity of using the Zend Mapper to add another layer of abstraction to the database.

And to add to the woes my local server just decided to die - looks like a MB fault. So once I've sorted out the hardware I'll give your suggestion a try and let you know how I get on

Cheers

Roy