PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 16, 2022

[FIXED] CakePHP 3 - How to use Trim() before validation NotEmpty?

 January 16, 2022     cakephp, cakephp-3.0, php, validation     No comments   

Issue

My form validate empty fields, but if the user use "space", the validation handle how one character.. How to use Trim() in Model-Table for it not happen?


Solution

Assuming that you have a title column in Posts table and you want to trim title before validating.

put following code in src\Model\Table\PostsTable.php

public function beforeMarshal(Event $event, ArrayObject $data)
    {
        $data['title'] = trim($data['title']);
    }

and add following two lines at the top of src\Model\Table\PostsTable.php

use Cake\Event\Event;
use ArrayObject;

Thanks



Answered By - Fazal Mohammad
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing