<commit_before><?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 <NAME> and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */

/**
 * Form validation class. 
 *
 * This class is used to generate a form. You basically build it the
 * same way you build a model.
 *
 * The form handling is heavily inspired by the Django form handling.
 *
 */
class Pluf_Form implements Iterator, ArrayAccess
{
    /**
     * The fields of the form. 
     *
     * They are the fully populated Pluf_Form_Field_* of the form. You
     * define them in the initFields method.
     */
    public $fields = array();

    /**
     * Prefix for the names of the fields.
     */
    public $prefix = '';
    public $id_fields = 'id_%s';
    public $data = array();
    public $cleaned_data = array();
    public $errors = array();
    public $is_bound = false;
    public $f = null;
    public $label_suffix = ':';

    protected $is_valid = null;

    function __construct($data=null, $extra=array(), $label_suffix=null)
    {
        if ($data !== null) {
            $this->data = $data;
            $this->is_bound = true;
        }
        if ($label_suffix !== null) $this->label_suffix = $label_suffix;

        $this->initFields($extra);
        $this->f = new Pluf_Form_FieldProxy($this);
    }

    function initFields($extra=array())
    {
        throw new Exception('Definition of the fields not implemented.');
    }

    /**
     * Add the prefix to the form names.
     *
     * @param string Field name.
     * @return string Field name or field name with form prefix.
     */
    function addPrefix($field_name)
    {
        if ('' !== $this->prefix) {
            return $this->prefix.'-'.$field_name;
        }
        return $field_name;
    }

    /**
     * Check if the form is valid.
     *
     * It is also encoding the data in the form to be then saved.  It
     * is very simple as it leaves the work to the field. It means
     * that you can easily extend this form class to have a more
     * complex validation procedure like checking if a field is equals
     * to another in the form (like for password confirmation) etc.
     *
     * @param array Associative array of the request
     * @return array Array of errors
     */
    function isValid()
    {
        if ($this->is_valid !== null) {
            return $this->is_valid;
        }
        $this->cleaned_data = array();
        $this->errors = array();
        $form_methods = get_class_methods($this);
        foreach ($this->fields as $name=>$field) {
            $value = $field->widget->valueFromFormData($this->addPrefix($name),
                                                       $this->data); 
            try {
                $value = $field->clean($value);
                $this->cleaned_data[$name] = $value;
                if (in_array('clean_'.$name, $form_methods)) {
                    $m = 'clean_'.$name;
                    $value = $this->$m();
                    $this->cleaned_data[$name] = $value;
                }                        
            } catch (Pluf_Form_Invalid $e) {
                if (!isset($this->errors[$name])) $this->errors[$name] = array();
                $this->errors[$name][] = $e->getMessage();<commit_msg>Add support for closures / lambdas as per-field validation methods<commit_after><?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 <NAME> and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */

/**
 * Form validation class. 
 *
 * This class is used to generate a form. You basically build it the
 * same way you build a model.
 *
 * The form handling is heavily inspired by the Django form handling.
 *
 */
class Pluf_Form implements Iterator, ArrayAccess
{
    /**
     * The fields of the form. 
     *
     * They are the fully populated Pluf_Form_Field_* of the form. You
     * define them in the initFields method.
     */
    public $fields = array();

    /**
     * Prefix for the names of the fields.
     */
    public $prefix = '';
    public $id_fields = 'id_%s';
    public $data = array();
    public $cleaned_data = array();
    public $errors = array();
    public $is_bound = false;
    public $f = null;
    public $label_suffix = ':';

    protected $is_valid = null;

    function __construct($data=null, $extra=array(), $label_suffix=null)
    {
        if ($data !== null) {
            $this->data = $data;
            $this->is_bound = true;
        }
        if ($label_suffix !== null) $this->label_suffix = $label_suffix;

        $this->initFields($extra);
        $this->f = new Pluf_Form_FieldProxy($this);
    }

    function initFields($extra=array())
    {
        throw new Exception('Definition of the fields not implemented.');
    }

    /**
     * Add the prefix to the form names.
     *
     * @param string Field name.
     * @return string Field name or field name with form prefix.
     */
    function addPrefix($field_name)
    {
        if ('' !== $this->prefix) {
            return $this->prefix.'-'.$field_name;
        }
        return $field_name;
    }

    /**
     * Check if the form is valid.
     *
     * It is also encoding the data in the form to be then saved.  It
     * is very simple as it leaves the work to the field. It means
     * that you can easily extend this form class to have a more
     * complex validation procedure like checking if a field is equals
     * to another in the form (like for password confirmation) etc.
     *
     * @param array Associative array of the request
     * @return array Array of errors
     */
    function isValid()
    {
        if ($this->is_valid !== null) {
            return $this->is_valid;
        }
        $this->cleaned_data = array();
        $this->errors = array();
        $form_methods = get_class_methods($this);
        $form_vars = get_object_vars($this);
        foreach ($this->fields as $name=>$field) {
            $value = $field->widget->valueFromFormData($this->addPrefix($name),
                                                       $this->data); 
            try {
                $value = $field->clean($value);
                $this->cleaned_data[$name] = $value;
                $method = 'clean_'.$name;
                if (in_array($method, $form_methods)) {
                    $value = $this->$method();
                    $this->cleaned_data[$name] = $value;
                } else if (array_key_exists($method, $form_vars) &&
                           is_callable($this->$method)) {
                    $value = call_user_func($this->$method, $this);
                    $this->cleaned_data[$name] = $value;
                }                        
            } catch (Pluf_Form_Invalid $e) {
                if (!isset($this->errors[$name])) $this->errors[$name] = array();
                $this->errors[$name][] = $e->getMessage();
{'fraction_non_alphanumeric': 0.11748316693754353, 'fraction_numerical': 0.005340143951706525, 'mean_word_length': 2.1895594224361346, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3695503', 'n_tokens_mistral': 2441, 'n_tokens_neox': 2334, 'n_words': 859}