Commit Graph

2045 Commits

Author SHA1 Message Date
Bernhard Schussek
2e4ebe444f [Validator] Renamed methods addViolationAtRelativePath() and getAbsolutePropertyPath() in ExecutionContext 2012-02-02 10:13:41 +01:00
Bernhard Schussek
9153f0e569 [Validator] Deprecated ConstraintValidator methods setMessage(), getMessageTemplate() and getMessageParameters()
Had to refactor the validation tests at the same time and fixed various small bugs while doing so.
2012-02-01 14:03:13 +01:00
Bernhard Schussek
0417282740 [Validator] Fixed typos 2012-02-01 09:21:22 +01:00
Bernhard Schussek
a30a679135 [Validator] Made ExecutionContext immutable and introduced new class GlobalExecutionContext
A new ExecutionContext is now created everytime that GraphWalker::walkConstraint() is
launched. Because of this, a validator B launched from within a validator A can't break
A anymore by changing the context.

Because we have a new ExecutionContext for every constraint validation, there is no point
in modifying its state anymore. Because of this it is now immutable.
2012-01-31 21:35:48 +01:00
Bernhard Schussek
fe85bbdb06 [Validator] Simplified ExecutionContext::addViolation(), added ExecutionContext::addViolationAt() 2012-01-31 18:42:14 +01:00
Bernhard Schussek
a103c28b08 [Validator] The Collection constraint adds "missing" and "extra" errors to the individual fields now 2012-01-30 20:57:20 +01:00
Bernhard Schussek
f904a9ed53 [Validator] Fixed: GraphWalker does not add constraint violation if error message is empty 2012-01-30 20:56:10 +01:00
Bernhard Schussek
1678a3dbdf [Validator] Fixed: Validator::validateValue() propagates empty validation root instead of the provided value 2012-01-30 18:43:47 +01:00
Bernhard Schussek
5aa5987aa4 [Form] Fixed: form children are always validated in group "Default" 2012-01-30 13:42:13 +01:00
Fabien Potencier
916597eb29 fixed CS, phpdoc, removed unused use statements 2012-01-28 18:02:36 +01:00
Fabien Potencier
5e0823c99c merged branch bschussek/issue1919 (PR #3156)
Commits
-------

8dc78bd [Form] Fixed YODA issues
600cec7 [Form] Added missing entries to CHANGELOG and UPGRADE
b154f7c [Form] Fixed docblock and unneeded use statement
399af27 [Form] Implemented checks to assert that values and indices generated in choice lists match their requirements
5f6f75c [Form] Fixed outstanding issues mentioned in the PR
7c70976 [Form] Fixed text in UPGRADE file
c26b47a [Form] Made query parameter name generated by ORMQueryBuilderLoader unique
18f92cd [Form] Fixed double choice fixing
f533ef0 [Form] Added ChoiceView class for passing choice-related data to the view
d72900e [Form] Incorporated changes suggested in PR comments
28d2f6d Removed duplicated lines from UPGRADE file
e1fc5a5 [Form] Restricted form names to specific characters to (1) fix generation of HTML IDs and to (2) avoid problems with property paths.
87b16e7 [Form] Greatly improved ChoiceListInterface and all of its implementations

Discussion
----------

[Form] Improved ChoiceList implementation and made form naming more restrictive

Bug fix: yes
Feature addition: yes
Backwards compatibility break: **yes**
Symfony2 tests pass: yes
Fixes the following tickets: #2869, #3021, #1919, #3153
Todo: adapt documentation

![Travis Build Status](https://secure.travis-ci.org/bschussek/symfony.png?branch=issue1919)

The changes in this PR are primarily motivated by the fact that invalid form/field names lead to various problems.

1. When a name contains any characters that are not permitted in HTML "id" attributes, these are invalid
2. When a name contains periods ("."), form validation is broken, because they confuse the property path resolution
3. Since choices in expanded choice fields are directly translated to field names, choices applying to either 1. or 2. lead to problems. But choices should be unrestricted.
4. Unless a choice field is not expanded and does not allow multiple selection, it is not possible to use empty strings as choices, which might be desirable in some occasions.

The solution to these problems is to

* Restrict form names to disallow unpermitted characters (solves 1. and 2.)
* Generate integer indices to be stored in the HTML "id" and "name" attributes and map them to the choices (solves 3.). Can be reverted to the old behaviour by setting the option "index_generation" to ChoiceList::COPY_CHOICE
* Generate integer values to be stored in the HTML "value" attribute and map them to the choices (solves 4.). Can be reverted to the old behaviour by setting the option "value_generation" to ChoiceList::COPY_CHOICE

Apart from these fixes, it is now possible to write more flexible choice lists. One of these is `ObjectChoiceList`, which allows to use objects as choices and is bundled in the core. `EntityChoiceList` has been made an extension of this class.

    $form = $this->createFormBuilder()
        ->add('object', 'choice', array(
            'choice_list' => new ObjectChoiceList(
                array($obj1, $obj2, $obj3, $obj4),
                // property path determining the choice label (optional)
                'name',
                // preferred choices (optional)
                array($obj2, $obj3),
                // property path for object grouping (optional)
                'category',
                // property path for value generation (optional)
                'id',
                // property path for index generation (optional)
                'id'
            )
        ))
        ->getForm()
    ;

---------------------------------------------------------------------------

by kriswallsmith at 2012-01-19T18:09:09Z

Rather than passing `choices` and a `choice_labels` arrays to the view would it make sense to introduce a `ChoiceView` class and pass one array of objects?

---------------------------------------------------------------------------

by stof at 2012-01-22T15:32:36Z

@bschussek can you update your PR according to the feedback (and rebase it as it conflicts according to github) ?

---------------------------------------------------------------------------

by bschussek at 2012-01-24T00:15:42Z

@kriswallsmith fixed

Fixed all outstanding issues. Would be glad if someone could review again, otherwise this PR is ready to merge.

---------------------------------------------------------------------------

by fabpot at 2012-01-25T15:17:59Z

Is it ready to be merged?

---------------------------------------------------------------------------

by Tobion at 2012-01-25T15:35:50Z

Yes I think so. He said it's ready to be merged when reviewed.

---------------------------------------------------------------------------

by bschussek at 2012-01-26T02:30:36Z

Yes.

---------------------------------------------------------------------------

by bschussek at 2012-01-28T12:39:00Z

Fixed outstanding issues. Ready for merge.
2012-01-28 15:19:10 +01:00
Fabien Potencier
a72bf897d3 merged branch vicb/profiler (PR #3190)
Commits
-------

b879397 [Profiler] Optimize time panel IS
d4300b9 [WebProfilerBundle] Tweak the time view
416a2a4 [Stopwatch] Fix some logic
8c3505e [Profiler] Tweak PHPDoc
3bcd154 [HttpKernel] Tweak the Profile class - DRY

Discussion
----------

[Profiler] Stopwatch related tweaks

* Some fixes in the stopwatch logic,
* Some JS fixes,
* Make use of modern JS.
2012-01-28 15:16:32 +01:00
Bernhard Schussek
0533c1b40f [Form] Fixed: IntegerToLocalizedStringTransformer does not accept "NaN" as valid number anymore 2012-01-28 14:21:37 +01:00
Bernhard Schussek
8c63d6d1e5 [Form] Fixed: NumberToLocalizedStringTransformer does not accept "NaN" as valid number anymore 2012-01-28 14:14:56 +01:00
Ivan Kurnosov
aaa9de6563 Added test case for checking that 'NaN' string converts into TransformationFailedException in NumberToLocalizedStringTransformer 2012-01-27 13:41:03 +13:00
Lars Strojny
746170bbe2 Make method non static 2012-01-25 14:56:27 +01:00
Lars Strojny
c3f637b834 PATCH support and tests for DELETE support 2012-01-25 14:54:48 +01:00
Victor Berchet
416a2a46df [Stopwatch] Fix some logic 2012-01-25 14:27:59 +01:00
Lars Strojny
2dd4bf1283 Support for PATCH method in forms 2012-01-24 19:46:37 +01:00
Bernhard Schussek
399af275ac [Form] Implemented checks to assert that values and indices generated in choice lists match their requirements 2012-01-24 12:21:25 +01:00
Victor Berchet
43e0db5f75 [DomCrawler] Add support for multivalued form fields (fix #1579, #3012) 2012-01-24 09:28:29 +01:00
Bernhard Schussek
f533ef0e1b [Form] Added ChoiceView class for passing choice-related data to the view 2012-01-24 01:07:33 +01:00
Bernhard Schussek
d72900e613 [Form] Incorporated changes suggested in PR comments 2012-01-23 18:58:56 +01:00
Bernhard Schussek
e1fc5a5c8c [Form] Restricted form names to specific characters to (1) fix generation of HTML IDs and to (2) avoid problems with property paths.
ad (1): HTML4 "id" attributes are limited to strings starting with a letter and containing only letters, digits, underscores, hyphens, periods and colons.

ad (2): Property paths contain three special characters needed for correct parsing: left/right bracket and period.

The rules for form naming are:

* Names may start with a letter, a digit or an underscore. Leading digits or underscores will be stripped from the "id" attributes.
* Names must only contain letters, digits, underscores, hyphens and colons.
* Root forms may have an empty name.

Solves #1919 and #3021 on a wider scope.
2012-01-23 18:28:25 +01:00
Bernhard Schussek
87b16e7015 [Form] Greatly improved ChoiceListInterface and all of its implementations
Fixes #2869, fixes #3021, fixes #1919, fixes #3153.
2012-01-23 18:28:25 +01:00
Christophe Coevoet
e37783f4f9 [DoctrineBridge] Refactored the query sanitization in the collector
The original parameters are kept whenever possible to allow using them
again to explain the query.
2012-01-23 10:57:46 +01:00
Christophe Coevoet
3b260d268b Refactored the collector to separate the loggers per connection 2012-01-23 09:22:30 +01:00
Fabien Potencier
3749355ba7 removed debug code 2012-01-22 17:21:04 +01:00
Fabien Potencier
35a61b3a52 [HttpKernel] added arguments to ExceptionHandler (closes #2739) 2012-01-22 16:53:27 +01:00
Fabien Potencier
63adb97cf2 Revert "merged branch blogsh/dynamic_constraints (PR #3114)"
This reverts commit 6b9a355fb0, reversing
changes made to 811ead8589.
2012-01-22 16:50:02 +01:00
Fabien Potencier
8d79ebc8ce [HttpKernel] added some unit tests for ExceptionHandler and
FlattenException
2012-01-22 11:19:40 +01:00
Fabien Potencier
8358cbf7a6 merged branch kriswallsmith/csrf-token-helper (PR #3080)
Commits
-------

753c067 [FrameworkBundle] added $view['form']->csrfToken() helper
e1aced8 [Twig] added {{ csrf_token() }} helper

Discussion
----------

[Twig] [FrameworkBundle] added CSRF token helper

I've added a templating helper and Twig function for generating a CSRF token without the overhead of creating a form.

```html+jinja
<form action="{{ path('user_delete', { 'id': user.id }) }}" method="post">
    <input type="hidden" name="_method" value="delete">
    <input type="hidden" name="_token" value="{{ csrf_token('delete_user_' ~ user.id) }}">
    <button type="submit">delete</button>
</form>
```

```php
<?php

class UserController extends Controller
{
    public function delete(User $user, Request $request)
    {
        $csrfProvider = $this->get('form.csrf_provider');
        if (!$csrfProvider->isCsrfTokenValid('delete_user_'.$user->getId(), $request->request->get('_token')) {
            throw new RuntimeException('CSRF attack detected.');
        }

        // etc...
    }
}
```

The test that is failing on Travis appears to be unrelated, but I may be wrong?

```
1) Symfony\Bundle\SecurityBundle\Tests\Functional\LocalizedRoutesAsPathTest::testLoginLogoutProcedure with data set #1 ('de')
RuntimeException: OUTPUT:
Catchable fatal error: Argument 3 passed to Symfony\Bundle\FrameworkBundle\Controller\TraceableControllerResolver::__construct() must be an instance of Symfony\Component\HttpKernel\Debug\Stopwatch, instance of Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser given, called in /tmp/2.1.0-DEV/StandardFormLogin/cache/securitybundletest/appSecuritybundletestDebugProjectContainer.php on line 94 and defined in /home/vagrant/builds/kriswallsmith/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/TraceableControllerResolver.php on line 37
```

---------------------------------------------------------------------------

by pablodip at 2012-01-10T14:18:45Z

As you don't need forms to use the csrf provider, how about putting its service without the form prefix? It could even make sense to put the CsrfProvider as a component since you can use it standalone and in more cases than only forms. It would be a small component though.

---------------------------------------------------------------------------

by Tobion at 2012-01-10T17:54:14Z

I think it would be more clear to generate the token in the controller. Doing so in the template will spread the CSRF intention across template and controller. So I don't think this extension is necessary.

---------------------------------------------------------------------------

by kriswallsmith at 2012-01-10T17:58:14Z

@pablodip I'm open to the idea of a Csrf component. This would be a good place for some nonce classes as well.

@Tobion I disagree. One use case is for a list of users, each with a delete form. Iterating over the users in the controller and generating a token for each, just to iterate over them again in the view is a waste and adds complexity.

---------------------------------------------------------------------------

by Tobion at 2012-01-10T18:05:14Z

I see. But I don't understand why the intention needs to be different for each user to delete. Usually the intention is the same for each form type. I thought this is enough.

---------------------------------------------------------------------------

by kriswallsmith at 2012-01-10T18:06:13Z

Yes, a static intention would suffice.

---------------------------------------------------------------------------

by Tobion at 2012-01-10T18:07:08Z

Then your use case is not valid anymore.

---------------------------------------------------------------------------

by Tobion at 2012-01-10T18:12:25Z

I would suggest to make a cookbook article out of it about how to create a simple form without the form component.
And include such things as validating the result using the validator component and checking the CSRF.

---------------------------------------------------------------------------

by kriswallsmith at 2012-01-10T21:32:50Z

This helper makes it easier to use CSRF protection without a form and we should make it as easy as possible. Spreading the intention across controller and template is not concerning to me. Either way, a cookbook entry is a great idea.

---------------------------------------------------------------------------

by Tobion at 2012-01-10T21:47:12Z

Well, it's just one line more without this helper. So I disagree it makes it really easier when you know how to use the CsrfProvider which is a pre-condition anyway since you must still validate its correctness by hand.

---------------------------------------------------------------------------

by kriswallsmith at 2012-01-13T13:24:15Z

Another use case is when rendering a page with a bunch of simple buttons with different intentions: delete user, delete comment, follow, unfollow... Creating all of these in the controller just leads to spaghetti.

---------------------------------------------------------------------------

by jwage at 2012-01-17T21:55:53Z

👍 lots of use cases for something like this @OpenSky
2012-01-22 10:31:29 +01:00
Fabien Potencier
6b9a355fb0 merged branch blogsh/dynamic_constraints (PR #3114)
Commits
-------

92f820a Renamed registerConstraints to loadDynamicValidatorMetadata
dd12ff8 CS fix, getConstraints renamed
09c1911 [Validator] Improved dynamic constraints
54cb6e4 [Validator] Added dynamic constraints

Discussion
----------

[Validator] Dynamic constraints

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes

By now the Validator component is based on a per-class configuration of
constraints, but in some cases it might be neccessary to add new constraints
dynamically at runtime.
This pull request adds a "ConstraintProviderInterface" to the Validator component. If an object is validated that implements this interface the method "getConstraints" is used to add dynamic constraints:

    class User implements ConstraintProviderInterface
    {
        protected $isPremium;
        protected $paymentInformation;

        public function getConstraints(ClassMetadata $metadata)
        {
            if ($this->isPremium) {
                $metadata->addPropertyConstraint('paymentInformation', new NotBlank());
            }
        }
    }

---------------------------------------------------------------------------

by alexandresalome at 2012-01-15T11:20:04Z

Related to #1151

---------------------------------------------------------------------------

by canni at 2012-01-16T09:22:28Z

👍

---------------------------------------------------------------------------

by bschussek at 2012-01-16T12:32:44Z

I think this is a good addition. I think we still have a naming problem though. When constraints are loaded using a static method, the default name for the loader method is `loadValidatorMetadata`. Since the method for dynamic constraint loading is basically the same, I think the two names should be related.

Solution (1): Rename the method in your interface to `loadDynamicValidatorMetadata`. Ugly and long.

    class MyClass implements ConstraintProviderInterface
    {
        public static loadValidatorMetadata(ClassMetadata $metadata) ...

        public loadDynamicValidatorMetadata(ClassMetadata $metadata) ...
    }

Solution (2): Rename the default method name in `StaticMethodLoader` to `registerConstraints` and adjust the docs. Breaks BC.

    class MyClass implements ConstraintProviderInterface
    {
        public static registerConstraints(ClassMetadata $metadata) ...

        public registerDynamicConstraints(ClassMetadata $metadata) ...
    }

@fabpot: Are we allowed to break BC here? If not, we should probably stick to (1).

---------------------------------------------------------------------------

by fabpot at 2012-01-16T12:36:14Z

I would prefer to not break BC if possible.

---------------------------------------------------------------------------

by blogsh at 2012-01-16T15:25:46Z

So "loadDynamicValidatorMetadata" would be the best solution?

---------------------------------------------------------------------------

by althaus at 2012-01-17T13:39:19Z

>So "loadDynamicValidatorMetadata" would be the best solution?

Sounds fine for me based on @bschussek's comment.
2012-01-22 10:26:39 +01:00
Fabien Potencier
811ead8589 [Serializer] fixed unit tests after previous merge + added a new one 2012-01-22 10:20:46 +01:00
Fabien Potencier
90fcbde685 merged branch canni/fix_cs (PR #3146)
Commits
-------

3cfaade [CS] Fix usage of assertCount

Discussion
----------

[CS] Fix usage of assertCount

Bug fix: no
Feature addition: no
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: -
Todo: -

[![Build Status](https://secure.travis-ci.org/canni/symfony.png)](http://travis-ci.org/canni/symfony)
2012-01-22 10:15:34 +01:00
Fabien Potencier
c7ec49c624 merged branch lstrojny/feature/form-http-delete (PR #3159)
Commits
-------

0b7e2e0 Support for DELETE method in forms

Discussion
----------

[Form] Support DELETE HTTP verb

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: none
Todo: -

As `Symfony\Component\HttpFoundation\Request` already support DELETE requests nicely by parsing the request for us, support for the HTTPs DELETE verb can be easily done.

---------------------------------------------------------------------------

by mvrhov at 2012-01-20T06:00:49Z

This is wrong. The body for DELETE method is supposed to be empty or if present ignored.
Also the DELETE is supposed to remove the resource identified by uri, so the same code as for GET should be executed.

---------------------------------------------------------------------------

by lstrojny at 2012-01-20T08:56:22Z

I don’t think that’s the case. The HTTP standard does not state explicitly that DELETE does not have a body. See this [StackOverflow thread](http://stackoverflow.com/questions/2539394/rest-http-delete-and-parameters)
2012-01-22 10:05:04 +01:00
Fabien Potencier
a5220313f6 Merge branch '2.0'
* 2.0:
  Updated Serbian translation.
  fixed CS
  [Locale][Testing] Fixed breaking tests if 'intl' extension is not installed (#3139)
  [Bridge] [Twig] fixed typo in a comment of the Twig FormExtension extension.
2012-01-22 07:33:58 +01:00
Lars Strojny
0b7e2e035a Support for DELETE method in forms 2012-01-20 01:04:31 +01:00
Sebastian Hörl
92f820a094 Renamed registerConstraints to loadDynamicValidatorMetadata 2012-01-18 22:24:42 +01:00
Dariusz Górecki
3cfaade8f7 [CS] Fix usage of assertCount
Bug fix: no
Feature addition: no
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: -
Todo: -
2012-01-18 14:42:47 +01:00
Manuel Kiessling
a1317c3437 [Locale][Testing] Fixed breaking tests if 'intl' extension is not installed (#3139)
Symfony\Tests\Component\Locale\LocaleTest->testGetDisplayCountriesReturnsFullListForSubLocale()
fails with a fatal error if the PHP extension 'intl' is not installed on the system.
Added a check which skips the affected tests if the extension is not available.
Fixes #3139
2012-01-17 18:44:55 +01:00
Fabien Potencier
e8f9a55012 fixed CS 2012-01-17 11:23:46 +01:00
Fabien Potencier
9c3c53a5c1 merged 2.0 2012-01-17 11:23:18 +01:00
Fabien Potencier
51ecb3c07b fixed CS 2012-01-17 10:56:02 +01:00
Fabien Potencier
733ac9de7a [HttpFoundation] fixed exception message (closes #3123) 2012-01-16 22:09:07 +01:00
Fabien Potencier
e056480ab2 merged branch bschussek/collection-validator (PR #3118)
Commits
-------

e6e3da5 [Validator] Improved test coverage of CollectionValidator and reduced test code duplication
509c7bf [Validator] Moved Optional and Required constraints to dedicated sub namespace.
bf59018 [Validator] Removed @api-tag from Optional and Required constraint, since these two are new.
6641f3e [Validator] Added constraints Optional and Required for the CollectionValidator

Discussion
----------

[Validator] Improve support for optional/required fields in Collection constraint

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: none
Todo: none

![Travis Build Status](https://secure.travis-ci.org/bschussek/symfony.png?branch=collection-validator)

Improves the `Collection` constraint to test on a more granular level if entries of the collection are optional or required. Before this could only be set using the "allowExtraFields" and "allowMissingFields" options, but these are very general and limited.

The former syntax - without Optional or Required - is still supported.

Usage:

    $array = array(
        'name' => 'Bernhard',
        'birthdate' => '1970-01-01',
    );
    $validator->validate($array, null, new Collection(array(
        'name' => new Required(),
        'birthdate' => new Optional(),
    ));

    // you can also pass additional constraints for the fields
    $validator->validate($array, null, new Collection(array(
        'name' => new Required(array(
            new Type('string'),
            new MinLength(3),
        )),
        'birthdate' => new Optional(new Date()),
    ));

---------------------------------------------------------------------------

by canni at 2012-01-15T20:22:17Z

@bschussek I've rewritten a lot of test code for Collection validator in 2.0 branch and also had modified validator itself, as it had a bug #3078, consider waiting with this PR till fabpot will merge 2.0 back into master, as there will be code conflicts :)

---------------------------------------------------------------------------

by Koc at 2012-01-15T23:13:04Z

Does it helps to #2615 ?

---------------------------------------------------------------------------

by fabpot at 2012-01-16T06:44:53Z

@canni: I've just merged 2.0 into master.

---------------------------------------------------------------------------

by bschussek at 2012-01-16T12:05:19Z

@fabpot: Rebased. I also fixed the CS issues mentioned by @stof.
2012-01-16 21:56:42 +01:00
Bernhard Schussek
0c70a410e5 [Form] Made validation of form children configurable. Set the option "cascade_validation" to true if you need it. 2012-01-16 20:49:43 +01:00
Manuel Kiessling
cacc880929 [Bugfix][Locale] Fixed incomplete Locale data loading
Sublocales like de_CH returned only incomplete results for
getDisplayCountries(), getDisplayLanguages() and getDisplayLocales(),
consisting only of results specific for this sublocale, but without the
results of their respective parent locale
2012-01-16 17:25:42 +01:00
Bernhard Schussek
e6e3da5063 [Validator] Improved test coverage of CollectionValidator and reduced test code duplication 2012-01-16 13:03:38 +01:00
Bernhard Schussek
509c7bfb5b [Validator] Moved Optional and Required constraints to dedicated sub namespace. 2012-01-16 11:30:56 +01:00