PHP - 返回真|假的验证函数,如果为假则返回一条消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5765057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:25:16  来源:igfitidea点击:

PHP - Validation function to return true|false, AND a message if false

phpfunction

提问by Majid Fouladpour

I have a validation function which returns either trueor false.
However, I want it to provide info as to what the problem is, when there is one.

我有一个验证函数,它返回truefalse
但是,当有问题时,我希望它提供有关问题是什么的信息。

Let's say the function is like this:

假设函数是这样的:

function is_valid($val) {
  $result = true;
  if( rule_1_not_met ) $result = false;
  if( rule_2_not_met ) $result = false;
  return $result;
}

Which is used like this

像这样使用

$val = $_GET['some_param'];
if(!is_valid($val)) $out .= 'Not so helpful feedback.';
...

I thought I could change it like this:

我以为我可以这样改变它:

function is_valid($val) {
  $result = array(true, array());
  if( rule_1_not_met ) $result[1][] = 'Reason 1';
  if( rule_2_not_met ) $result[1][] = 'Reason 2';
  if(count($result[1]) > 0) $result[0] = false;
  return $result;
}

And use it like this:

并像这样使用它:

$val = $_GET['some_param'];
$validation_result = is_valid($val);
if(!$validation_result[0]) $out .= implode('<br/>', $validation_result[1]);
...

My question is

我的问题是

  • Am I in, for unexpected results with this?
  • Are there better ways to achieve this?
  • 我在,因为这出乎意料的结果吗?
  • 有没有更好的方法来实现这一目标?

P.S. Would make this community wiki

PS将使这个社区维基

回答by Shakti Singh

You are in the right track but I would like to do this in this way

你在正确的轨道上,但我想以这种方式做到这一点

function is_valid($val,&$mes) {
  $result = true;
  if( rule_1_not_met ) { $mes[]='message one'; $result = false; }
  if( rule_2_not_met ) { $mes[]='Message two'; $result = false; }
  return $result;
}

$mes=array();
if(isvalid($val,$mes) ===false)  $out .= implode('<br/>', $mes);

回答by Brad Thomas

You could use a Result object that encapsulates return data, a message and a status.

您可以使用封装返回数据、消息和状态的 Result 对象。

i.e.

IE

class Result( $bResult, $sMessage, $mData ) {
    public function __construct() {
        $this->bResult = $bResult;
        $this->sMessage = $sMessage;
        $this->mData = $mData;
    }
}

In Your code:

在您的代码中:

$result = new Result(true, 'some helpful message here', null);

回答by Mark Tomlin

$reasons = array();
function is_valid($val)
{
    global $reasons;
    if ( rule_1_not_met ) $reasons[] = 'Reason 1';
    if ( rule_2_not_met ) $reasons[] = 'Reason 2';
    if ( count($reasons) == 0 )
        return TRUE;
    else
        return FALSE;
}

if (!is_valid($condition))
{
    echo 'Was not valid for these reasons<br />';
    foreach($reasons as $reason)
        echo $reason, '<br>';
}
else
    echo 'Is valid!';

回答by Kodos Johnson

I think the solutions suggested here are great. I just wanted to add my two cents along with another possible solution.

我认为这里建议的解决方案很棒。我只是想加上我的两分钱以及另一种可能的解决方案。

In my opinion, your proposed solution works pretty good. The only problem with it is that you have to rememberthat $validation_result[0]is the status and $validation_result[1]contains the messages. This might be OK with you but it will be hard to maintain if other people use your code. For that reason, I like Brad Thomas's solutionof creating a specialized class that contains the messages and status. Since the properties are named, you don't have to guess how to access the validation information. Also, most good IDEs will autocomplete when you try to access their properties.

在我看来,您提出的解决方案效果很好。唯一的问题是您必须记住$validation_result[0]是状态并$validation_result[1]包含消息。这对您来说可能没问题,但如果其他人使用您的代码,将很难维护。出于这个原因,我喜欢Brad Thomas创建一个包含消息和状态的专门类的解决方案。由于属性已命名,因此您不必猜测如何访问验证信息。此外,当您尝试访问其属性时,大多数优秀的 IDE 都会自动完成。

I have an alternate solution. Instead of including a boolean true or false. Just return the array of messages. The caller would just have to check if the returned array has a non-zero number of errors. Here is an example:

我有一个替代解决方案。而不是包含布尔值 true 或 false。只需返回消息数组。调用者只需检查返回的数组是否有非零错误数。下面是一个例子:

function get_errors($val) {
    $errors = array();
    if( rule_1_not_met ) $errors[] = 'Reason 1';
    if( rule_2_not_met ) $errors[] = 'Reason 2';
    return $errors;
}

Then the caller would use it like this:

然后调用者会像这样使用它:

$val = $_GET['some_param'];
$validation_result = get_errors($val);
if (count($validation_result) > 0) $out .= implode('<br/>', $validation_result);