2

Brief:

I have validate request value for empty string.

Code:

if(isset($request->name)) {
    $this->validate($request, [
        'name' => [
            function ($attribute, $value, $fail) {
                if (mb_strlen(preg_replace('/\s/', '', $value)) == 0) {
                    $fail($attribute.' is can't be empty.');
                }
            }
        ]
    ]);
    $user->name = $request->name;
    $user->save(); 
}

Also tired with cutom rule.

Rule code:

public function passes($attribute, $value)
{
    $result = preg_replace('/\s/', '', $value);
    return mb_strlen($result) == 0 ? false : true;
}

/**
 * Get the validation error message.
 *
 * @return string
 */
public function message()
{
    return "Attribute value can't be empty string.";
}

Rule testing code:

if(isset($request->name)) {
    $this->validate($request, [
        'name' => [
            new IsEmptyString
        ]
    ]);
    $user->name= $request->name;
    $user->save(); 
}

Question:

Where I've errors? Why laravel not sending validation error message when I check request value for empty string?

Thanks!

6
  • 1
    Of course the validations doesn't work for empty string. validation is not even call for an empty string, because you have put it inside an if condition. Commented Oct 25, 2018 at 5:51
  • It is well understood @Tharaka Dilshan , but what if I check each field separately with conditions only? How to do validation then? An empty string is also not recorded in the database in my case, but I cannot notify the user that he entered an empty field. Commented Oct 25, 2018 at 5:59
  • Other types of validation inside the condition work in my case, only when the empty string does not work. Commented Oct 25, 2018 at 6:01
  • Yes that's because you have put the validate function inside an if(isset($request->name)). don't you understand that if the name is empty, the validate function is not even called. Commented Oct 25, 2018 at 6:04
  • I understand you @Tharaka Dilshan. But how then can I tell the user that he entered an empty string? Commented Oct 25, 2018 at 6:06

2 Answers 2

1

In your situation, you must change your condition to:

if(array_key_exists('name', $request->all())) {

}

When you check isset($request->name) and this condition result return false and the validation code does not even run and for the fact that the conditions returned false.

Generally, your working code looks like this:

if(array_key_exists('name', $request->all())) {
    $this->validate($request, [
        'name' => [
            'required',
            function ($attribute, $value, $fail) {
                if (mb_strlen(preg_replace('/\s/', '', $value)) == 0) {
                    $fail($attribute." is can't be empty.");
                }
            }
        ]
    ]);

    $user->name = $request->name;
    $user->save();  
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, @Felix Cruz, your code work inside condition
0

Remove the if() condition. and then add the required rule to the validation.

$this->validate($request, [
    'name' => ['required',
        function ($attribute, $value, $fail) {
            if (mb_strlen(preg_replace('/\s/', '', $value)) == 0) {
                $fail($attribute.' is can't be empty.');
            }
        }
    ]
]);
$user->name = $request->name;
$user->save(); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.