Form Validation Examples

example 1
SOURCE

$f=$page->add('Form');

$f->addField('line','email')
    ->validateNotNull()
    ->validateField(
        'filter_var($this->get(), FILTER_VALIDATE_EMAIL)');

$f->addSubmit();

  
Demo
example 2
SOURCE

$f=$page->add('Form');

$f_email=$f->addField('line','email')
    ->validateNotNull()
    ->set('test@example.com');

$f->addSubmit();
if($f->isSubmitted()){
	// manually displaying error message
    if($f->get('email')=='test@example.com'){
        return $f_email->displayFieldError('Choose other email');
    }
}

  
Demo
example 3
SOURCE

$f=$page->add('Form');

$f_email=$f->addField('line','email')
    ->validateNotNull()
    ->set('test@example.com');

// Adding validation hook through closure
$f_email->addHook('validate',function() use ($f_email){
    if($f_email->get()=='test@example.com')
        $f_email->displayFieldError('Choose other email');
});

$f->addSubmit();

  
Demo
example 4
SOURCE

$f=$page->add('Form');

// JavaScript-based validation
$f->addField('line','age')->js(true)
    ->univ()->numericField();

  
Demo