Преглед изворни кода

Merge remote branch 'mweimerskirch/form_pattern_attribute' into mweimerskirch_merge

Conflicts:
	src/Symfony/Bundle/TwigBundle/Resources/views/Form/div_layout.html.twig
	src/Symfony/Component/Form/Extension/Core/Type/FieldType.php
	src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php
Bernhard Schussek пре 14 година
родитељ
комит
216d7e415d

+ 8 - 0
src/Symfony/Bridge/Doctrine/Form/DoctrineOrmTypeGuesser.php

@@ -180,4 +180,12 @@ class DoctrineOrmTypeGuesser implements FormTypeGuesserInterface
             }
         }
     }
+
+    /**
+     * @inheritDoc
+     */
+    public function guessMinLength($class, $property)
+    {
+        return;
+    }
 }

+ 1 - 0
src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/number_widget.html.php

@@ -5,4 +5,5 @@
     <?php if ($read_only): ?>disabled="disabled"<?php endif ?>
     <?php if ($required): ?>required="required"<?php endif ?>
     <?php if ($max_length): ?>maxlength="<?php echo $max_length ?>"<?php endif ?>
+    <?php if ($pattern): ?>pattern="<?php echo $pattern ?>"<?php endif ?>
 />

+ 1 - 0
src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/password_widget.html.php

@@ -5,4 +5,5 @@
     <?php if ($read_only): ?>disabled="disabled"<?php endif ?>
     <?php if ($required): ?>required="required"<?php endif ?>
     <?php if ($max_length): ?>maxlength="<?php echo $max_length ?>"<?php endif ?>
+    <?php if ($pattern): ?>pattern="<?php echo $pattern ?>"<?php endif ?>
 />

+ 1 - 0
src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/text_widget.html.php

@@ -5,4 +5,5 @@
     <?php if ($read_only): ?>disabled="disabled"<?php endif ?>
     <?php if ($required): ?>required="required"<?php endif ?>
     <?php if ($max_length): ?>maxlength="<?php echo $max_length ?>"<?php endif ?>
+    <?php if ($pattern): ?>pattern="<?php echo $pattern ?>"<?php endif ?>
 />

+ 1 - 1
src/Symfony/Bundle/TwigBundle/Resources/views/Form/div_layout.html.twig

@@ -43,7 +43,7 @@
 
 {% block attributes %}
 {% spaceless %}
-    id="{{ id }}" name="{{ full_name }}"{% if read_only %} disabled="disabled"{% endif %}{% if required %} required="required"{% endif %}{% if max_length %} maxlength="{{ max_length }}"{% endif %}
+    id="{{ id }}" name="{{ full_name }}"{% if read_only %} disabled="disabled"{% endif %}{% if required %} required="required"{% endif %}{% if max_length %} maxlength="{{ max_length }}"{% endif %}{% if pattern %} pattern="{{ pattern }}"{% endif %}
     {% for attrname,attrvalue in attr %}{{attrname}}="{{attrvalue}}" {% endfor %}
 {% endspaceless %}
 {% endblock attributes %}

+ 3 - 0
src/Symfony/Component/Form/Extension/Core/Type/FieldType.php

@@ -44,6 +44,7 @@ class FieldType extends AbstractType
             ->setAttribute('property_path', $options['property_path'])
             ->setAttribute('error_mapping', $options['error_mapping'])
             ->setAttribute('max_length', $options['max_length'])
+            ->setAttribute('pattern', $options['pattern'])
             ->setAttribute('label', $options['label'] ?: $this->humanize($builder->getName()))
             ->setData($options['data'])
             ->addValidator(new DefaultValidator())
@@ -76,6 +77,7 @@ class FieldType extends AbstractType
             ->set('read_only', $form->isReadOnly())
             ->set('required', $form->isRequired())
             ->set('max_length', $form->getAttribute('max_length'))
+            ->set('pattern', $form->getAttribute('pattern'))
             ->set('size', null)
             ->set('label', $form->getAttribute('label'))
             ->set('multipart', false)
@@ -98,6 +100,7 @@ class FieldType extends AbstractType
             'required'          => true,
             'read_only'         => false,
             'max_length'        => null,
+            'pattern'           => null,
             'property_path'     => null,
             'by_reference'      => true,
             'error_bubbling'    => false,

+ 69 - 0
src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php

@@ -64,6 +64,53 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
     }
 
     /**
+<<<<<<< HEAD
+=======
+     * @inheritDoc
+     */
+    public function guessMinLength($class, $property)
+    {
+        $guesser = $this;
+
+        return $this->guess($class, $property, function (Constraint $constraint) use ($guesser) {
+            return $guesser->guessMinLengthForConstraint($constraint);
+        });
+    }
+
+    /**
+     * Iterates over the constraints of a property, executes a constraints on
+     * them and returns the best guess
+     *
+     * @param string $class       The class to read the constraints from
+     * @param string $property    The property for which to find constraints
+     * @param \Closure $guessForConstraint   The closure that returns a guess
+     *                            for a given constraint
+     * @return Guess  The guessed value with the highest confidence
+     */
+    protected function guess($class, $property, \Closure $guessForConstraint)
+    {
+        $guesses = array();
+        $classMetadata = $this->metadataFactory->getClassMetadata($class);
+
+        if ($classMetadata->hasMemberMetadatas($property)) {
+            $memberMetadatas = $classMetadata->getMemberMetadatas($property);
+
+            foreach ($memberMetadatas as $memberMetadata) {
+                $constraints = $memberMetadata->getConstraints();
+
+                foreach ($constraints as $constraint) {
+                    if ($guess = $guessForConstraint($constraint)) {
+                        $guesses[] = $guess;
+                    }
+                }
+            }
+        }
+
+        return Guess::getBestGuess($guesses);
+    }
+
+    /**
+>>>>>>> mweimerskirch/form_pattern_attribute
      * Guesses a field class name for a given constraint
      *
      * @param  Constraint $constraint  The constraint to guess for
@@ -266,6 +313,28 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
         }
     }
 
+    /**
+     * Guesses a field's minimum length based on the given constraint
+     *
+     * @param  Constraint $constraint  The constraint to guess for
+     * @return Guess       The guess for the minimum length
+     */
+    public function guessMinLengthForConstraint(Constraint $constraint)
+    {
+        switch (get_class($constraint)) {
+            case 'Symfony\Component\Validator\Constraints\MinLength':
+                return new ValueGuess(
+                    $constraint->limit,
+                    Guess::HIGH_CONFIDENCE
+                );
+            case 'Symfony\Component\Validator\Constraints\Min':
+                return new ValueGuess(
+                    strlen((string)$constraint->limit),
+                    Guess::HIGH_CONFIDENCE
+                );
+        }
+    }
+
     /**
      * Iterates over the constraints of a property, executes a constraints on
      * them and returns the best guess

+ 9 - 0
src/Symfony/Component/Form/FormFactory.php

@@ -305,6 +305,7 @@ class FormFactory implements FormFactoryInterface
 
         $typeGuess = $this->guesser->guessType($class, $property);
         $maxLengthGuess = $this->guesser->guessMaxLength($class, $property);
+        $minLengthGuess = $this->guesser->guessMinLength($class, $property);
         $requiredGuess = $this->guesser->guessRequired($class, $property);
 
         $type = $typeGuess ? $typeGuess->getType() : 'text';
@@ -313,6 +314,14 @@ class FormFactory implements FormFactoryInterface
             $options = array_merge(array('max_length' => $maxLengthGuess->getValue()), $options);
         }
 
+        if ($minLengthGuess) {
+            if ($maxLengthGuess) {
+                $options = array_merge(array('pattern' => '.{'.$minLengthGuess->getValue().','.$maxLengthGuess->getValue().'}'), $options);
+            } else {
+                $options = array_merge(array('pattern' => '.{'.$minLengthGuess->getValue().',}'), $options);
+            }
+        }
+
         if ($requiredGuess) {
             $options = array_merge(array('required' => $requiredGuess->getValue()), $options);
         }

+ 7 - 0
src/Symfony/Component/Form/FormTypeGuesserChain.php

@@ -61,6 +61,13 @@ class FormTypeGuesserChain implements FormTypeGuesserInterface
         });
     }
 
+    public function guessMinLength($class, $property)
+    {
+        return $this->guess(function ($guesser) use ($class, $property) {
+            return $guesser->guessMinLength($class, $property);
+        });
+    }
+
     /**
      * Executes a closure for each guesser and returns the best guess from the
      * return values

+ 10 - 0
src/Symfony/Component/Form/FormTypeGuesserInterface.php

@@ -42,4 +42,14 @@ interface FormTypeGuesserInterface
      * @return Guess  A guess for the field's maximum length
      */
     function guessMaxLength($class, $property);
+
+    /**
+     * Returns a guess about the field's minimum length
+     *
+     * @param  string $class      The fully qualified class name
+     * @param  string $property   The name of the property to guess for
+     * @return Guess  A guess for the field's minimum length
+     */
+    function guessMinLength($class, $property);
+
 }