[DomCrawler] Allowed internal validation of ChoiceFormField to be disabled
This commit is contained in:
parent
2c7c4a5ea0
commit
739bf715c7
@ -34,6 +34,10 @@ class ChoiceFormField extends FormField
|
|||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
private $options;
|
private $options;
|
||||||
|
/**
|
||||||
|
* @var boolean
|
||||||
|
*/
|
||||||
|
private $validationDisabled = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the field should be included in the submitted values.
|
* Returns true if the field should be included in the submitted values.
|
||||||
@ -280,6 +284,10 @@ class ChoiceFormField extends FormField
|
|||||||
*/
|
*/
|
||||||
public function containsOption($optionValue, $options)
|
public function containsOption($optionValue, $options)
|
||||||
{
|
{
|
||||||
|
if ($this->validationDisabled) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
foreach ($options as $option) {
|
foreach ($options as $option) {
|
||||||
if ($option['value'] == $optionValue) {
|
if ($option['value'] == $optionValue) {
|
||||||
return true;
|
return true;
|
||||||
@ -304,4 +312,16 @@ class ChoiceFormField extends FormField
|
|||||||
|
|
||||||
return $values;
|
return $values;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disables the internal validation of the field.
|
||||||
|
*
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function disableValidation()
|
||||||
|
{
|
||||||
|
$this->validationDisabled = true;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -330,6 +330,22 @@ class Form extends Link implements \ArrayAccess
|
|||||||
$this->fields->remove($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.
|
* Sets the node for the form.
|
||||||
*
|
*
|
||||||
|
@ -284,6 +284,21 @@ class ChoiceFormFieldTest extends FormFieldTestCase
|
|||||||
$this->assertEquals('foo', $field->getValue(), '->select() changes the selected option');
|
$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())
|
protected function createSelectNode($options, $attributes = array())
|
||||||
{
|
{
|
||||||
$document = new \DOMDocument();
|
$document = new \DOMDocument();
|
||||||
|
@ -304,6 +304,26 @@ class FormTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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()
|
public function testOffsetUnset()
|
||||||
{
|
{
|
||||||
$form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');
|
$form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');
|
||||||
|
Reference in New Issue
Block a user