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

Afform - Implement client-side validation of required fields #23604

Merged
merged 2 commits into from
Sep 5, 2022
Merged
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
7 changes: 6 additions & 1 deletion ext/afform/core/Civi/Afform/AfformMetadataInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public static function preprocess($e) {
try {
$module = \Civi::service('angular')->getModule(basename($path, '.aff.html'));
$meta = \Civi\Api4\Afform::get(FALSE)->addWhere('name', '=', $module['_afform'])->setSelect(['join_entity', 'entity_type'])->execute()->first();

// Add ngForm directive to afForm controllers
foreach (pq('af-form[ctrl]') as $afForm) {
pq($afForm)->attr('ng-form', $module['_afform']);
}
}
catch (\Exception $e) {
}
Expand Down Expand Up @@ -169,7 +174,7 @@ private static function getField(string $entityName, string $fieldName, string $
$params = [
'action' => $action,
'where' => [['name', 'IN', $namesToMatch]],
'select' => ['name', 'label', 'input_type', 'input_attrs', 'help_pre', 'help_post', 'options', 'fk_entity'],
'select' => ['name', 'label', 'input_type', 'input_attrs', 'help_pre', 'help_post', 'options', 'fk_entity', 'required'],
'loadOptions' => ['id', 'label'],
// If the admin included this field on the form, then it's OK to get metadata about the field regardless of user permissions.
'checkPermissions' => FALSE,
Expand Down
1 change: 1 addition & 0 deletions ext/afform/core/ang/af/afField.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<label class="crm-af-field-label" ng-if=":: $ctrl.defn.label" for="{{:: fieldId }}">
{{:: $ctrl.defn.label }}
<span class="crm-marker" title="{{:: ts('Required') }}" ng-if=":: $ctrl.defn.required">*</span>
</label>
<p class="crm-af-field-help-pre" ng-if=":: $ctrl.defn.help_pre">{{:: $ctrl.defn.help_pre }}</p>
<div class="crm-af-field" ng-include="'~/af/fields/' + $ctrl.defn.input_type + '.html'"></div>
Expand Down
7 changes: 7 additions & 0 deletions ext/afform/core/ang/af/afForm.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
bindings: {
ctrl: '@'
},
require: {
ngForm: 'form'
},
controller: function($scope, $element, $timeout, crmApi4, crmStatus, $window, $location, $parse, FileUploader) {
var schema = {},
data = {},
Expand Down Expand Up @@ -135,6 +138,10 @@
}

this.submit = function() {
if (!ctrl.ngForm.$valid) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@colemanw just wondering if we need to be able to customise the alert here and does this work on front ends? I can't recall if CRM gets loaded correctly on front end

Copy link
Contributor

@kurund kurund Jun 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if this logic will ever be invoked as required validation happens at field level.

CRM.alert(ts('Please fill all required fields.'), ts('Form Error'));
return;
}
status = CRM.status({});
$element.block();

Expand Down
2 changes: 1 addition & 1 deletion ext/afform/core/ang/af/fields/ChainSelect.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<input class="form-control" crm-ui-select="{data: select2Options, multiple: $ctrl.defn.input_attrs.multiple, placeholder: $ctrl.defn.input_attrs.placeholder}" id="{{:: fieldId }}" ng-model="dataProvider.getFieldData()[$ctrl.fieldName]" />
<input class="form-control" ng-required="$ctrl.defn.required" crm-ui-select="{data: select2Options, multiple: $ctrl.defn.input_attrs.multiple, placeholder: $ctrl.defn.input_attrs.placeholder}" id="{{:: fieldId }}" ng-model="dataProvider.getFieldData()[$ctrl.fieldName]" />
2 changes: 1 addition & 1 deletion ext/afform/core/ang/af/fields/CheckBox.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
<label for="{{ fieldId + opt.id }}">{{:: opt.label }}</label>
</li>
</ul>
<input type="checkbox" ng-if="!$ctrl.defn.options" id="{{:: fieldId }}" ng-model="dataProvider.getFieldData()[$ctrl.fieldName]" />
<input type="checkbox" ng-required="$ctrl.defn.required" ng-if="!$ctrl.defn.options" id="{{:: fieldId }}" ng-model="dataProvider.getFieldData()[$ctrl.fieldName]" />
2 changes: 1 addition & 1 deletion ext/afform/core/ang/af/fields/Date.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<input ng-if=":: !$ctrl.defn.search_range" class="form-control" crm-ui-datepicker=":: $ctrl.defn.input_attrs" id="{{:: fieldId }}" ng-model="dataProvider.getFieldData()[$ctrl.fieldName]" />
<div ng-if=":: $ctrl.defn.search_range" class="form-inline">
<input class="form-control" crm-ui-datepicker=":: $ctrl.inputAttrs[1]" id="{{:: fieldId }}1" ng-model="dataProvider.getFieldData()[$ctrl.fieldName]['>=']" />
<input class="form-control" ng-required="$ctrl.defn.required" crm-ui-datepicker=":: $ctrl.inputAttrs[1]" id="{{:: fieldId }}1" ng-model="dataProvider.getFieldData()[$ctrl.fieldName]['>=']" />
<span class="af-field-range-sep">-</span>
<input class="form-control" crm-ui-datepicker=":: $ctrl.inputAttrs[2]" id="{{:: fieldId }}2" ng-model="dataProvider.getFieldData()[$ctrl.fieldName]['<=']" />
</div>
2 changes: 1 addition & 1 deletion ext/afform/core/ang/af/fields/EntityRef.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<input class="form-control" id="{{:: fieldId }}" ng-model="getSetSelect" ng-model-options="{getterSetter: true}" crm-entityref="{entity: $ctrl.defn.fk_entity, select: {multiple: !!$ctrl.defn.input_attrs.multiple, placeholder: $ctrl.defn.input_attrs.placeholder}}" >
<input class="form-control" id="{{:: fieldId }}" ng-required="$ctrl.defn.required" ng-model="getSetSelect" ng-model-options="{getterSetter: true}" crm-entityref="{entity: $ctrl.defn.fk_entity, select: {multiple: !!$ctrl.defn.input_attrs.multiple, placeholder: $ctrl.defn.input_attrs.placeholder}}" >
1 change: 1 addition & 0 deletions ext/afform/core/ang/af/fields/File.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<input type="file" nv-file-select
ng-required="$ctrl.defn.required"
uploader="$ctrl.afFieldset.afFormCtrl.fileUploader"
options="{crmApiParams: $ctrl.getFileUploadParams}">
2 changes: 1 addition & 1 deletion ext/afform/core/ang/af/fields/Number.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<input ng-if=":: !$ctrl.defn.search_range" class="form-control" type="number" id="{{:: fieldId }}" ng-model="dataProvider.getFieldData()[$ctrl.fieldName]" placeholder="{{:: $ctrl.defn.input_attrs.placeholder }}" >
<input ng-if=":: !$ctrl.defn.search_range" class="form-control" ng-required="$ctrl.defn.required" type="number" id="{{:: fieldId }}" ng-model="dataProvider.getFieldData()[$ctrl.fieldName]" placeholder="{{:: $ctrl.defn.input_attrs.placeholder }}" >
<div ng-if=":: $ctrl.defn.search_range" class="form-inline">
<input class="form-control" type="number" id="{{:: fieldId }}" ng-model="dataProvider.getFieldData()[$ctrl.fieldName]['>=']" placeholder="{{:: $ctrl.defn.input_attrs.placeholder }}" >
<span class="af-field-range-sep">-</span>
Expand Down
2 changes: 1 addition & 1 deletion ext/afform/core/ang/af/fields/Text.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<input ng-if=":: !$ctrl.defn.search_range" class="form-control" type="text" id="{{:: fieldId }}" ng-model="dataProvider.getFieldData()[$ctrl.fieldName]" placeholder="{{:: $ctrl.defn.input_attrs.placeholder }}" >
<input ng-if=":: !$ctrl.defn.search_range" class="form-control" type="text" ng-required="$ctrl.defn.required" id="{{:: fieldId }}" ng-model="dataProvider.getFieldData()[$ctrl.fieldName]" placeholder="{{:: $ctrl.defn.input_attrs.placeholder }}" >
<div ng-if=":: $ctrl.defn.search_range" class="form-inline">
<input class="form-control" type="text" id="{{:: fieldId }}" ng-model="dataProvider.getFieldData()[$ctrl.fieldName]['>=']" placeholder="{{:: $ctrl.defn.input_attrs.placeholder }}" >
<span class="af-field-range-sep">-</span>
Expand Down
2 changes: 1 addition & 1 deletion ext/afform/core/ang/af/fields/TextArea.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<textarea class="crm-form-textarea" id="{{:: fieldId }}" ng-model="dataProvider.getFieldData()[$ctrl.fieldName]" ></textarea>
<textarea class="crm-form-textarea" id="{{:: fieldId }}" ng-required="$ctrl.defn.required" ng-model="dataProvider.getFieldData()[$ctrl.fieldName]" ></textarea>