# Validation for a form select box and text area

**URL:** <https://discourse.cakephp.org/t/validation-for-a-form-select-box-and-text-area/11667>\
**Category:** Uncategorized\
**Created:** [November 13, 2023, 5:58pm UTC](https://discourse.cakephp.org/t/validation-for-a-form-select-box-and-text-area/11667 "2023-11-13T17:58:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)\
**Post date:** [November 13, 2023, 5:58pm UTC](https://discourse.cakephp.org/t/validation-for-a-form-select-box-and-text-area/11667/1 "2023-11-13T17:58:19Z")

</div>

I’m trying to add validation to recipes when they’re submitted. The recipe category needs to be selected from a select box. I tried adding an id to the form element (select box) with some jquery and I tried adding the “required” key word like you can do for form fields but neither worked. Also when I use -\>notEmptyString(‘directions’) for another form element, it doesn’t let the form be submitted even when the field is filled in. ‘directions’ is a text area and when I add text to it, it still doesn’t submit. I’m not able to add the “required” key word to the text area either.

In my RecipesTable.php

```auto
public function validationDefault(Validator $validator): Validator
    {
        
        $validator
            ->scalar('directions')
            ->allowEmptyString('directions');
			//->notEmptyString('directions');
        $validator
            ->scalar('category_id')
            //->notEmptyString('category_id');
			->allowEmptyString('category_id');
			
        return $validator;
    }

```

when I use -\>notEmptyString(‘category\_id’);, the form can’t be submitted even when the category\_id is given. When I use -\>allowEmptyString(‘category\_id’);, the form can be submitted but obviously there’s no validation for the category select box.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [November 14, 2023, 4:44am UTC](https://discourse.cakephp.org/t/validation-for-a-form-select-box-and-text-area/11667/2 "2023-11-14T04:44:59Z")

</div>

`notEmptyString` and `required` are two different things. `notEmptyString` checks that _if a value is present, it is not empty_. It does not require that a value be present. For that, you need `->requirePresence('category_id', 'create')`. This, in conjunction with `notEmptyString` should guarantee that there is a category\_id value in the submitted data, and that it is not blank.

As for what’s going wrong with directions, I can’t say. `notEmptyString` should definitely allow it through when you have put text there. I guess check that you haven’t got a typo in a field name anywhere that’s messing this up.

---

<div class="post-metadata">

**Author:** ![makamo66](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/makamo66/32/2156_2.png) [@makamo66](https://discourse.cakephp.org/u/makamo66)\
**Post date:** [November 14, 2023, 9:03pm UTC](https://discourse.cakephp.org/t/validation-for-a-form-select-box-and-text-area/11667/3 "2023-11-14T21:03:55Z")

</div>

Thanks, Zuluru.  
I put aside the task with “category\_id” for now and concentrated on the textarea field called “directions.”  
I commented out all of the rest of the code in case there was an error, but I still couldn’t submit the form even when the “directions” textarea was filled in. I’m using ckEditor with the ID of “editor”. This line of code still never let the form submit even when the textarea was filled out:` ->notEmptyString('directions');` I tried adding rules for minimum length and tried to find out what scalar even meant, but to no avail. My directions field in the database has the data type of TEXT.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [November 15, 2023, 12:06am UTC](https://discourse.cakephp.org/t/validation-for-a-form-select-box-and-text-area/11667/4 "2023-11-15T00:06:15Z")

</div>

Is `directions` [“accessible”](https://book.cakephp.org/4/en/orm/entities.html#mass-assignment) in your entity class? If not, then whatever you send in that field will never make it into your entity, and the validation will of course always fail.

If it is, you’ll need to show us your form code for that field, and/or a debug dump of the data that is being received by your controller.
