A field’s settings can be set dynamically when the form is rendered on the page – this is when the field data is “localized” to the page for use by the form.
Dynamic Field Settings
The number field uses the settings num_min, num_max, num_step, which are unique to the number field and can be set dynamically when localized.
Related Filters
add_filter( ‘ninja_forms_localize_field’, ‘my_callback’ );
add_filter( ‘ninja_forms_localize_field_’ . $field_type, ‘my_callback’ );
Example
<?php | |
/** | |
* Change a field's settings when localized to the page. | |
* ninja_forms_localize_field_{$field_type} | |
* | |
* @param array $field [ id, settings => [ type, key, label, etc. ] ] | |
* @return array $field | |
*/ | |
add_filter( 'ninja_forms_localize_field_number', function( $field ){ | |
// Change the min setting of the number field. | |
$field[ 'settings' ][ 'num_min' ] = 12; | |
return $field; | |
}); |
Full Example
<?php | |
/** | |
* Wrap hooks for a specific form. | |
* | |
* @param int $form_id | |
*/ | |
add_action( 'nf_get_form_id', function( $form_id ){ | |
// Check for a specific Form ID. | |
if( 19 !== $form_id ) return; | |
/** | |
* Change a field's settings when localized to the page. | |
* ninja_forms_localize_field_{$field_type} | |
* | |
* @param array $field [ id, settings => [ type, key, label, etc. ] ] | |
* @return array $field | |
*/ | |
add_filter( 'ninja_forms_localize_field_number', function( $field ){ | |
if( 'number_1527000740297' !== $field[ 'settings' ][ 'key' ] ) return $field; | |
// Check for a min value as a querystring. ex site.com/?min=12 | |
if( ! isset( $_GET[ 'min' ] ) ) return $field; | |
// Change the min setting of the number field. | |
$field[ 'settings' ][ 'num_min' ] = intval( $_GET[ 'min' ] ); | |
return $field; | |
}); | |
} ); |