How to write data without Jquery mask using Cakephp3

Asked

Viewed 287 times

0

I have a system in Cakephp 3.4.5 and am using jquery to apply a mask to the cep, Cpf, cnpj and etc.
How do I save the data without the mask?
I call Jquery in my default.ctp which is in my layout folder.

echo $this->Html->script('jquery.maskedinput.min');

I also put the information mask cobnforme code below.

echo $this->Html->scriptBlock('jQuery(function($){ $("#cpf").mask("999.999.999-99"); });', array('inline' => false));
echo $this->Html->scriptBlock('jQuery(function($){ $("#cep").mask("99999-999"); });', array('inline' => false));

Grateful

  • When submitting your form gives an replace, no?

  • How should I do it, @Aline?

  • Take a look: https://www.w3schools.com/php/func_string_str_replace.asp

  • I’m sorry, but I don’t know how to apply this. ?

  • Sorry it took so long. but it’s like Bruno explained to you. (=

2 answers

1


The way to make mask via PHP, take the clean number, example 12345678910 and turn it into 123.456.789-10 would be with the code below:

$cpf = "12345675910";
echo $cpfMask = substr($cpf, 0, 3) . "." . substr($cpf, 3,3) .  "." . substr($cpf, 6,3) . "-" . substr($cpf, 9);

1

You can remove what you don’t want in the backend. Where you restore data, clear the variable before it is saved. Example:

<?php

// Remove qualquer caracter diferente de número
// Antes 123.456.789-10
$cpf = preg_replace("/\D/i", "", $cpf);
// Depois 12345678910

?>
  • Got it. That way I have CPF without mask. But how do I display with mask and record without mask? <div class='col-Md-3'> <? php echo $this->Form->input('Cpf', ['class' => 'Cpf big-field inline-field', 'required' => 'required', 'label' => ['text' => 'CPF', 'class' => 'small-field form-input Mandatory']]); ? > </div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.