File: D:/HostingSpaces/yoda-ict/yoda-ict.nl/wwwroot/lib/form/form_live_validation.class.php
<?php
/**
* Created by Komma.pro
* User: mikevandersanden
* Date: 11/1/14
*/
class Form_Live_Validation
{
/*
* Javascript output
*/
private $_js;
/*
* Name of the field
*/
private $_name;
/*
* Type of the field
*/
private $_type;
/*
* Milliseconds to wait for validation
*/
private $_wait = 200;
/*
* Constructor
*/
public function __construct($fieldName, $type)
{
// Set name
$this->_name = $fieldName;
$this->_type = $type;
// Start js output
$this->_js = 'var validate' . ucFirst($this->_name) . ' = new LiveValidation( \'' . $this->_name . '\', { validMessage: \' \', wait: ' . $this->_wait. ' } );';
}
/*
* Initialise: Check what validation to add
*/
public function init($data)
{
foreach($data as $validation)
{
// Call method if exists
if(method_exists($this,'add' . ucfirst($validation)))
{
$this->{'add' . ucfirst($validation)}();
}
}
}
/*
* Get js output
*/
public function get()
{
return $this->_js;
}
/*
* Add validation for required text fields
*/
public function addRequired()
{
switch($this->_type)
{
case 'Field_Type_Select':
$this->_js .= 'validate' . ucFirst($this->_name) . '.add( Validate.Exclusion, { within: [ \'empty\' ], partialMatch: true, failureMessage: \' \' } );';
break;
default:
$this->_js .= 'validate' . ucFirst($this->_name) . '.add( Validate.Presence, { failureMessage: \' \' } );';
}
}
/*
* Add validation for price format
*/
public function addPrice()
{
$this->_js .= 'validate' . ucFirst($this->_name) . '.add( Validate.Format, { pattern: /^\d+([.,]\d{2}|)$/ , failureMessage: \' \' } );';
}
}