Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

[DomCrawler] Allowed internal validation of ChoiceFormField to be disabled #8637

Closed
wants to merge 2 commits into from

2 participants

@pylebecq
Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #7672
License MIT
Doc PR Not yet

Hi,

Here is a quite basic attempt to be able to disable the internal validation of the ChoiceFormField. It's pretty basic.
Feel free to tell me what you think guys. Maybe I should check the validationDisabled property at the beginning of the containsOption() method ?
I'll make the documentation PR as soon as the implementation will be validated.

Regards.

@fabpot
Owner

What about setting the validation flag on the form itself instead? That way, it would apply on all form fields.

@pylebecq

I don't know what is the best.

Choice Pros Cons
Field Can be controlled on a per field basis Tedious to disable validation on a whole form
Form Can disable validation of all fields with 1 method call Cannot disable validation for only one field
Both Can disable validation of all fields with 1 method call or on a per field basis None

For the last option, my proposed implementation is to keep validation flag on the fields and having a method on the Form class to toggle the flag on all the fields.

@pylebecq

I made an attempt to implement the "Both" solution of my previous post.

@fabpot What do you think ? Does it fits what you had in mind ? If so, do you think we should introduce an Interface for form fields having validation capabilities, and for the Form class itself ? I did not because only the ChoiceFormField has such capabilities yet.

@fabpot fabpot closed this pull request from a commit
@fabpot fabpot feature#8637 [DomCrawler] Allowed internal validation of ChoiceFormFi…
…eld to be disabled (pylebecq)

This PR was squashed before being merged into the master branch (closes #8637).

Discussion
----------

[DomCrawler] Allowed internal validation of ChoiceFormField to be disabled

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #7672
| License       | MIT
| Doc PR        | Not yet

Hi,

Here is a quite basic attempt to be able to disable the internal validation of the ChoiceFormField. It's pretty basic.
Feel free to tell me what you think guys. Maybe I should check the `validationDisabled` property at the beginning of the `containsOption()` method ?
I'll make the documentation PR as soon as the implementation will be validated.

Regards.

Commits
-------

739bf71 [DomCrawler] Allowed internal validation of ChoiceFormField to be disabled
112fa5e
@fabpot fabpot closed this in 112fa5e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 1, 2013
  1. @pylebecq
Commits on Aug 2, 2013
  1. @pylebecq
This page is out of date. Refresh to see the latest.
View
20 src/Symfony/Component/DomCrawler/Field/ChoiceFormField.php
@@ -34,6 +34,10 @@ class ChoiceFormField extends FormField
* @var array
*/
private $options;
+ /**
+ * @var boolean
+ */
+ private $validationDisabled = false;
/**
* Returns true if the field should be included in the submitted values.
@@ -280,6 +284,10 @@ private function buildOptionValue($node)
*/
public function containsOption($optionValue, $options)
{
+ if ($this->validationDisabled) {
+ return true;
+ }
+
foreach ($options as $option) {
if ($option['value'] == $optionValue) {
return true;
@@ -304,4 +312,16 @@ public function availableOptionValues()
return $values;
}
+
+ /**
+ * Disables the internal validation of the field.
+ *
+ * @return self
+ */
+ public function disableValidation()
+ {
+ $this->validationDisabled = true;
+
+ return $this;
+ }
}
View
16 src/Symfony/Component/DomCrawler/Form.php
@@ -331,6 +331,22 @@ public function offsetUnset($name)
}
/**
+ * Disables validation
+ *
+ * @return self
+ */
+ public function disableValidation()
+ {
+ foreach ($this->fields->all() as $field) {
+ if ($field instanceof Field\ChoiceFormField) {
+ $field->disableValidation();
+ }
+ }
+
+ return $this;
+ }
+
+ /**
* Sets the node for the form.
*
* Expects a 'submit' button \DOMNode and finds the corresponding form element.
View
15 src/Symfony/Component/DomCrawler/Tests/Field/ChoiceFormFieldTest.php
@@ -284,6 +284,21 @@ public function testOptionWithNoValue()
$this->assertEquals('foo', $field->getValue(), '->select() changes the selected option');
}
+ public function testDisableValidation()
+ {
+ $node = $this->createSelectNode(array('foo' => false, 'bar' => false));
+ $field = new ChoiceFormField($node);
+ $field->disableValidation();
+ $field->setValue('foobar');
+ $this->assertEquals('foobar', $field->getValue(), '->disableValidation() allows to set a value which is not in the selected options.');
+
+ $node = $this->createSelectNode(array('foo' => false, 'bar' => false), array('multiple' => 'multiple'));
+ $field = new ChoiceFormField($node);
+ $field->disableValidation();
+ $field->setValue(array('foobar'));
+ $this->assertEquals(array('foobar'), $field->getValue(), '->disableValidation() allows to set a value which is not in the selected options.');
+ }
+
protected function createSelectNode($options, $attributes = array())
{
$document = new \DOMDocument();
View
20 src/Symfony/Component/DomCrawler/Tests/FormTest.php
@@ -304,6 +304,26 @@ public function testSetValueOnMultiValuedFieldsWithMalformedName()
}
}
+ public function testDisableValidation()
+ {
+ $form = $this->createForm('<form>
+ <select name="foo[bar]">
+ <option value="bar">bar</option>
+ </select>
+ <select name="foo[baz]">
+ <option value="foo">foo</option>
+ </select>
+ <input type="submit" />
+ </form>');
+
+ $form->disableValidation();
+
+ $form['foo[bar]']->select('foo');
+ $form['foo[baz]']->select('bar');
+ $this->assertEquals('foo', $form['foo[bar]']->getValue(), '->disableValidation() disables validation of all ChoiceFormField.');
+ $this->assertEquals('bar', $form['foo[baz]']->getValue(), '->disableValidation() disables validation of all ChoiceFormField.');
+ }
+
public function testOffsetUnset()
{
$form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');
Something went wrong with that request. Please try again.