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 8885567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:20:24+00:00 2026-06-14T21:20:24+00:00

I have a question regarding default values for model fields, this problem has been

  • 0

I have a question regarding default values for model fields, this problem has been driving me crazy for a while.

To give you an example, I have a model with a status field that must be validated:

$validate = array (
    ...
    'status' => array(
        'list' => array(
            'rule' => array('inList', array('0','1')),
            'allowEmpty' => true
        )
    )
)

The data that is inserted in the database is not submitted from a form.

If the status field is missing, I want it to default to 1, otherwise it must be validated.

Is there a way to do this from the model without using custom validation rules? I know I can set the default value in the MySQL table, but I still want to validate it from the model, in case a different value is submitted.

EDIT

I looked at the Validation class code and the inList validation is just a stupid wrapper for an in_array check:

Cake.Utility.Validation.php

public static function inList($check, $list, $strict = true) {
   return in_array($check, $list, $strict);
}

What do you know, there is also a third param that is not mentioned in the CakePHP Api docs ($strict), which defaults to true.

From the in_array docs:

If the third parameter strict is set to TRUE then the in_array()
function will also check the types of the needle in the haystack.

It still doesn’t solve my problem, but now I know why array(‘inList’, array(‘0′,’1’)) was not validating empty fields even though allowEmpty was set to true.

Thank you.

  • 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-06-14T21:20:26+00:00Added an answer on June 14, 2026 at 9:20 pm

    You can use the beforeSave method to set a default value. Don’t forget to return true or you will abort the save.

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

Sidebar

Related Questions

Good morning, I have a question regarding default values and NULL values in Sybase
I have a question regarding the style in the iframe the problem i have
I've just been reading this question which is about giving an ActiveRecord model's date
I'm reading about the initialized values by default of an array/struct and have this
I posted a question regarding this about how to figure attribute values before. Even
I have question regarding the SQLAlchemy. How can I add into my mapped class
I have question regarding the use of function parameters. In the past I have
I have question regarding disabling browser caching. I have already found few solutions, and
i have a question regarding the AsyncTask class in android, and why it is
I have a question regarding applying a RTRIM on a ASP:Hyperlink statement. The code

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.