Пример #1
0
 public function testAddingInputWithNameDoesNotInjectNameInInput()
 {
     $filter = new InputFilter();
     $foo = new Input('foo');
     $filter->add($foo, 'bar');
     $test = $filter->get('bar');
     $this->assertSame($foo, $test);
     $this->assertEquals('foo', $foo->getName());
 }
Пример #2
0
 /**
  * Pre-process form: set id if needed and  and set
  */
 private function preProcessForm()
 {
     $xpath = new DOMXPath($this->document);
     $elements = $xpath->query('//input | //textarea | //div[@data-input-name]');
     /** @var DOMElement $element */
     foreach ($elements as $element) {
         // Set some basic vars
         $name = $element->getAttribute('name');
         if (!$name) {
             $name = $element->getAttribute('data-input-name');
         }
         if (!$name) {
             // At least a name is needed to submit a value.
             // Silently continue, might be a submit button.
             continue;
         }
         // Create an id if needed, this speeds up finding the element again
         $id = $element->getAttribute('id');
         if (!$id) {
             $id = md5(spl_object_hash($element));
             $element->setAttribute('id', $id);
         }
         $this->nameIdXref[$name] = $id;
         // Detect element type
         $type = $element->getAttribute('type');
         if ($element->tagName == 'textarea') {
             $type = 'textarea';
         }
         // Add validation
         if (isset($this->formElements[$type])) {
             $validator = new $this->formElements[$type]();
         } else {
             // Create a default validator
             $validator = new $this->formElements['text']();
         }
         if ($this->inputFilter->has($name)) {
             $input = $this->inputFilter->get($name);
         } else {
             // No input found for element, create a new one
             $input = new Input($name);
             // Enforce properties so the NotEmpty validator is automatically added,
             // we'll take care of this later.
             $input->setRequired(false);
             $input->setAllowEmpty(true);
             $this->inputFilter->add($input);
         }
         // Process element and attach filters and validators
         $validator($element, $input);
     }
 }
Пример #3
0
 /**
  * @group 5270
  * @requires extension intl
  */
 public function testIsValidWhenValuesSetOnFilters()
 {
     $filter = new InputFilter();
     $foo = new Input();
     $foo->getFilterChain()->attachByName('stringtrim')->attachByName('alpha');
     $foo->getValidatorChain()->attach(new Validator\StringLength(15, 18));
     $filter->add($foo, 'foo');
     //test valid with setData
     $filter->setData(array('foo' => 'invalid'));
     $this->assertFalse($filter->isValid());
     //test invalid with setData
     $filter->setData(array('foo' => 'thisisavalidstring'));
     $this->assertTrue($filter->isValid());
     //test invalid when setting data on actual filter
     $filter->get('foo')->setValue('invalid');
     $this->assertFalse($filter->get('foo')->isValid(), 'Filtered value is valid, should be invalid');
     $this->assertFalse($filter->isValid(), 'Input filter did not return value from filter');
     //test valid when setting data on actual filter
     $filter->get('foo')->setValue('thisisavalidstring');
     $this->assertTrue($filter->get('foo')->isValid(), 'Filtered value is not valid');
     $this->assertTrue($filter->isValid(), 'Input filter did return value from filter');
 }