mirror of
https://github.com/farmOS/farmOS.git
synced 2024-02-23 11:37:38 +01:00
Add custom validation for quantity inventory values.
This commit is contained in:
parent
e9caab4e4a
commit
7cb61108b9
|
@ -38,6 +38,11 @@ function farm_inventory_inline_entity_form_entity_form_alter(array &$entity_form
|
|||
return;
|
||||
}
|
||||
|
||||
// Specify special validation for the inventory values.
|
||||
// Validation is needed because we cannot solely rely on FAPI #states,
|
||||
// partially because it is hard to target the entity browser form widget.
|
||||
$entity_form['#element_validate'][] = 'farm_inventory_quantity_entity_inline_form_validate';
|
||||
|
||||
// Set the inventory_adjustment default value to N/A unless already provided.
|
||||
if (empty($entity_form['inventory_adjustment']['widget']['#default_value'])) {
|
||||
$entity_form['inventory_adjustment']['widget']['#default_value'] = '_none';
|
||||
|
@ -55,3 +60,35 @@ function farm_inventory_inline_entity_form_entity_form_alter(array &$entity_form
|
|||
$inventory_adjustment_selector => ['value' => '_none'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom validation callback for the quantity inline form.
|
||||
*
|
||||
* @param array $form
|
||||
* The entity form array.
|
||||
* @param \Drupal\Core\Form\FormStateInterface $form_state
|
||||
* The entity form state.
|
||||
*/
|
||||
function farm_inventory_quantity_entity_inline_form_validate(array &$form, FormStateInterface $form_state) {
|
||||
|
||||
// Get the inline entity form values out of the entire entity form state.
|
||||
$quantity_form_values = $form_state->getValue($form['#parents']);
|
||||
|
||||
// If a quantity was provided, validate correct inventory values are provided.
|
||||
if (!empty($quantity_form_values)) {
|
||||
$adjustment = $quantity_form_values['inventory_adjustment'];
|
||||
$asset = $quantity_form_values['inventory_asset']['target_id'];
|
||||
|
||||
// Set error if an adjustment is provided without an asset.
|
||||
if (!empty($adjustment) && empty($asset)) {
|
||||
// Error is set on the inventory_adjustment field because form errors
|
||||
// are not highlighted when set on the entity browser widget.
|
||||
$form_state->setError($form['inventory_adjustment']['widget'], 'Inventory asset is required if an inventory adjustment is selected.');
|
||||
}
|
||||
|
||||
// Set error if an asset is provided without an adjustment.
|
||||
if (empty($adjustment) && !empty($asset)) {
|
||||
$form_state->setError($form['inventory_adjustment']['widget'], 'Inventory adjustment is required if an inventory asset is selected.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue