Skip to main content
Formatting and spelling
Source Link
Stephen Rauch
  • 4.2k
  • 12
  • 23
  • 36

My method counts the number of times andan error occurs.

I have a feeling my snippet isn't the best practice way of doing this. So when one of the validation methods fails I want to stop the method.

public static function validate($data)
{
    $errors = [
        'settings_mismatch' => 0,
        'name_not_set' => 0,
        'price_not_set' => 0,
        'payment_qty_not_set' => 0,
        'payment_qty_zero' => 0,
        'payment_unit_not_set' => 0,
        'payment_unit_not_allowed' => 0
    ];


    if (!isset($data['name'])) {
        $errors['name_not_set']++;
        return $errors;
    }

    if (!isset($data['price'])) {
        $errors['price_not_set']++;
        return $errors;
    }

    if (!isset($data['payment_qty'])) {
        $errors['payment_qty_not_set']++;
        return $errors;
    }

    if (!isset($data['payment_unit'])) {
        $errors['payment_unit_not_set']++;
        return $errors;
    }

    if (isset($data['payment_qty']) && $data['payment_qty'] == 0) {
        $errors['payment_qty_zero']++;
        return $errors;
    }

    if (isset($data['payment_unit']) && !in_array(strtolower(self::transformUnit($data)['payment_unit']), self::$allowedMeasurements)) {
        $errors['payment_unit_not_allowed']++;
        return $errors;
    }

    return $errors;
}

My method counts the number of times and error occurs

I have a feeling my snippet isn't the best practice way of doing this. So when one of the validation methods fails I want to stop the method.

public static function validate($data)
{
    $errors = [
        'settings_mismatch' => 0,
        'name_not_set' => 0,
        'price_not_set' => 0,
        'payment_qty_not_set' => 0,
        'payment_qty_zero' => 0,
        'payment_unit_not_set' => 0,
        'payment_unit_not_allowed' => 0
    ];


    if (!isset($data['name'])) {
        $errors['name_not_set']++;
        return $errors;
    }

    if (!isset($data['price'])) {
        $errors['price_not_set']++;
        return $errors;
    }

    if (!isset($data['payment_qty'])) {
        $errors['payment_qty_not_set']++;
        return $errors;
    }

    if (!isset($data['payment_unit'])) {
        $errors['payment_unit_not_set']++;
        return $errors;
    }

    if (isset($data['payment_qty']) && $data['payment_qty'] == 0) {
        $errors['payment_qty_zero']++;
        return $errors;
    }

    if (isset($data['payment_unit']) && !in_array(strtolower(self::transformUnit($data)['payment_unit']), self::$allowedMeasurements)) {
        $errors['payment_unit_not_allowed']++;
        return $errors;
    }

    return $errors;
}

My method counts the number of times an error occurs.

I have a feeling my snippet isn't the best practice way of doing this. So when one of the validation methods fails I want to stop the method.

public static function validate($data)
{
    $errors = [
        'settings_mismatch' => 0,
        'name_not_set' => 0,
        'price_not_set' => 0,
        'payment_qty_not_set' => 0,
        'payment_qty_zero' => 0,
        'payment_unit_not_set' => 0,
        'payment_unit_not_allowed' => 0
    ];


    if (!isset($data['name'])) {
        $errors['name_not_set']++;
        return $errors;
    }

    if (!isset($data['price'])) {
        $errors['price_not_set']++;
        return $errors;
    }

    if (!isset($data['payment_qty'])) {
        $errors['payment_qty_not_set']++;
        return $errors;
    }

    if (!isset($data['payment_unit'])) {
        $errors['payment_unit_not_set']++;
        return $errors;
    }

    if (isset($data['payment_qty']) && $data['payment_qty'] == 0) {
        $errors['payment_qty_zero']++;
        return $errors;
    }

    if (isset($data['payment_unit']) && !in_array(strtolower(self::transformUnit($data)['payment_unit']), self::$allowedMeasurements)) {
        $errors['payment_unit_not_allowed']++;
        return $errors;
    }

    return $errors;
}

My Method counts the number of times and error occurs My method counts the number of times and error occurs

I have a feeling my snippetssnippet isn't the best practice way of doing this. So when one of the validation methods fails I want to stop the method..

public static function validate($data)
{
    $errors = [
        'settings_mismatch' => 0,
        'name_not_set' => 0,
        'price_not_set' => 0,
        'payment_qty_not_set' => 0,
        'payment_qty_zero' => 0,
        'payment_unit_not_set' => 0,
        'payment_unit_not_allowed' => 0
    ];


    if (!isset($data['name'])) {
        $errors['name_not_set']++;
        return $errors;
    }

    if (!isset($data['price'])) {
        $errors['price_not_set']++;
        return $errors;
    }

    if (!isset($data['payment_qty'])) {
        $errors['payment_qty_not_set']++;
        return $errors;
    }

    if (!isset($data['payment_unit'])) {
        $errors['payment_unit_not_set']++;
        return $errors;
    }

    if (isset($data['payment_qty']) && $data['payment_qty'] == 0) {
        $errors['payment_qty_zero']++;
        return $errors;
    }

    if (isset($data['payment_unit']) && !in_array(strtolower(self::transformUnit($data)['payment_unit']), self::$allowedMeasurements)) {
        $errors['payment_unit_not_allowed']++;
        return $errors;
    }

    return $errors;
}

My Method counts the number of times and error occurs

I have a feeling my snippets isn't the best practice way of doing this. So when one of the validation methods fails I want to stop the method..

public static function validate($data)
{
    $errors = [
        'settings_mismatch' => 0,
        'name_not_set' => 0,
        'price_not_set' => 0,
        'payment_qty_not_set' => 0,
        'payment_qty_zero' => 0,
        'payment_unit_not_set' => 0,
        'payment_unit_not_allowed' => 0
    ];


    if (!isset($data['name'])) {
        $errors['name_not_set']++;
        return $errors;
    }

    if (!isset($data['price'])) {
        $errors['price_not_set']++;
        return $errors;
    }

    if (!isset($data['payment_qty'])) {
        $errors['payment_qty_not_set']++;
        return $errors;
    }

    if (!isset($data['payment_unit'])) {
        $errors['payment_unit_not_set']++;
        return $errors;
    }

    if (isset($data['payment_qty']) && $data['payment_qty'] == 0) {
        $errors['payment_qty_zero']++;
        return $errors;
    }

    if (isset($data['payment_unit']) && !in_array(strtolower(self::transformUnit($data)['payment_unit']), self::$allowedMeasurements)) {
        $errors['payment_unit_not_allowed']++;
        return $errors;
    }

    return $errors;
}

My method counts the number of times and error occurs

I have a feeling my snippet isn't the best practice way of doing this. So when one of the validation methods fails I want to stop the method.

public static function validate($data)
{
    $errors = [
        'settings_mismatch' => 0,
        'name_not_set' => 0,
        'price_not_set' => 0,
        'payment_qty_not_set' => 0,
        'payment_qty_zero' => 0,
        'payment_unit_not_set' => 0,
        'payment_unit_not_allowed' => 0
    ];


    if (!isset($data['name'])) {
        $errors['name_not_set']++;
        return $errors;
    }

    if (!isset($data['price'])) {
        $errors['price_not_set']++;
        return $errors;
    }

    if (!isset($data['payment_qty'])) {
        $errors['payment_qty_not_set']++;
        return $errors;
    }

    if (!isset($data['payment_unit'])) {
        $errors['payment_unit_not_set']++;
        return $errors;
    }

    if (isset($data['payment_qty']) && $data['payment_qty'] == 0) {
        $errors['payment_qty_zero']++;
        return $errors;
    }

    if (isset($data['payment_unit']) && !in_array(strtolower(self::transformUnit($data)['payment_unit']), self::$allowedMeasurements)) {
        $errors['payment_unit_not_allowed']++;
        return $errors;
    }

    return $errors;
}
Source Link

PHP return validate method of if one error is found

My Method counts the number of times and error occurs

I have a feeling my snippets isn't the best practice way of doing this. So when one of the validation methods fails I want to stop the method..

public static function validate($data)
{
    $errors = [
        'settings_mismatch' => 0,
        'name_not_set' => 0,
        'price_not_set' => 0,
        'payment_qty_not_set' => 0,
        'payment_qty_zero' => 0,
        'payment_unit_not_set' => 0,
        'payment_unit_not_allowed' => 0
    ];


    if (!isset($data['name'])) {
        $errors['name_not_set']++;
        return $errors;
    }

    if (!isset($data['price'])) {
        $errors['price_not_set']++;
        return $errors;
    }

    if (!isset($data['payment_qty'])) {
        $errors['payment_qty_not_set']++;
        return $errors;
    }

    if (!isset($data['payment_unit'])) {
        $errors['payment_unit_not_set']++;
        return $errors;
    }

    if (isset($data['payment_qty']) && $data['payment_qty'] == 0) {
        $errors['payment_qty_zero']++;
        return $errors;
    }

    if (isset($data['payment_unit']) && !in_array(strtolower(self::transformUnit($data)['payment_unit']), self::$allowedMeasurements)) {
        $errors['payment_unit_not_allowed']++;
        return $errors;
    }

    return $errors;
}