Automattic\WooCommerce\Internal\RestApi\Routes\V4\Settings\General
Controller::validate_setting_value
Validate a setting value before updating.
Method of the class: Controller{}
No Hooks.
Returns
bool|WP_Error. True if valid, WP_Error if invalid.
Usage
// private - for code of main (parent) class only $result = $this->validate_setting_value( $setting_id, $value );
- $setting_id(string) (required)
- Setting ID.
- $value(mixed) (required)
- Setting value.
Controller::validate_setting_value() Controller::validate setting value code WC 11.2.2
private function validate_setting_value( $setting_id, $value ) {
// Custom validation rules for specific settings.
switch ( $setting_id ) {
case 'woocommerce_price_num_decimals':
$int = filter_var( $value, FILTER_VALIDATE_INT );
if ( false === $int || $int < 0 || $int > 10 ) {
return new WP_Error(
'rest_invalid_param',
__( 'Number of decimals must be between 0 and 10.', 'woocommerce' ),
array( 'status' => 400 )
);
}
break;
case 'woocommerce_default_country':
if ( ! $this->validate_country_or_state_code( $value ) ) {
return new WP_Error(
'rest_invalid_param',
__( 'Invalid country/state format.', 'woocommerce' ),
array( 'status' => 400 )
);
}
break;
case 'woocommerce_allowed_countries':
$valid_options = array( 'all', 'all_except', 'specific' );
if ( ! in_array( $value, $valid_options, true ) ) {
return new WP_Error(
'rest_invalid_param',
__( 'Invalid selling location option.', 'woocommerce' ),
array( 'status' => 400 )
);
}
break;
case 'woocommerce_ship_to_countries':
$valid_options = array( '', 'all', 'specific', 'disabled' );
if ( ! in_array( $value, $valid_options, true ) ) {
return new WP_Error(
'rest_invalid_param',
__( 'Invalid shipping location option.', 'woocommerce' ),
array( 'status' => 400 )
);
}
break;
case 'woocommerce_specific_allowed_countries':
case 'woocommerce_specific_ship_to_countries':
if ( ! is_array( $value ) ) {
return new WP_Error(
'rest_invalid_param',
__( 'Expected an array of country codes.', 'woocommerce' ),
array( 'status' => 400 )
);
}
foreach ( $value as $code ) {
if ( ! is_string( $code ) || ! $this->validate_country_or_state_code( $code ) ) {
return new WP_Error(
'rest_invalid_param',
__( 'Invalid country code in list.', 'woocommerce' ),
array( 'status' => 400 )
);
}
}
break;
}
return true;
}