Cakephp 从视图访问invalidFields

Cakephp 从视图访问invalidFields,cakephp,cakephp-1.3,Cakephp,Cakephp 1.3,我有一个表单,它从多个模型中收集信息,这些模型在它们的关联中有好几层。出于这个原因,我必须单独保存每一个,如果有任何失败,则向视图报告,以便显示错误消息。由于顺序保存,我假设没有正确显示任何错误,也没有发现isFieldError()方法正在捕获错误的存在 知道如何在视图级别访问此数据以检查错误吗?我想验证所有3个模型,以便可以同时显示所有错误,同时避免创建手动数据结构并进行测试。是否有我可以访问的本机蛋糕功能/数据,因此这不是一个我无法在更传统的实例中使用的完全定制的解决方案 # Contro

我有一个表单,它从多个模型中收集信息,这些模型在它们的关联中有好几层。出于这个原因,我必须单独保存每一个,如果有任何失败,则向视图报告,以便显示错误消息。由于顺序保存,我假设没有正确显示任何错误,也没有发现
isFieldError()
方法正在捕获错误的存在

知道如何在视图级别访问此数据以检查错误吗?我想验证所有3个模型,以便可以同时显示所有错误,同时避免创建手动数据结构并进行测试。是否有我可以访问的本机蛋糕功能/数据,因此这不是一个我无法在更传统的实例中使用的完全定制的解决方案

# Controller snippet
if( $this->Proposal->Requestor->saveField( 'phone_number', $this->data['Requestor']['phone_number'] ) && $this->Proposal->Requestor->Building->saveAll( $this->data ) ) {
  # Save off the proposal and message record.
  exit( 'saved' );
}      
else {
  $this->Session->setFlash( 'We can\'t send your quote just yet. Please correct the errors below.', null, null, 'error' );
  # At this point, I may have 2 or more models with validation errors to display
}

# Snippet from an element loaded into the view
# $model = Requestor, but the condition evaluates to false
<?php if( $this->Form->isFieldError( $model . '.phone_number' ) ): ?>
  <?php echo $this->Form->error( $model . '.phone_number' ) ?>
<?php endif; ?>
#控制器代码段
如果($this->Proposal->Requestor->saveField('phone_number',$this->data['Requestor']['phone_number'])&&&$this->Proposal->Requestor->Building->saveAll($this->data)){
#保存提案和消息记录。
退出(“已保存”);
}      
否则{
$this->Session->setFlash('我们还不能发送您的报价。请更正下面的错误',null,null,'错误');
#在这一点上,我可能有2个或更多带有验证错误的模型要显示
}
#加载到视图中的元素的代码段
#$model=请求者,但条件的计算结果为false

谢谢。

这就是开源软件的魅力所在。对源代码进行一点深入研究后,我发现
$this->Form->isFieldError
最终从名为
$validationErrors
的视图变量中读取。在执行独立保存时,我只需在控制器操作中以相同的名称写入局部变量,然后手动设置它。因此,非常规过程映射到常规结果,视图代码不需要识别任何类型的自定义结构

# Compile our validation errors from each separate
$validationErrors = array();
if( !$this->Proposal->Requestor->validates( array( 'fieldList' => array_keys( $this->data['Requestor'] ) ) ) ) {
  $validationErrors['Requestor'] = $this->Proposal->Requestor->validationErrors;
}
if( !$this->Proposal->Requestor->Building->saveAll( $this->data ) ) {
  $validationErrors = Set::merge( $validationErrors, $this->Proposal->Requestor->Building->validationErrors );
}

if( empty( $validationErrors ) ) {
  # TODO: perform any post-save actions
}
else {
  # Write the complete list of validation errors to the view
  $this->set( compact( 'validationErrors' ) );
}
如果有更好的方法,请有人告诉我。至少就目前而言,这似乎是在做正确的事情