Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

boldstar's avatar

Validating form data (text vs string)

So I am trying to submit a form using the store method with the following code

public function store(Request $request)
    {
        $data = $request->validate([
            'engagement_id' => 'required|integer',
            'question' => 'required|string',
            'answered' => 'required|boolean',
        ]);

        $question = Question::create($data);

        return response($question, 201);
    }

Now when I submit the form with the field for question set to

 'question' => 'required|string',

It will seem to validate the request but my question field is set to be [text] data type... However if I submit the form as

 'question' => 'required|text',

I will get the following message

BadMethodCallException: Method Illuminate\Validation\Validator::validateText does not exist. in file C:\laragon\www\traxit\vendor\laravel\framework\src\Illuminate\Validation\Validator.php on line 1158
Stack trace:

So should I change the question field to a [string] data type? or is there a way I can have it validate a [text] data type?

0 likes
7 replies
Swaz's avatar

In your scenario, there's not really any need to validate that the content is a string. Though you may want a max rule as the database cuts strings off at 255 characters.

$data = $request->validate([
    'engagement_id' => 'required|exists:engagements,id',
    'question' => 'required|max:255',
    'answered' => 'required|boolean',
]);
1 like
Snapey's avatar

how do you differentiate text and string ?

boldstar's avatar

well on my migration I set the field for question on my Questions table to [text]

Schema::create('questions', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('engagement_id')->unsigned();
            $table->text('question');
            $table->boolean('answered')->default(false);
            $table->timestamps();
        });

        Schema::table('questions', function(Blueprint $table)
        {
            $table->foreign('engagement_id')->references('id')->on('engagements')->onDelete('cascade');
        });

I set it to that with concerns that string may not be able to hold the size of the data... but perhaps I am misunderstanding something. What do I do if I would like the field to be able to hold more than max:255 characters?

and the text data type was an option for the migrations so i figured it made sense...

Snapey's avatar

Just set the max length in validation. PHP does not understand the difference between string and text - its just a database blueprint thing.

Cronix's avatar
Cronix
Best Answer
Level 67

But in terms of validation, both of those (migration using string/text fields) are just strings.

What do I do if I would like the field to be able to hold more than max:255 characters?

Use a text datatype like you are. Depending on your collation, it could hold about 65k chars.

1 like
boldstar's avatar

Okay i set the validate field for question back to string

'question' => 'required|string',

Everything works.

Thank you for the clarity!

Please or to participate in this conversation.