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

Monday, February 21, 2022

[FIXED] Auto merge two form input values to one before patchEntity in CakePHP 3

 February 21, 2022     cakephp, cakephp-3.0, php     No comments   

Issue

I have table Users that has telephone field.

The problem is if I want to seperate user telephone number into 2 textboxes when showing user information and vice versa when User submits form, auto merges them into one value before patchEntity() and save() to database.

Can CakePHP make it nice and easy using CakePhp Form Helper?

Thanks.


Solution

You can use Model.beforeMarshal event to modify , restructure request data before patching entitiy , The beforMarshal event is triggered just before the validation process. For example , to concatenate two form values to one you can do following

// In a table or behavior class
public function beforeMarshal(Event $event, $data)
{
   $data['telephone'] = $data['telephone_1'].' '.  $data['telephone_2'];
}

Do not forget to add this statement use Cake\Event\Event; at the top of your table or behavior class. For more info about Model.beforeMarshal see http://book.cakephp.org/3.0/en/orm/saving-data.html#modifying-request-data-before-building-entities



Answered By - N Nem
  • 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