Skip to content

Instantly share code, notes, and snippets.

@mcfdn
Created October 9, 2012 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcfdn/3858391 to your computer and use it in GitHub Desktop.
Save mcfdn/3858391 to your computer and use it in GitHub Desktop.
Validate XML With Zend_Form
<?php
class App_Validate_Xml extends Zend_Validate_Abstract
{
const ERROR_MESSAGE = 'error';
public $xmlError;
protected $_messageVariables = array(
'xmlError' => 'xmlError'
);
protected $_messageTemplates = array(
self::ERROR_MESSAGE => '%xmlError%'
);
public function isValid($value)
{
libxml_use_internal_errors(true);
$dom = new DOMDocument('1.0', 'utf-8');
$dom->loadXML($value);
$errors = libxml_get_errors();
if(count($errors) > 0) {
foreach($errors as $error) {
$this->xmlError = $error->message;
$this->_error(self::ERROR_MESSAGE);
}
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment