librelist archives

« back to archive

Form handling coming in

Form handling coming in

Loic d'Anterroches
2011-02-27 @ 11:17

just to let you know that I have pushed quite some code into the
repository with respect to form handling. This not perfect, far from it,
for several reasons.

- this is a port of the Pluf form handling with a bit of improvements
based on the current ideas from the current Django code. So, still some
references to the Pluf_* classes/functions here and there.
- not everything works.
- no unit testing.

But, you can do:

    public function testform($request, $match)
        if ('POST' === $request->method) {
            $form = new \photonweb\form\Simple($request->POST);
            if ($form->isValid()) {
                print "Valid form\n";
        } else {
            $form = new \photonweb\form\Simple(array('email'=>'toto'));
                                                    array('form' => $form),

with Simple being:

namespace photonweb\form;

use \photon\form\field;

use \photon\form\Form;
use \photon\form\Invalid;

class Simple extends Form
    public function initFields($extra=array())
        $this->fields['login'] = new field\Varchar(
                                      array('required' => true,
                                            'label' => __('Your login'),
                                            'max_length' => 15,
                                            'min_length' => 3,
                                            'help_text' => __('The login
must be between 3 and 15 characters long and contains only letters and
                                            'widget_attrs' => array(
                                                       'maxlength' => 15,
                                                       'size' => 10,
        $this->fields['email'] = new field\Email(
                                      array('required' => true,
                                            'label' => __('Your email'),
                                            'initial' => '',
                                            'help_text' => __('We will
never send you any unsolicited emails. We hate spams too!'),

        $this->fields['terms'] = new field\Boolean(
                                      array('required' => true,
                                            'label' => __('I agree to
the terms and conditions.'),
                                            'initial' => '',

    public function clean_login()
        $this->cleaned_data['login'] =
        if (preg_match('/[^A-Za-z0-9]/', $this->cleaned_data['login'])) {
            throw new Invalid(sprintf(__('The login "%s" can only
contain letters and digits.'), $this->cleaned_data['login']));
        return $this->cleaned_data['login'];

     * Check the terms.
    public function clean_terms()
        if (!$this->cleaned_data['terms']) {
            throw new Invalid(__('We know, this is boring, but you need
to agree with the terms and conditions.'));
        return $this->cleaned_data['terms'];

    function clean_email()
        $this->cleaned_data['email'] =
        return $this->cleaned_data['email'];

Basically, you have the base stuff, now I need to unit test everything,
especially the checkbox/radio buttons and the file upload. For the file
upload, the multipart encoding parser is smart and do not saturate the
memory, so I need also to show how you can within your form handling,
stream the content of a big file to storage, may it be a file or let say
a MongoFS file storage. But I must say, this is not my top priority as
it is rather simple to do.


Indefero - Project management and code hosting -
Photon - High Performance PHP Framework -
Céondo Ltd - Web + Science = Fun -