Pop\Form\Element::validate PHP Method

validate() public method

Validate the form element object.
public validate ( ) : boolean
return boolean
    public function validate()
    {
        $this->errors = array();
        // Check if the element is required.
        if ($this->required == true) {
            if (is_array($this->value)) {
                $curElemValue = $this->marked;
            } else {
                if ($_FILES && isset($_FILES[$this->name]['name'])) {
                    $curElemValue = $_FILES[$this->name]['name'];
                } else {
                    $curElemValue = $this->value;
                }
            }
            if (empty($curElemValue) && $curElemValue != '0') {
                $this->errors[] = \Pop\I18n\I18n::factory()->__('This field is required.');
            }
        }
        // Check the element's validators.
        if (isset($this->validators[0])) {
            foreach ($this->validators as $validator) {
                $curElemSize = null;
                if (is_array($this->value)) {
                    $curElemValue = $this->marked;
                } else {
                    if ($_FILES && isset($_FILES[$this->name]['name'])) {
                        $curElemValue = $_FILES[$this->name]['name'];
                        $curElemSize = $_FILES[$this->name]['size'];
                    } else {
                        $curElemValue = $this->value;
                    }
                }
                // If Pop\Validator\*
                if ($validator instanceof \Pop\Validator\ValidatorInterface) {
                    if ('Pop\\Validator\\NotEmpty' == get_class($validator)) {
                        if (!$validator->evaluate($curElemValue)) {
                            $this->errors[] = $validator->getMessage();
                        }
                    } else {
                        if (null !== $curElemSize && 'Pop\\Validator\\LessThanEqual' == get_class($validator)) {
                            if (!$validator->evaluate($curElemSize)) {
                                $this->errors[] = $validator->getMessage();
                            }
                        } else {
                            if (!empty($curElemValue) && !$validator->evaluate($curElemValue)) {
                                $this->errors[] = $validator->getMessage();
                            }
                        }
                    }
                    // Else, if callable
                } else {
                    $result = call_user_func_array($validator, array($curElemValue));
                    if (null !== $result) {
                        $this->errors[] = $result;
                    }
                }
            }
        }
        // If errors are found on any of the form elements, return false.
        return count($this->errors) > 0 ? false : true;
    }

Usage Example

Exemplo n.º 1
0
 public function testValidate()
 {
     $e = new Element('text', 'email');
     $e->addValidator(new Email());
     $e->setValue('*****@*****.**');
     $this->assertTrue($e->validate());
     $e = new Element('text', 'email');
     $e->addValidator(new Email());
     $e->setValue('testtest.com');
     $this->assertFalse($e->validate());
     $this->assertContains('class="error"', $e->render(true));
     $this->assertGreaterThan(0, count($e->getErrors()));
     $this->assertTrue($e->hasErrors());
 }