Skip to content

Commit

Permalink
fix: フォーム設定で添付ファイルを必須としていてもファイル無しでフォーム送信できてしまった不具合を修正。
Browse files Browse the repository at this point in the history
あわせて、無効なファイルタイプをアップロードしたときに内部エラーになってしまっていたのも修正。
  • Loading branch information
RyujiAMANO committed Oct 2, 2020
1 parent 81b7aff commit 62dafc2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
13 changes: 13 additions & 0 deletions Model/RegistrationAnswer.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ public function beforeValidate($options = array()) {
$question = $options['question'];
$allAnswers = $options['allAnswers'];

// 添付ファイル用のバリデーションを一時退避(他のvalidateルールを初期化しておきたいので)
$fileValidate = $this->validate['answer_value_file'];

// Answerモデルは繰り返し判定が行われる可能性高いのでvalidateルールは最初に初期化
// mergeはしません
$this->validate = array(
Expand Down Expand Up @@ -168,6 +171,16 @@ public function beforeValidate($options = array()) {

),
);

if ($question['question_type'] === RegistrationsComponent::TYPE_FILE) {
if ($question['is_require']) {
$this->validate['answer_value_file'] = $fileValidate;
$this->validate['answer_value_file']['notBlank'] = [
'rule' => array('isFileUpload'),
'message' => array(__d('files', 'Please specify the file'))
];
}
}
parent::beforeValidate($options);

return true;
Expand Down
4 changes: 4 additions & 0 deletions View/Helper/RegistrationAnswerHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public function answer($question, $readonly = false) {
array($index, $fieldName, $question, $readonly));

if (! RegistrationsComponent::isMatrixInputType($question['question_type'])) {
if ($question['question_type'] === RegistrationsComponent::TYPE_FILE) {
// 添付ファイルは answer_value_fileフィールドで処理しているのでエラー確認もanswer_value_fileに対しておこなう
$fieldName .= '_file';
}
$ret .= $this->_error($fieldName);
$ret .= $this->NetCommonsForm->hidden($baseFieldName . 'registration_answer_summary_id');
$ret .= $this->NetCommonsForm->hidden($baseFieldName . 'registration_question_key',
Expand Down
10 changes: 9 additions & 1 deletion View/RegistrationAnswers/view.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,16 @@ $jsAnswers = NetCommonsAppController::camelizeKeyRecursive($answers);
?>

<?php foreach($questionPage['RegistrationQuestion'] as $index => $question): ?>
<?php
$fieldName = 'answer_value';
if ($question['question_type'] === RegistrationsComponent::TYPE_FILE) {
// 添付ファイルは answer_value_fileフィールドで処理しているのでエラー確認もanswer_value_fileに対しておこなう
$fieldName = 'answer_value_file';
}
$hasError = $this->Form->isFieldError('RegistrationAnswer.' . $question['key'] . '.0.' . $fieldName);
?>
<div class="form-group
<?php if ($this->Form->isFieldError('RegistrationAnswer.' . $question['key'] . '.0.answer_value')): ?>
<?php if ($hasError): ?>
has-error
<?php endif ?>">

Expand Down

0 comments on commit 62dafc2

Please sign in to comment.