Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/Symfony/Component/DomCrawler/Field/ChoiceFormField.php
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}
16 changes: 16 additions & 0 deletions src/Symfony/Component/DomCrawler/Form.php
Expand Up @@ -330,6 +330,22 @@ public function offsetUnset($name)
$this->fields->remove($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.
*
Expand Down
Expand Up @@ -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();
Expand Down
20 changes: 20 additions & 0 deletions src/Symfony/Component/DomCrawler/Tests/FormTest.php
Expand Up @@ -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>');
Expand Down