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

若如初见. 提交于 2019-12-11 09:28:50

问题


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?


回答1:


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




回答2:


I like to trim data in general for all requests. This asserts that nonsense whitespace added does not make the validation unfunctional:

public function startup(Event $event) {
    // Data preparation
    if (!empty($this->Controller->request->data) && !Configure::read('DataPreparation.notrim')) {
        $this->Controller->request->data = $this->trimDeep($this->Controller->request->data);
    }
    if (!empty($this->Controller->request->query) && !Configure::read('DataPreparation.notrim')) {
        $this->Controller->request->query = $this->trimDeep($this->Controller->request->query);
    }
    if (!empty($this->Controller->request->params['pass']) && !Configure::read('DataPreparation.notrim')) {
        $this->Controller->request->params['pass'] = $this->trimDeep($this->Controller->request->params['pass']);
    }

So maybe use such a component hook to clean your data prior to using it anywhere in your controller or model layer.

Source 2.x: https://github.com/dereuromark/cakephp-tools/blob/2.x/Controller/Component/CommonComponent.php#L45-L57

Source 3.x: https://github.com/dereuromark/cakephp-tools/blob/master/src/Controller/Component/CommonComponent.php#L25-L34




回答3:


You could use beforeRules callback and use trim () before data is validated.




回答4:


You could use beforeRules callback and use trim () before data is validated.

Edit: A simple example:

public function beforeRules($event, $entity, $options, $operation){
    $entity->set ('yourFieldname', trim ($entity->get ('yourFieldname')));
    return parent:: beforeRules($event, $entity, $options, $operation);

Put this in your table class.




回答5:


you need to add beforeMarshal in every model if you want to trim each record.

here is the working code

// Include use statements at the top of your file.
use Cake\Event\Event;
use ArrayObject;

// In a table or behavior class
public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options)
{
    foreach ($data as $key => $value) {
        if (is_string($value)) {
            $data[$key] = trim($value);
        }
    }
}

here is the official reference of cakephp https://book.cakephp.org/3.0/en/orm/saving-data.html#modifying-request-data-before-building-entities



来源:https://stackoverflow.com/questions/28531521/cakephp-3-how-to-use-trim-before-validation-notempty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!