Location: PHPKode > projects > Ace Framework > Ace-master/Ace/Form/Adapter/Doctrine/Validator/Choice.php
<?php
/**
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the new BSD license.
 *
 * @package     Ace
 * @license     http://www.opensource.org/licenses/BSD-3-Clause New BSD license
 * @since       1.0
 */
class Ace_Form_Adapter_Doctrine_Validator_Choice extends Ace_Form_Validator
{
    /**
     * Configures the current validator.
     *
     * Available options:
     *
     *  * model:      The model class (required)
     *  * query:      A query to use when retrieving objects
     *  * column:     The column name (null by default which means we use the primary key)
     *                must be in field name format
     *  * multiple:   true if the select tag must allow multiple selections
     *  * min:        The minimum number of values that need to be selected (this option is only active if multiple is true)
     *  * max:        The maximum number of values that need to be selected (this option is only active if multiple is true)
     *
     * @see Ace_Form_Validator
     */
    protected function configure($options = array(), $messages = array())
    {
        $this->addRequiredOption('model');
        $this->addOption('query', null);
        $this->addOption('column', null);
        $this->addOption('multiple', false);
        $this->addOption('min');
        $this->addOption('max');

        $this->addMessage('min', 'At least %min% values must be selected (%count% values selected).');
        $this->addMessage('max', 'At most %max% values must be selected (%count% values selected).');
    }

    /**
     * @see Ace_Form_Validator
     */
    protected function doClean($value)
    {
        if($query = $this->getOption('query'))
        {
            $query = clone $query;
        }
        else
        {
            $query = Doctrine_Core::getTable($this->getOption('model'))->createQuery();
        }

        if($this->getOption('multiple'))
        {
            if(!is_array($value))
            {
                $value = array($value);
            }

            if(isset($value[0]) && !$value[0])
            {
                unset($value[0]);
            }

            $count = count($value);

            if($this->hasOption('min') && $count < $this->getOption('min'))
            {
                throw new Ace_Form_Validator_Exception($this, 'min', array('count' => $count, 'min' => $this->getOption('min')));
            }

            if($this->hasOption('max') && $count > $this->getOption('max'))
            {
                throw new Ace_Form_Validator_Exception($this, 'max', array('count' => $count, 'max' => $this->getOption('max')));
            }

            $query->andWhereIn(sprintf('%s.%s', $query->getRootAlias(), $this->getColumn()), $value);

            if($query->count() != count($value))
            {
                throw new Ace_Form_Validator_Exception($this, 'invalid', array('value' => $value));
            }
        }
        else
        {
            $query->andWhere(sprintf('%s.%s = ?', $query->getRootAlias(), $this->getColumn()), $value);

            if(!$query->count())
            {
                throw new Ace_Form_Validator_Exception($this, 'invalid', array('value' => $value));
            }
        }

        return $value;
    }

    /**
     * Returns the column to use for comparison.
     *
     * The primary key is used by default.
     *
     * @return string The column name
     */
    protected function getColumn()
    {
        $table = Doctrine_Core::getTable($this->getOption('model'));
        if($this->getOption('column'))
        {
            $columnName = $this->getOption('column');
        }
        else
        {
            $identifier = (array) $table->getIdentifier();
            $columnName = current($identifier);
        }

        return $table->getColumnName($columnName);
    }
}
Return current item: Ace Framework