从视图访问 invalidFields
我有一个表单,可以从多个模型收集信息,这些模型在关联中相隔几层。因此,我必须单独保存每个,如果有任何失败,请报告回视图,以便可以显示错误消息。由于顺序保存,我认为任何错误都没有正确显示,我也没有发现 isFieldError()
方法正在捕获错误的存在。
知道如何在视图级别访问此数据以检查错误吗?我想验证所有 3 个模型,这样我就可以同时显示所有错误,并避免创建手动数据结构并进行测试。是否有我可以访问的本机 Cake 功能/数据,因此这不是一个完全自定义的解决方案,我无法在更传统的实例中使用?
# 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; ?>
谢谢。
I have a form that collects information from several models that are several layers apart in their association. For that reason, I have to save each individually and, if any fail, report back to the view so that error messages can be displayed. Because of the sequential saves, I assume, any errors aren't appearing correctly, nor am I finding that the isFieldError()
method is catching the existence of the error.
Any idea how I can access this data at the view level to check for an error? I'd like to validate all 3 models so I can display all errors at the same time and also avoid creating a manual data structure and testing for that. Is there native Cake functionality/data that I can access so this isn't a completely custom solution that I can't use in more traditional instances?
# 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; ?>
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是开源软件的魔力。对源代码进行了一些挖掘,我发现
$this->Form->isFieldError
最终从名为$validationErrors
的视图变量中读取。在进行独立保存时,我只需在控制器操作中写入同名的局部变量并手动设置它。因此,非常规过程被映射到常规结果,并且视图代码不需要识别任何类型的自定义结构。如果有更好的方法来做到这一点,请有人告诉我。至少目前看来,这似乎是正确的做法。
This is the magic of open source software. A little digging around in the source code showed me that
$this->Form->isFieldError
ultimately reads from a view variable named$validationErrors
. When doing my independent saves, I just write to a local variable by the same name in my controller action and set it out manually. Thus the unconventional process is mapped to conventional results and the view code doesn't need to recognize any kind of custom structure.If there's a better way to do this, someone please let me know. For the moment, at least, this seems to be doing the right thing.