app/Dorcas/Support/helpers.php
/**
 * Converts a validation exception to the appropriate human-understandable text.
 *
 * @param ValidationException $exception
 *
 * @return array
 */
Converts a validation exception to the appropriate human-understandable text.

@return array
Converts a validation exception to the appropriate human - understandable text . @return array
function validation_errors_to_messages(ValidationException $exception)
{
    $dependentFieldChecks = [
        'required_if',
        'required_unless',
        'required_with',
        'required_with_all',
        'required_without',
        'required_without_all'
    ];
    $messages = [];
    $errors = [];
    foreach ($exception->validator->failed() as $field => $failures) {
        foreach ($failures as $rule => $data) {
            $errors[$field][$rule] = is_array($data) ? implode(', ', $data) : $data;
        }
    }
    foreach ($exception->errors() as $field => $failures) {
        $fieldErrors = $errors[$field] ?? [];
        foreach ($failures as $id) {
            $components = explode('.', $id);
            $checkName = count($components) > 1 ? $components[1] : $components[0];
            $errorKey = str_replace('_', '', title_case($checkName));
            $attributes = [
                'attribute' => $field,
                'values' => $fieldErrors[$errorKey],
                strtolower($checkName) => $fieldErrors[$errorKey]
            ];
            if (in_array($checkName, $dependentFieldChecks)) {
                $split = explode(',', $fieldErrors[$errorKey]);
                $additional = ['other' => $split[0]];
                if (count($split) === 2) {
                    $additional['value'] = trim($split[1]);
                } elseif (count($split) > 2) {
                    unset($split[0]);
                    $additional['value'] = implode(',', $split);
                }
                $attributes = array_merge($attributes, $additional);
            }
            if (isset($messages[$field]) && !is_array($messages[$field])) {
                $messages[$field] = (array) $messages[$field];
            }
            if (!empty($messages[$field]) && is_array($messages[$field])) {
                $messages[$field][] = trans($id, $attributes, 'en');
            } else {
                $messages[$field] = trans($id, $attributes, 'en');
            }
        }
    }
    return $messages;
}
function validation_errors_to_messages ( ValidationException $ exception ) { $ dependentFieldChecks = [ 'required_if' , 'required_unless' , 'required_with' , 'required_with_all' , 'required_without' , 'required_without_all' ] ; $ messages = [ ] ; $ errors = [ ] ; foreach ( $ exception -> validator -> failed ( ) as $ field => $ failures ) { foreach ( $ failures as $ rule => $ data ) { $ errors [ $ field ] [ $ rule ] = is_array ( $ data ) ? implode ( ', ' , $ data ) : $ data ; } } foreach ( $ exception -> errors ( ) as $ field => $ failures ) { $ fieldErrors = $ errors [ $ field ] ?? [ ] ; foreach ( $ failures as $ id ) { $ components = explode ( '.' , $ id ) ; $ checkName = count ( $ components ) > 1 ? $ components [ 1 ] : $ components [ 0 ] ; $ errorKey = str_replace ( '_' , '' , title_case ( $ checkName ) ) ; $ attributes = [ 'attribute' => $ field , 'values' => $ fieldErrors [ $ errorKey ] , strtolower ( $ checkName ) => $ fieldErrors [ $ errorKey ] ] ; if ( in_array ( $ checkName , $ dependentFieldChecks ) ) { $ split = explode ( ',' , $ fieldErrors [ $ errorKey ] ) ; $ additional = [ 'other' => $ split [ 0 ] ] ; if ( count ( $ split ) === 2 ) { $ additional [ 'value' ] = trim ( $ split [ 1 ] ) ; } elseif ( count ( $ split ) > 2 ) { unset ( $ split [ 0 ] ) ; $ additional [ 'value' ] = implode ( ',' , $ split ) ; } $ attributes = array_merge ( $ attributes , $ additional ) ; } if ( isset ( $ messages [ $ field ] ) && ! is_array ( $ messages [ $ field ] ) ) { $ messages [ $ field ] = ( array ) $ messages [ $ field ] ; } if ( ! empty ( $ messages [ $ field ] ) && is_array ( $ messages [ $ field ] ) ) { $ messages [ $ field ] [ ] = trans ( $ id , $ attributes , 'en' ) ; } else { $ messages [ $ field ] = trans ( $ id , $ attributes , 'en' ) ; } } } return $ messages ; }
Converts a validation exception to the appropriate human-understandable text.
Converts a validation exception to the appropriate human - understandable text .
# checks that have dependent fields # get the specific errors for the field -- we'll need this to get additional validation data # split up the id # we parse out the check name from the id # now we see if we got it # this field is one of those that has dependencies # we set the attributes