Accessing invalidFields from the view

▼魔方 西西 提交于 2019-12-25 02:18:54

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/7854369/accessing-invalidfields-from-the-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!