On Wed, 2003-11-12 at 04:31, Robert Ian Smit wrote:
> I am trying to implement a generic form handler that is capable of
> printing the form and checking the user input.
> 
> I want this application to be useful in the end, but I also use it
> to explore OOP in PHP.
> 
> The working code at the moment is able to add HTML input elements to
> the page. These fields are all objects that get added to the Form
> object. When the user submits, the Form object tells all elements to
> check their input and create error messages if needed.
> 
> So far so good. One of the intended applications is to store
> addresses submitted by visitors to our website. We have users from
> two countries, with two different zip-code systems.
> 
> I could ofcourse create a double check in the zipcode-object, but
> this would mean that it would be possible to submit a zipcode from
> the other country without any objection by the application.
> 
> I'd like the zipcode object to ask a question to find out what
> country we're dealing with and then apply the check of the input
> depending on the answer.
> 
> Although these questions are specific to my application, I guess my
> real question is more general. When you have a containerobject that
> stores different kinds of elements, how do you give the elements the
> ability to learn about their surroundings so they can alter their
> behaviour? 
> 
> I know have:
> 
> $foo = new Form();
> $foo->addToForm(new Element('country'));
> $foo->addToForm(new Element('zipcode'));
> $foo->processForm();
> 
> 
> I hope I made clear what it is I am trying to do. Hopefully I'll
> receive a few hints to help me on my way.

Give elements the ability to access their own form object. Then they can
request elements belonging to their own form for complex validation. For
instance:

function validateZip( &$element )
{
    $form = $element->getForm();

    $country = $form->getElement( 'country' );
    if( $country->getValue() == 'Some country' )
    {
        // ZIP must meet format for "Some country".
    }
    else
    if( $country->getValue() == 'Some other country' )
    {
        // ZIP must meet format for "Some other country".
    }
    else
    {
        // Default case.
    }
}

HTH,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to