| 75 | | * Set the format of message strings. |
| 76 | | * |
| 77 | | * @chainable |
| 78 | | * @param string new message format |
| 79 | | * @return object |
| 80 | | */ |
| 81 | | public function message_format($str) |
| 82 | | { |
| 83 | | if (strpos($str, '{message}') === FALSE) |
| 84 | | throw new Kohana_Exception('validation.error_format'); |
| 85 | | |
| 86 | | // Set the new message format |
| 87 | | $this->message_format = $str; |
| 88 | | |
| 89 | | return $this; |
| 90 | | } |
| 91 | | |
| 92 | | /** |
| 93 | | * Sets or returns the message for an input. |
| 94 | | * |
| 95 | | * @chainable |
| 96 | | * @param string input key |
| 97 | | * @param string message to set |
| 98 | | * @return string|object |
| 99 | | */ |
| 100 | | public function message($input = NULL, $message = NULL) |
| 101 | | { |
| 102 | | if ($message === NULL) |
| 103 | | { |
| 104 | | if ($input === NULL) |
| 105 | | { |
| 106 | | $messages = array(); |
| 107 | | foreach (array_keys($this->messages) as $input) |
| 108 | | { |
| 109 | | $messages[] = $this->message($input); |
| 110 | | } |
| 111 | | |
| 112 | | return implode("\n", $messages); |
| 113 | | } |
| 114 | | |
| 115 | | // Return nothing if no message exists |
| 116 | | if (empty($this->messages[$input])) |
| 117 | | return ''; |
| 118 | | |
| 119 | | // Return the HTML message string |
| 120 | | return str_replace('{message}', $this->messages[$input], $this->message_format); |
| 121 | | } |
| 122 | | else |
| 123 | | { |
| 124 | | $this->messages[$input] = $message; |
| 125 | | } |
| 126 | | |
| 127 | | return $this; |
| 128 | | } |
| 129 | | |
| 130 | | /** |
| | 428 | * Sets or returns the message for an input. |
| | 429 | * |
| | 430 | * @chainable |
| | 431 | * @param string input key |
| | 432 | * @param string message to set |
| | 433 | * @return string|object |
| | 434 | */ |
| | 435 | public function message($input = NULL, $message = NULL) |
| | 436 | { |
| | 437 | if ($message === NULL) |
| | 438 | { |
| | 439 | if ($input === NULL) |
| | 440 | { |
| | 441 | $messages = array(); |
| | 442 | foreach (array_keys($this->messages) as $input) |
| | 443 | { |
| | 444 | $messages[] = $this->message($input); |
| | 445 | } |
| | 446 | |
| | 447 | return implode("\n", $messages); |
| | 448 | } |
| | 449 | |
| | 450 | // Return nothing if no message exists |
| | 451 | if (empty($this->messages[$input])) |
| | 452 | return ''; |
| | 453 | |
| | 454 | // Return the HTML message string |
| | 455 | return str_replace('{message}', $this->messages[$input], $this->message_format); |
| | 456 | } |
| | 457 | else |
| | 458 | { |
| | 459 | $this->messages[$input] = $message; |
| | 460 | } |
| | 461 | |
| | 462 | return $this; |
| | 463 | } |
| | 464 | |
| | 465 | /** |
| 486 | | * @return array |
| 487 | | */ |
| 488 | | public function errors() |
| 489 | | { |
| 490 | | return $this->errors; |
| 491 | | } |
| 492 | | |
| 493 | | /** |
| 494 | | * Provides a generic interface to load the errors. Each field can have a |
| 495 | | * specific message, such as username.required, and a default message, such |
| 496 | | * as username.default. |
| 497 | | * |
| 498 | | * @param string language file to load errors from |
| 499 | | * @return void |
| 500 | | */ |
| 501 | | public function load_errors($file = 'forms') |
| 502 | | { |
| 503 | | foreach ($this->errors as $input => $error) |
| 504 | | { |
| 505 | | // Key for this input error |
| 506 | | $key = "$file.$input.$error"; |
| 507 | | |
| 508 | | if (($str = Kohana::lang($key)) === $key) |
| 509 | | { |
| 510 | | // Get the default error message |
| 511 | | $str = Kohana::lang("$file.$input.default"); |
| 512 | | } |
| 513 | | |
| 514 | | // Add the message |
| 515 | | $this->message($input, $str); |
| | 468 | * @param boolean load errors from the a lang file |
| | 469 | * @return array |
| | 470 | */ |
| | 471 | public function errors($file = NULL) |
| | 472 | { |
| | 473 | if ($file === NULL) |
| | 474 | { |
| | 475 | return $this->errors; |
| | 476 | } |
| | 477 | else |
| | 478 | { |
| | 479 | $errors = array(); |
| | 480 | foreach ($this->errors as $input => $error) |
| | 481 | { |
| | 482 | // Key for this input error |
| | 483 | $key = "$file.$input.$error"; |
| | 484 | |
| | 485 | if (($str = Kohana::lang($key)) === $key) |
| | 486 | { |
| | 487 | // Get the default error message |
| | 488 | $errors[$input] = Kohana::lang("$file.$input.default"); |
| | 489 | } |
| | 490 | } |
| | 491 | |
| | 492 | return $errors; |