Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Laravel Validate String?

When I use Laravel's string validation rule, what exactly is it doing? For example, does it simply run php's is_string() method, or does it run filter_var() perhaps, with the FILTER_VALIDATE_REGEXP constant?

Secondly, how do I strip tags using Laravel?

like image 994
DatsunBing Avatar asked Nov 18 '25 08:11

DatsunBing


2 Answers

When you use Laravel's string validation, the following function gets called

protected function validateAttribute($attribute, $rule) { ... }

which you could find in

vendor/laravel/framework/src/Illuminate/Validation/Validator.php

and if you examine the method, you'll find this piece of code

$method = "validate{$rule}";

if ($validatable && ! $this->$method($attribute, $value, $parameters, $this)) {
    $this->addFailure($attribute, $rule, $parameters);
}

which would call the trait Concerns\ValidatesAttributes

/**
 * Validate that an attribute is a string.
 *
 * @param  string  $attribute
 * @param  mixed   $value
 * @return bool
 */
public function validateString($attribute, $value)
{
    return is_string($value);
}

and to strip tags you could use PHP's strip_tags method

like image 93
linktoahref Avatar answered Nov 21 '25 06:11

linktoahref


When I use Laravel's string validation rule, what exactly is it doing?

When you use Laravel's string validation rule, it only checks if the field under validation is of string type or not.

https://laravel.com/docs/5.0/validation#rule-string

how do I strip tags using Laravel?

You can use the strip_tags() function:

http://php.net/manual/en/function.strip-tags.php

like image 41
Manu Bhardwaj Avatar answered Nov 21 '25 05:11

Manu Bhardwaj