问题描述
我有一个表单收集来自几个模型的信息,这些模型在它们的关联中有几层。因此,我必须单独保存,如果有任何失败,报告回视图,以便可以显示错误消息。因为顺序保存,我假设任何错误都不正确显示,我也没有发现 isFieldError()
方法捕获错误的存在。 p>
任何想法如何在视图级别访问此数据以检查错误?我想验证所有3个模型,所以我可以同时显示所有错误,也避免创建手动数据结构和测试。有没有我可以访问的本机Cake功能/数据,所以这不是一个完全自定义的解决方案,我不能在更传统的实例中使用?
#控制器代码段
if($ this-> Proposal-> Requestor-> saveField('phone_number',$ this-> data ['Requestor'] ['phone_number']) &&&&&&&&&&&&&&
exit('saved');
}
else {
$ this-> Session-> setFlash('我们可以不发送您的报价,请更正下面的错误',null,null ,'error');
#在这一点上,我可能有两个或更多的模型有验证错误显示
}
#从加载到视图的元素的代码段
#$ model = Requestor,但条件计算为false
<?php if($ this-> Form-> isFieldError($ model。'.phone_number')):?>
<?php echo $ this-> Form->错误($ model。'.phone_number')?>
<?php endif; ?>
谢谢。
这是开源软件的神奇之处。在源代码中的一点挖掘显示, $ this-> Form-> isFieldError
最终读取一个视图变量名为 $ validationErrors
。当我执行独立的保存时,我只是在我的控制器操作中写入一个相同名称的局部变量,并手动设置它。因此,非常规过程映射到常规结果,并且视图代码不需要识别任何种类的自定义结构。
#从每个单独的
$ 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:执行任何后保存操作
}
else {
#将完整的验证错误列表写入视图
$ this-> set(compact('validationErrors'));
}
如果有更好的方法,有人请让我知道。目前,至少,这似乎是做正确的事情。
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.
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.
# 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' ) );
}
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.
这篇关于从视图访问invalidFields的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!