Form validation working now.

Be sure to review the changes made to the blog tutorial.
Some specific changes to note we pass model names in inputTag().
was: $html->inputTag('title', 40)
now: $html->inputTag('post/title', 40)

Also in the tagErrorMsg() we pass the model name.
was: $html->tagErrorMsg('body', 'Body is required.')
now: $html->tagErrorMsg('post/body', 'Body is required.') 
https://trac.cakephp.org/wiki/Cake/Tutorials/BlogPosts


git-svn-id: https://svn.cakephp.org/repo/trunk/cake@306 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
phpnut 2005-07-03 23:54:50 +00:00
parent 1178a63db4
commit a558a1e922
3 changed files with 20 additions and 10 deletions

View file

@ -279,7 +279,6 @@ class Controller extends Object
function render($action=null, $layout=null, $file=null)
{
$view =& View::getInstance();
$view->_viewVars =& $this->_viewVars;
$view->action =& $this->action;
@ -295,8 +294,15 @@ class Controller extends Object
$view->parent =& $this->parent;
$view->viewPath =& $this->viewPath;
$view->params =& $this->params;
$view->data =& $this->data;
foreach ($this->models as $key => $value)
{
if(!empty($this->models[$key]->validationErrors))
{
$view->validationErrors[$key] =& $this->models[$key]->validationErrors;
}
}
return $view->render($action, $layout, $file);
}

View file

@ -230,10 +230,12 @@ class HtmlHelper
*/
function inputTag($tag_name, $size=20, $html_options=null)
{
$elements = explode("/", $tag_name);
$html_options['size'] = $size;
$html_options['value'] = isset($html_options['value'])? $html_options['value']: $this->tagValue($tag_name);
$this->tagIsInvalid($tag_name)? $html_options['class'] = 'form_error': null;
return sprintf(TAG_INPUT, $tag_name, $this->parseHtmlOptions($html_options, null, '', ' '));
$html_options['value'] = isset($html_options['value'])? $html_options['value']: $this->tagValue($elements[1]);
$this->tagIsInvalid($elements[0],$elements[1])? $html_options['class'] = 'form_error': null;
return sprintf(TAG_INPUT, $elements[1], $this->parseHtmlOptions($html_options, null, '', ' '));
}
/**
@ -609,9 +611,9 @@ class HtmlHelper
* @param unknown_type $field
* @return unknown
*/
function tagIsInvalid ($field)
function tagIsInvalid ($model, $field)
{
return empty($this->validationErrors[$field])? 0: $this->validationErrors[$field];
return empty($this->validationErrors[$model][$field])? 0: $this->validationErrors[$model][$field];
}
/**
@ -649,15 +651,16 @@ class HtmlHelper
/**
* Returns a formatted error message for given FORM field, NULL if no errors.
*
* @param string $name
* @param string $field
* @param string $text
* @return string If there are errors this method returns an error message, else NULL.
*/
function tagErrorMsg ($field, $text)
{
$error = $this->tagIsInvalid($field);
if (0 == $error)
$elements = explode("/", $field);
$error = 1;
if ($error == $this->tagIsInvalid($elements[0], $elements[1]))
{
return sprintf(SHORT_ERROR_MESSAGE, is_array($text)? (empty($text[$error-1])? 'Error in field': $text[$error-1]): $text);
}

View file

@ -480,6 +480,7 @@ class View extends Object
${$helper}->params = $this->params;
${$helper}->action = $this->action;
${$helper}->data = $this->data;
${$helper}->validationErrors = $this->validationErrors;
}
}
}