Skip to content

Commit

Permalink
Afform - Implement client-side validation of required fields
Browse files Browse the repository at this point in the history
  • Loading branch information
colemanw committed May 27, 2022
1 parent 12f6fca commit a2916df
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 8 deletions.
5 changes: 5 additions & 0 deletions ext/afform/core/Civi/Afform/AfformMetadataInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,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
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
9 changes: 8 additions & 1 deletion 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, FileUploader) {
var schema = {},
data = {},
Expand Down Expand Up @@ -83,6 +86,10 @@
}

this.submit = function() {
if (!ctrl.ngForm.$valid) {
CRM.alert(ts('Please fill all required fields.'), ts('Form Error'));
return;
}
status = CRM.status({});
$element.block();

Expand All @@ -92,7 +99,7 @@
values: data}
).then(function(response) {
if (ctrl.fileUploader.getNotUploadedItems().length) {
_.each(ctrl.fileUploader.getNotUploadedItems(), function(file) {
_.each(ctrl.fileUploader.getNotUploadedItegitk&ms(), function(file) {
file.formData.push({
params: JSON.stringify(_.extend({
token: response[0].token,
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>

0 comments on commit a2916df

Please sign in to comment.