Friday, 15 July 2011

How to get all form error in symfony?

Hi Guys,

1. First Method:

Add following line in the template:

foreach($form->getWidgetSchema()->getPositions() as $widgetName)
{
  echo $widgetName." == ".$form[$widgetName]->renderError();
  echo "<br />";
}

Using above script, you will find something like this:

first_name == Please enter First Name.
last_name == Please enter Last Name.
address1 == Please enter Address 1.

2. Second Method:

 public function getAllErrors()
  {
       $err = array();
       foreach ($this as $form_field){
         if ($form_field->hasError()){
           $err_obj = $form_field->getError();
           if ($$err_obj instanceof sfValidatorErrorSchema){
             foreach ($err_obj->getErrors() as $err){
               $err[$form_field->getName()] = $err->getMessage();
             }
           }else{
             $err[$form_field->getName()] = $err_obj->getMessage();
           }
         }
       }
       // global err
       foreach ($this->getGlobalErrors() as $validator_err){
         $err[] = $validator_err->getMessage();
       }
       return $err;
  }

put above function in your form class. and call this function in your action.class.php as given below:

$this->form->getAllErrors();

Using above method, you will get something like this:

Array
(
    [first_name] => Please enter First Name.
    [last_name] => Please enter Last Name.
    [address1] => Please enter Address 1.
    [town] => Please enter Town.
    [country] => Please select Country
    [email] => Please enter E-Mail address
    [phone] => Please enter Phone Number
    [address_proof] => Please upload credit card statement.
    [user_id_proof] => Please upload identity proof.
    [card_Num] => Please enter Card Number
    [card_holder] => Please enter Card Holder name.
    [transaction_type] => Please select transaction type.
)


Cheers!

3 comments:

  1. not include the value in embedded form

    ReplyDelete
  2. Yes, I need error from embedded forms also.

    ReplyDelete