0

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 );
    }
}
4
  • foreach ($data as $info) { if (validation($info)) { //sql insert } else { echo "error on $info";} Commented Oct 20, 2015 at 16:34
  • another approach: foreach ($data as $info) { if (validation($info)) { $validInfo[] = $info; } else { $badInfo[] = $info; foreach ($validInfo as $info) { //sql insert } Commented Oct 20, 2015 at 16:35
  • Thanks, let me try second option Commented Oct 20, 2015 at 16:40
  • Sure, let me know if you succeeded. Commented Oct 20, 2015 at 16:42

2 Answers 2

1

trivial but may work :)

$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';
    }

    /**
     * loop all variables
     */
}
if ( count( $errors ) == 0) {
    foreach ( $optionnames as $optionname ) {
        $optionvalue = get_post_field( $optionname );
        update_setting( $optionname, $optionvalue );
    }
} else {
  //routine or whatever you want to fire errors on the page
}
Sign up to request clarification or add additional context in comments.

Comments

1

Rather than hitting the database with an UPDATE query every iteration, why not just run one query? Regardless, you can achieve your goal if you break your code up into two foreach loops, as so:

// first loop validates each option
$errors = [];
foreach($optionNames as $optionName){
     $optionValue = get_post_field($optionName);

     if( strlen(trim($optionValue)) == 0 ){
         $errors[] = sprintf("%s field is required!", $optionName);
         // you could break here if you do not want to accumulate error messages
     }
}

// if any errors were found, halt execution and output error messages
if( count($errors) > 0){
   $errorMessages = implode(",", $errors);
   die("Cannot save due to the following errors: " . $errorMessages);
}

// this will only execute if no errors were found
foreach($optionNames as $optionName){
    $optionValue = get_post_field($optionName);
    update_setting( $optionName, $optionValue );
}

This is not how I would go about doing this, but I chose to answer your question using the code you provided rather than providing something entirely different.

Try to avoid using "else" in situations where returning early (or in my example, halting execution) is an option. It helps clean up your code by providing a clear path to the desired outcome.

1 Comment

Cool.. I am trying this.. thanks.. will get back soon

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.