-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #242 from umanit/feature_form_options
Add form options feature
- Loading branch information
Showing
5 changed files
with
151 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
Setting-up Form options | ||
======================= | ||
|
||
Should you want to set up your Form's global options, you can specify | ||
them by setting the `options` key to it. | ||
|
||
This is useful for custom validation on the whole form, e.g: say you want to | ||
make sure your end-user only fills-in **one** out of two fields, you can define a | ||
Callback validator like so: | ||
|
||
```yaml | ||
# app/config/extensions/boltforms.bolt.yml | ||
options: | ||
constraints: | ||
- { Callback: { callback: [ 'Bundle\App\Form\Validator\FormValidator', 'validate'] } } | ||
``` | ||
|
||
Then create the validator class. | ||
|
||
```php | ||
<?php | ||
|
||
namespace Bundle\App\Form\Validator; | ||
|
||
use Bolt\Extension\Bolt\BoltForms\Form\Entity\Content; | ||
use Symfony\Component\Validator\Context\ExecutionContextInterface; | ||
|
||
class FormValidator | ||
{ | ||
/** | ||
* Validates that either "adherent_structure" or "other_structure" is filled-in but not both. | ||
* | ||
* @param Content|mixed $object | ||
* @param ExecutionContextInterface $context | ||
*/ | ||
public function validate($object, ExecutionContextInterface $context) | ||
{ | ||
if (true !== (null === $object->get('adherent_structure') xor null === $object->get('other_structure'))) { | ||
$context | ||
->buildViolation('You must fill-in one structure only.') | ||
->addViolation() | ||
; | ||
} | ||
} | ||
|
||
} | ||
``` | ||
|
||
Any option available from `FormType field can be defined. | ||
See [Symfony's doc](https://symfony.com/doc/current/reference/forms/types/form.html) | ||
for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace Bolt\Extension\Bolt\BoltForms\Config\Form; | ||
|
||
use Symfony\Component\HttpFoundation\ParameterBag; | ||
|
||
/** | ||
* Form options configuration for BoltForms | ||
* | ||
* Copyright (c) 2014-2016 Gawain Lynch | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License or GNU Lesser | ||
* General Public License as published by the Free Software Foundation, | ||
* either version 3 of the Licenses, or (at your option) any later version. | ||
* | ||
* This program 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 General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* @author Arthur Guigand <arthur@guigand.com> | ||
* @license http://opensource.org/licenses/GPL-3.0 GNU Public License 3.0 | ||
* @license http://opensource.org/licenses/LGPL-3.0 GNU Lesser General Public License 3.0 | ||
*/ | ||
class FormOptionsBag extends ParameterBag | ||
{ | ||
/** | ||
* Constructor. | ||
* | ||
* @param array $parameters | ||
*/ | ||
public function __construct(array $parameters) | ||
{ | ||
foreach ($parameters as $key => $value) { | ||
if (is_array($value)) { | ||
$value = new ParameterBag($value); | ||
} | ||
$parameters[$key] = $value; | ||
} | ||
|
||
parent::__construct($parameters); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function all() | ||
{ | ||
$parameters = $this->parameters; | ||
foreach ($parameters as $key => $value) { | ||
if ($value instanceof ParameterBag) { | ||
$parameters[$key] = $value->all(); | ||
} else { | ||
$parameters[$key] = $value; | ||
} | ||
} | ||
|
||
return $parameters; | ||
} | ||
|
||
/** | ||
* Get form's constraints. | ||
* | ||
* @return array | ||
*/ | ||
public function getConstraints() | ||
{ | ||
return $this->get('constraints'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters