Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FormBuilder - DisplayOnly fields cannot be required #32034

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ext/afform/core/Civi/Api4/Action/Afform/Submit.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,17 @@ private static function getRequiredFieldError(AfformValidateEvent $event, string
if (isset($attributes['defn']['required']) && !$attributes['defn']['required']) {
return NULL;
}
// InputType set to 'DisplayOnly' which skips validation
if (($attributes['defn']['input_type'] ?? NULL) === 'DisplayOnly') {
return NULL;
}
// Load full field definition, because $attributes['defn'] only has the form markup
$fullDefn = FormDataModel::getField($apiEntity, $fieldName, 'create');

// With the full definition loaded, check input_type again
if (($attributes['defn']['input_type'] ?? $fullDefn['input_type']) === 'DisplayOnly') {
return NULL;
}
// we don't need to validate the file fields as it's handled separately
if ($fullDefn['input_type'] === 'File') {
return NULL;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
namespace Civi\ext\afform\mock\tests\phpunit\api\v4\Afform;

use api\v4\Afform\AfformUsageTestCase;
use Civi\Api4\Afform;

/**
* Test case for Afform with autocomplete.
*
* @group headless
*/
class AfformSubmitUsageTest extends AfformUsageTestCase {

public function testSubmitWithDisplayOnlyFields(): void {

$layout = <<<EOHTML
<af-form ctrl="afform">
<af-entity data="{contact_type: 'Individual'}" type="Contact" name="Individual1" label="Individual 1" actions="{create: true, update: true}" url-autofill="1" security="RBAC" />
<fieldset af-fieldset="Individual1" class="af-container" af-title="Individual 1">
<af-field name="first_name" defn="{input_type: 'DisplayOnly', required: true}" />
<af-field name="last_name" />
</fieldset>
<button class="af-button btn btn-primary" crm-icon="fa-check" ng-click="afform.submit()">Submit</button>
</af-form>
EOHTML;

$this->useValues([
'layout' => $layout,
'permission' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
]);

$cid = $this->saveTestRecords('Individual', [
'records' => [
['first_name' => 'One', 'last_name' => 'Person'],
],
])->column('id');

$prefill = Afform::prefill()
->setName($this->formName)
->setFillMode('form')
->setArgs(['Individual1' => $cid])
->execute()
->indexBy('name');
$this->assertCount(1, $prefill['Individual1']['values']);
$this->assertEquals('One', $prefill['Individual1']['values'][0]['fields']['first_name']);
$this->assertEquals('Person', $prefill['Individual1']['values'][0]['fields']['last_name']);

// Submit with empty first_name: should not hit a validation error because DisplayOnly fields cannot be required
$submission = [
['fields' => ['last_name' => 'Person']],
];
$result = Afform::submit()
->setName($this->formName)
->setValues(['Individual1' => $submission])
->setArgs(['Individual1' => $cid])
->execute();
$this->assertSame($cid[0], $result[0]['Individual1'][0]['id']);
}

}