I am having array of for fields and saving data to the database without issue if I submit without validation.
The trouble I am having is to check for errors existence in foreach iteration before saving any single field data using update_setting( $optionname, $optionvalue );
Current code is saving data for all fields but the error one. So is there any way to first validate all fields and only store to database if there is no single error. Otherwise shoot error message on the page.
$errors = [];
foreach ( $optionnames as $optionname ) {
$optionvalue = get_post_field( $optionname );
// check the field if not set
if ( get_post_field( 'test' ) == '' ) {
$errors['test'] = 'Test field is required';
}
/**
* all loop items only should be add/update if there is not single error
* If any single error occure than it shold not save any single field data
*/
// add/update settings
elseif ( empty( $errors ) ) {
update_setting( $optionname, $optionvalue );
}
}
foreach ($data as $info) { if (validation($info)) { //sql insert } else { echo "error on $info";}foreach ($data as $info) { if (validation($info)) { $validInfo[] = $info; } else { $badInfo[] = $info; foreach ($validInfo as $info) { //sql insert }