Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6531999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:56:47+00:00 2026-05-25T09:56:47+00:00

Attached code taken from cakephp bakery, where someone uploaded a sample about custom validation

  • 0

Attached code taken from cakephp bakery, where someone uploaded a sample about custom validation rules.

class Contact extends AppModel
{
    var $name = 'Contact';
    var $validate = array(
        'email' => array(
            'identicalFieldValues' => array(
                 'rule'    => array('identicalFieldValues', 'confirm_email' ),
                 'message' => 'Please re-enter your password twice so that the values match'
                )
            )
        );


    function identicalFieldValues( $field=array(), $compare_field=null ) 
    {
        foreach( $field as $key => $value ){
            $v1 = $value;
            $v2 = $this->data[$this->name][ $compare_field ];                 
            if($v1 !== $v2) {
                return FALSE;
            } else {
                continue;
            }
        }
        return TRUE;
    }

} 

In the code, the guy used a foreach to access an array member which he had its name already!
As far as I understand – it’s a waste of resources and a bad(even strange) practice.

One more thing about the code:
I don’t understand the usage of the continue there. it’s a single field array, isn’t it? the comparison should happen once and the loop will be over.
Please enlighten me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T09:56:47+00:00Added an answer on May 25, 2026 at 9:56 am

    In the code, the guy used a foreach to access an array member which he had its name already! As far as I understand – it’s a waste of resources and a bad(even strange) practice.

    The first parameter is always an array on one key and its value, the second parameter comes from the call to that function, in a block named as the key… So, all you need is to send the key and no need to iterate

    The code uses foreach to iterate through $field, which is an array of one key value pair. It all starts when the validation routine invokes identicalFieldValues, passing it two values – $field, which would be an array that looks like:

    array ( 
        [email] => 'user entered value 1'
    )
    

    The second parameter $compare_field would be set to the string confirm_email.

    In this particular case, it doesn’t look like it makes a lot of sense to use foreach since your array only has one key-value pair. But you must write code this way because CakePHP will pass an array to the method.

    I believe the reason why CakePHP does this is because an array is the only way to pass both the field name and its value. While in this case the field name (email) is irrelevant, you might need in other cases.

    What you are seeing here is one of the caveats of using frameworks. Most of the time, they simplify your code. But sometimes you have to write code that you wouldn’t write normally just so the framework is happy.


    One more thing about the code: I don’t understand the usage of the continue there. it’s a single field array, isn’t it? the comparison should happen once and the loop will be over. Please enlighten me.

    Indeed. And since there are no statements in the foreach loop following continue, the whole else block could also be omitted.

    A simplified version of this would be:

    function identicalFieldValues($field=array(), $compare_field=null) 
    {
        foreach ($field as $field) {
    
            $compare = $this->data[$this->name][$compare_field];                 
            if ($field !== $compare) {
                return FALSE;
            } 
        }
        return TRUE;
    }
    

    And I agree with you, the loop only goes through one iteration when validating the email field. regardless of the field. You still need the foreach because you are getting an array though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best way to refactor the attached code to accommodate multiple email
In my code attached below, I'm trying to upload a file via ASP.NET. I
The attached screenshot is from OS X/Firefox 3. Note that the center tab (an
Consider a table with 3 columns: ID (unique, taken from an Oracle sequence), CATEGORY
Please see the attached image: 1) I downloaded a new library here: ( http://www.java2s.com/Code/Jar/ABC/Downloadcommonslang24jar.htm
My website was recently attacked by, what seemed to me as, an innocent code:
I've created an attached behaviour in my WPF application which lets me handle the
When a function is attached to an object and called: function f() { return
I have Visual Studio web test attached nicely to a data source, but I
Is there a pdf library attached/that can be attached to .NET 3.5 that allows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.