# |
Dec 1st 2017, 21:40 |
phantomwatson |
The code that I posted was in the context of a controller method. `$this->request` isn't available in a table. |
# |
Dec 1st 2017, 21:39 |
obinoob |
beforeSave() in table right? |
# |
Dec 1st 2017, 21:37 |
obinoob |
and make them ->empty('number') |
# |
Dec 1st 2017, 21:37 |
phantomwatson |
Like ``` $data = $this->request->getData(); foreach ($data as $datum) { ... (if a number is empty(), remove that 'Phone' member from $data) ... } ``` |
# |
Dec 1st 2017, 21:36 |
phantomwatson |
Though there's likely a more elegant solution involving putting that logic in a `beforeSave()` callback. |
# |
Dec 1st 2017, 21:36 |
phantomwatson |
If it were me, I'd loop through the request data and remove empty phone numbers before saving. |
# |
Dec 1st 2017, 21:34 |
obinoob |
the thing is how can it be done? |
# |
Dec 1st 2017, 21:34 |
obinoob |
exactly |
# |
Dec 1st 2017, 21:34 |
phantomwatson |
It sounds like you need to actually `allowEmpty('number')` and then add some logic in your controller or `PhonesTable` that ignores any `Phone`s with empty numbers instead of saving them to the database. |
# |
Dec 1st 2017, 21:32 |
phantomwatson |
Is there any error that is still occurring that you would like help solving? |
# |
Dec 1st 2017, 21:32 |
obinoob |
but then I need to provide two phone number as they are not allowed to be empty... |
# |
Dec 1st 2017, 21:32 |
obinoob |
no, with not Empty() it will fail in validation as expected |
# |
Dec 1st 2017, 21:31 |
phantomwatson |
Even after you added the `notEmpty()` validation rule? |
# |
Dec 1st 2017, 21:30 |
obinoob |
at phones table |
# |
Dec 1st 2017, 21:30 |
obinoob |
no they are not however the entity is filling up the id and client_id automatically and filling the phone number with null |
# |
Dec 1st 2017, 21:29 |
phantomwatson |
So they're not obligated to enter two phone numbers? |
# |
Dec 1st 2017, 21:29 |
obinoob |
only saving whats available |
# |
Dec 1st 2017, 21:29 |
obinoob |
quite the opposite I only want to save registry in database if available I can't predict if client has or wants to give phone number that are the business rules get that so I can't force them to be required |
# |
Dec 1st 2017, 21:27 |
phantomwatson |
It sounds like you need some client-side validation to require values for both phone numbers. if you also include `$validator->requirePresence('number')`, I think the FormHelper will automatically mark the field as required, and modern browsers will automatically prevent the form from being submitted until a value is entered. |
# |
Dec 1st 2017, 21:24 |
obinoob |
but the deal is it will fail and I'm obligated to enter two phone numbers |
# |
Dec 1st 2017, 21:24 |
obinoob |
sorry not it doesn't |
# |
Dec 1st 2017, 21:23 |
obinoob |
yes it does |
# |
Dec 1st 2017, 21:23 |
phantomwatson |
Even after you add the `notEmpty()` validation rule it still saves empty values for `number`? |
# |
Dec 1st 2017, 21:22 |
obinoob |
phantomwatson the first time it will save empty but the second the "NULL" value is duplicated in DB and by then it will throw a SQL exception at uniqueness constraint |
# |
Dec 1st 2017, 21:19 |
obinoob |
phantomwatson INSERT INTO `phones` (`number`, `client_id`, `created`) VALUES (NULL, 44, '2017-12-01 20:38:05') |
# |
Dec 1st 2017, 21:19 |
phantomwatson |
In the `PhonesTable` class, try adding `$validator->notEmpty('number');` to `validationDefault()` and removing `allowEmpty('number')` if it's there. |
# |
Dec 1st 2017, 21:18 |
obinoob |
phantomwatson entity is saving empty phone... |
# |
Dec 1st 2017, 21:15 |
phantomwatson |
What is the error that you're trying to correct? |
# |
Dec 1st 2017, 21:14 |
obinoob |
next when I enter a second client with one phone number (a second empty) it fails due to UNIQUE constraint obviously I'm not allowing duplications in database... |
# |
Dec 1st 2017, 21:12 |
obinoob |
phantomwatson yes I can, I have and entity with multiple association ex: client hasMany phones! in form I have two inputs for phone numbers (phones.0.number and phones.1.number) so if only enter one phone number the second goes empty. with client FK id and PK id as well... |
# |
Dec 1st 2017, 21:07 |
phantomwatson |
*Question for the rest of the channel:* can anyone recommend a way to change a component configuration value from inside an integration test? (I'm trying to turn off reCAPTCHA response checking in a test.) |
# |
Dec 1st 2017, 21:05 |
phantomwatson |
You need a validation rule that invalidates entities if they're missing a foreign key for an association? |
# |
Dec 1st 2017, 21:05 |
phantomwatson |
Could you be more specific in your question? I don't understand. |
# |
Dec 1st 2017, 21:04 |
obinoob |
anyone willing to help? |
# |
Dec 1st 2017, 20:26 |
obinoob |
how can I avoid entity from saving association if empty allowed and no value is passed? |
# |
Dec 1st 2017, 19:56 |
obinoob |
h.mavisakalian I would start by checking the XHR request under Network -> Chrome (or oder) so you can take a closer look at "Form Data" then obviously debug($this->request->data())... in controller action |
# |
Dec 1st 2017, 19:48 |
h.mavisakalian |
I am fetching data from a JSON url |
# |
Dec 1st 2017, 19:48 |
h.mavisakalian |
? |
# |
Dec 1st 2017, 19:48 |
h.mavisakalian |
Any ideas |
# |
Dec 1st 2017, 19:48 |
h.mavisakalian |
“General error: 1366 Incorrect string value: ” |
# |
Dec 1st 2017, 19:48 |
h.mavisakalian |
I get an error while saving data to database |