This is special post for symfony developers, who struggling for get all error message from symfony-1.4 form object.
It's very easy for normal form object, see below code block :
1: <?php
2: $errors = array();
3: foreach ($Form->getErrorSchema() as $key => $err) {
4: if ($key) {
5: $errors[$key] = $err->getMessage();
6: }
7: }
8: ?>
See errors array :
1: <?php
2: print_r($errors);
3: ?>
But what for embedded forms, continue reading for solution.
Create formUtility class in project lib directory :
1: <?php
2: class formUtility {
3: /**
4: * Process Form object and return array of errors
5: *
6: * @param object $form Form object
7: * @param array $embedded_forms String array of embedded form names
8: * @return array Array of error messages and total errors count.
9: * @author Rajesh Meniya<rjmeniya@gmail.com>
10: */
11: public static function getErrors($form = false, $embedded_forms = array()) {
12: if (!$form){
13: return false;
14: }
15: $errors = array(); $total_error++;
16: // individual widget errors
17: foreach ($form as $form_field) {
18: if ($form_field->hasError()) {
19: $error_obj = $form_field->getError();
20: if ($error_obj instanceof sfValidatorErrorSchema) {
21: foreach ($error_obj->getErrors() as $error) {
22: // add namespace for embedded form erros
23: if ($form->getName() != $form->getName()) {
24: $errors[$form->getName()][$form_field->getName()][] = $error->getMessage(); $total_error++;
25: } else {
26: $errors[$form_field->getName()][] = $error->getMessage();
27: }
28: }
29: } else {
30: if ($form->getName() != $form->getName()) {
31: $errors[$form->getName()][$form_field->getName()][] = $error_obj->getMessage(); $total_error++;
32: } else {
33: $errors[$form_field->getName()] = $error_obj->getMessage(); $total_error++;
34: }
35: }
36: }
37: }
38: // for global errors
39: foreach ($form->getGlobalErrors() as $validator_error) {
40: $errors[] = $validator_error->getMessage();
41: }
42: // for embedded form error processing
43: $count_embedded_error = 0;
44: if (count($embedded_forms) && is_array($embedded_forms) ) {
45: foreach($embedded_forms as $key => $embedded_form_name){
46: if (isset($errors[$embedded_form_name])) {
47: if(is_array($errors[$embedded_form_name])){
48: foreach($errors[$embedded_form_name] as $key1=>$errors_embedded){
49: $error_embedded_form = array();
50: $asEFRawErrors = explode("]", $errors_embedded);
51: foreach($asEFRawErrors as $ssRawError){
52: if ($ssRawError!=null) {
53: $raw_error = explode("[",$ssRawError);
54: $error_embedded_form[trim($raw_error[0])] = trim($raw_error[1]); $total_error++;
55: //$error_embedded_form[] = $ssRawError;
56: }
57: }
58: $errors[$embedded_form_name][$key1] = $error_embedded_form;
59: $count_embedded_error += count($error_embedded_form);
60: }
61: }
62: }
63: }
64: }
65: $errors_final['error_message'] = $errors;
66: // count errors
67: $errors_final['error_count'] = $total_error;
68: return $errors_final;
69: }
70: }
71: ?>
Let's use our formUtility class in action for get array of all errors.
1: <?php
2: /**
3: * Action class
4: */
5: class companyActions extends sfActions {
6: /**
7: * Action for handle posted form
8: */
9: public function executeCreate(sfWebRequest $request) {
10: $form = new BlogForm();
11: if ($request->isMethod("post")) {
12: $form->bind($request->getParameter($form->getName()));
13: // user of our formUtility class
14: $errors = formUtility::getErrors($form, array('comments'));
15: print_r($errors);
16: }
17: }
18: }
19: ?>
Please, give your valuable comments and +1
Thanks