Adding and/or injecting error messages into Symfony 1.1 Forms

Symfony 1.1 does a great job handling forms and form validation but say you have a few external forces checking the validity of the user submitted data, such as Paypal. Paypal would love to return it’s own error messages (thank you very much!) and tie it to a particular form field (say, the credit_card field).

Place this method into “symfony_project/lib/form/BaseFormPropel.class.php”:

/**
   * Define error
   *
   * <code>author Dmitry Nesteruk, Andrey Kotlyarov
   *

param string $fieldName *
param string $message
   *

return void */ public function defineError($fieldName, $message) { $checkName = ‘check_define_’.md5($fieldName); $this->getErrorSchema()->getValidator()->addOption($checkName); $this->getErrorSchema()->getValidator()->addMessage($checkName, $message); $this->getErrorSchema()->addError( new sfValidatorError( $this->getErrorSchema()->getValidator(), $checkName, array( ‘value’ => sfContext::getInstance()->getRequest()->getParameter($fieldName), $checkName => $this->getErrorSchema()->getValidator($checkName) ) ) ); }

And use it like so:

<br />
$this-&gt;form = new BillingForm();<br />
$Paypal-&gt;sendPayment();<br />
if(in_array($Paypal-&gt;error_codes,10527)){
	$this-&gt;form-&gt;defineError(&#8216;credit_card&#8217;,&#8220;Paypal doesn&#8217;t like your credit card number. Please try another.&#8221;);<br />
}<br />