Problem
I'm using #element_validate
to validate an input on a custom form which creates a node. I'm using element_validate_integer_positive
as the function to validate. It works perfectly.
It partly works when I try to hook the same code into the existing node edit form. The form still breaks and displays the correct message but it also throws two errors due to missing values. How can I remove these errors?
So far
Here's the form code from my custom module. This works as expected.
$form['field_total_vacation'] = array(
'#type' => 'textfield',
'#title' => t('Total Vacation'),
'#size' => 5,
'#element_validate' => array('element_validate_integer_positive'),
);
Here's my hook into the node edit form:
function viewspagehack_form_alter(&$form, $form_state, $form_id) {
if ($form_id === 'job_advert_node_form') {
$form['field_total_vacation']['#weight'] = 19;
$form['field_total_vacation']['#element_validate'] = array('element_validate_integer_positive');
}
}
If I enter a non-positive integer into field_total_vacation
on the node edit form, it still breaks and throws the correct message: "Total Vacation must be a positive integer" but it also throws:
Notice: Undefined index: #value in element_validate_integer_positive() (line 4190 of C:\XAMPP\htdocs\drupal\includes\form.inc).
Notice: Undefined index: #title in element_validate_integer_positive() (line 4192 of C:\XAMPP\htdocs\drupal\includes\form.inc).
This is because element_validate_integer_positive()
takes $element
as it's first input and the function then calls:
$element['#value']
$element['#title']
But this error doesn't throw on my custom form which also doesn't take an input. What's going on?
EDIT
I moved #element_validate to here, this means it now has access to #title
:
$form['field_total_vacation']['und']['0']['#element_validate'] = array('element_validate_integer_positive');
This removes the title error but leaves the value error. I can't understand why this function takes #value
from $element
.
function element_validate_integer_positive($element, &$form_state) {
$value = $element['#value'];
None of the form elements have #value
, #value
only seems to appear in $form_state
. But yet it works in my custom form and not on the node edit.
#value
error still exists and I can't understand where my custom form is even getting#value
from. Added an edit to my main question.