[DomCrawler] added some shortcut methods to the Form classes to make the API more friendly
This commit is contained in:
parent
16f7d3a040
commit
b17400454b
|
@ -41,6 +41,46 @@ class ChoiceFormField extends FormField
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of the field.
|
||||
*
|
||||
* @param string $value The value of the field
|
||||
*
|
||||
* @throws \InvalidArgumentException When value type provided is not correct
|
||||
*/
|
||||
public function select($value)
|
||||
{
|
||||
$this->setValue($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ticks a checkbox.
|
||||
*
|
||||
* @throws \InvalidArgumentException When value type provided is not correct
|
||||
*/
|
||||
public function tick()
|
||||
{
|
||||
if ('checkbox' !== $this->type) {
|
||||
throw new \LogicException(sprintf('You cannot tick "%s" as it is not a checkbox (%s).', $this->name, $this->type));
|
||||
}
|
||||
|
||||
$this->setValue(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ticks a checkbox.
|
||||
*
|
||||
* @throws \InvalidArgumentException When value type provided is not correct
|
||||
*/
|
||||
public function untick()
|
||||
{
|
||||
if ('checkbox' !== $this->type) {
|
||||
throw new \LogicException(sprintf('You cannot tick "%s" as it is not a checkbox (%s).', $this->name, $this->type));
|
||||
}
|
||||
|
||||
$this->setValue(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of the field.
|
||||
*
|
||||
|
|
|
@ -37,6 +37,16 @@ class FileFormField extends FormField
|
|||
$this->value = array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => $error, 'size' => 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of the field.
|
||||
*
|
||||
* @param string $value The value of the field
|
||||
*/
|
||||
public function upload($value)
|
||||
{
|
||||
$this->setValue($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of the field.
|
||||
*
|
||||
|
|
|
@ -18,7 +18,7 @@ namespace Symfony\Components\DomCrawler;
|
|||
* @subpackage Components_DomCrawler
|
||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*/
|
||||
class Form
|
||||
class Form implements \ArrayAccess
|
||||
{
|
||||
protected $document;
|
||||
protected $button;
|
||||
|
@ -327,4 +327,51 @@ class Form
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the named field exists.
|
||||
*
|
||||
* @param string $name The field name
|
||||
*
|
||||
* @param Boolean true if the field exists, false otherwise
|
||||
*/
|
||||
public function offsetExists($name)
|
||||
{
|
||||
return $this->hasValue($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of a field.
|
||||
*
|
||||
* @param string $name The field name
|
||||
*
|
||||
* @throws \InvalidArgumentException if the field does not exist
|
||||
*/
|
||||
public function offsetGet($name)
|
||||
{
|
||||
return $this->getValue($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of a field.
|
||||
*
|
||||
* @param string $name The field name
|
||||
* @param string|array $value The value of the field
|
||||
*
|
||||
* @throws \InvalidArgumentException if the field does not exist
|
||||
*/
|
||||
public function offsetSet($name, $value)
|
||||
{
|
||||
$this->setValue($name, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unimplemented.
|
||||
*
|
||||
* @param string $name The field name
|
||||
*/
|
||||
public function offsetUnset($name)
|
||||
{
|
||||
throw new \LogicException('The Form fields cannot be removed.');
|
||||
}
|
||||
}
|
||||
|
|
Reference in New Issue