Is it possible to not display default values from database table definition?

I’ve tried setting default => false, null, and 0 in $this->Form->control() but in all cases the default value defined in the database is populated in the form for an empty entity.

My aim with having default values in the database is to just have something to fall back to in the event the user doesn’t input a value… not to offer them to the user as “suggested” values.

Yup.

In your src/Model/action.php add this ['value' => '' to your Form->control maker.
Like

<?= $this->Form->control('field_name', ['value' => '']) ?>

Where Model & action & field_name are your controller fields.

There may be a more Cake-polite way of doing it, but raw entry of specific html control parameters does it!
You can put your instructions for them in the placeholder, like the actual value: -

<?= $this->Form->control('field_name', ['value' => '', 'placeholder' => $model->field_name]) ?>
1 Like

This file, “src/Model/action.php” does not exist.

Sorry, I put the wrong folder (tripped up on my own feet), it’ll be /template/Model/action.php where you replace Model & action with your folder & filename for where you need this change, and in that control maker <?= $this->Form->control(' put the 'value' => '' as a second parameter as my first post does say!

I see what you mean… you mean in the view file… where I am echoing the form helper to create the html for the field.

There is a problem with this approach however. It means that if there are user inputted values already in the database, they will not appear in the fields. If I want the user to edit the entity, this approach would not work for me.

I see what you’re saying, but you should have a template for both add and edit, so you only need to do it in add. I don’t suppose you can just remove that default value from the database itself? I mean, you must have the credentials for it to add & edit, unless you’re restricted to being unable to restructure the table. Even still, if you don’t want or need those defaults, have them removed by whoever can do that. Database problem = database solution.

There may be a polite Cake way of doing it, so if anyone else what that is (perhaps in the Entity) then please post :slight_smile:

Yeah, I generally combine my add and edit actions into one.

They are rather different operations though. You’re taking the C out of CRUD (hehe).

But yea, Add creates a blank entity, Edit pulls it from table. Even the URL is different as edit will have an extra /1 or whatever.

I would at least say in this situation for you generally doesn’t apply anyway!

So either split off a create/add actions or fix the defaults in your database.

Or, and, I mean, if you really want a non-Cake solution write a java script which after load locates your control and clears the value - but I so don’t recommend this - use Cake as it was designed, in the long run it’ll make less work for you.

I had hoped the developers had simply thought of being able to suppress database defaults from showing in the UI. $this->Form->control(…, [‘default’=false]) seems to me to be how it should work. As well as allowing us to override the database default with our own value.

The way Cake implements it, it’s “too smart” imo. Now I feel like I have to get rid of all the defaults I set up in the database. Which, honestly, are mostly about making it easier to create records on the back end for seeding, testing, or other purposes — without having to be troubled with manually inputting every last detail. An admin sometimes faces the need to create new records, and some fields are simply required for the data to work correctly in the app while not being relevant (at least at the time) to whatever the admin has to accomplish, knowing such records can have their details fleshed out later (or the records completely deleted).

Defaults in the database make placeholder text non-viable. Great for data integrity, terrible for UX. I can’t recall any time as a user I needed to see a default value show up in a form field. Never. Ok, in some applications, default values for settings or parameters are absolutely called for. But that is a very specific use case.

As for combining add and edit, it’s because they are exactly the same thing in most cases. The only differences being whether an ID exists or not, and whether there is already data or not. There are an awful lot of records that are created for app development that are never edited. Maybe exceptionally so. Why recreate the exact same functionality?

Think of when you create a new Word Doc. Does the GUI look any different compared to when you open it later? Nope. The only functional difference between creating a new document and editing an existing document is that the new document has no user-generated name and hasn’t been committed to disk. Other than that, they are the exact same “form” presented to the user.

On that same note, I wish Cake would allow for reusing edit actions and views but with read-only access applied. I don’t need a whole entire view action and view just to look at the same data in the same overall layout as you would find in the edit version. A switch like that would be a monumental time saver. Just skip generating the form and render form controls as pure values. Done.

All good points.

Maybe make a suggestion for a default parameter, and perhaps for a read-only parameter too (which really just needs to not create the submit button). Bear in mind your approach is directly opposed to the CakePHP design approach (as I see it) where the action is part of the URL; like asking Word to support command mode editing like vi.

I only threw that placeholder in there as an explanation for anyone else coming across this and wasn’t aware of the html element overrides.

I keep the URLs the same. What I do is pass the call inside the action:

public function add($id=null)
{
  $this->edit($id);
}

And if there is anything exceptional I want to handle, I do it before (or after in some cases) that call. Typically that may mean setting certain variables that the view will need.

It’s absolutely possible to set a default value that the input field will use in the case of a new entity, and I’m pretty sure that it will override whatever the default value is from the database. I don’t think null works for this, though. I’m surprised that false doesn’t, maybe it’s because of the field type? Did you try ''?

You don’t have some custom bit of code that initializes your new entities with the default values from the database, do you? (Or has Cake added that in a more recent version?) Because anything in the entity will take precedence over any default you provide (quite rightly, or else your edit function wouldn’t work!)

Its totally posible to reuse the view for add/actions, and with crud plugin its more magic.

public function add() {
  $this->viewBuilder()->setName('edit');
  if ($this->request->isPost()) {
    // handle add
  }
}
public function edit() {
  if ($this->request->isPost()) {
    // handle edit
  }
}

As for defaults from the db, you can use seeds to generate dummy data, instead of reliying on defaults.

Can you try the method newEmptyEntity() instead of newEntity() (if you are using cake 4)