CRUD values from model don't propagate to boolean and dropdown values


#1

Hi there. I have recently started with this framework so I might not know things. Anyway, I need some help to figure out how to do what I want.

I have a model called HeroItem that is populated from my database. I have built a simple CRUD admin interface to edit items that belong to heroes, for example.

Question 1 - replace integer key with label

There is a field called type which is integer – type of item such as weapon, armor, potion etc. First of all, I didn’t found how to supply values native so I have used a hack described here https://github.com/atk4/ui/issues/251#issuecomment-340202939

I used this snippet to alter the form value

function init()
    {
        parent::init();

        $values = [
            1 => 'Junk',
            2 => 'Recipe',
            3 => 'Quest',
            4 => 'Weapon',
            5 => 'Armor',
// etc...
        ];

        $this->getElement('type')->ui['form'] = new \atk4\ui\FormField\DropDown(['values' => $values]);
    }

This works great for the dropdown in the form, I see the label and not the key. However it is still displayed as a numeric value in the grid. How an I solve that?

Question 2 - checkbox is not checked for boolean=true columns

On the previos screenshot item with type=4 is equipped. This is correct. But when I click on edit and see the form – it is not checked and if I save it I will lose my value. If I manually tick it it will work fine. But I often forget to do that. See screenshot below.

Thank you! Hope we can resolve it without hacking the engine. I can provide the code of my models if that is needed.


#2

Question 1.

This is one area where there are a bit of confusion as to which implementation to use, which is why it may appear as un-finished. We have discussed it few times on the Gitter but I’ll try to convey the essence here:

  • Agile Data cares about values an how to store them.
  • Agile UI cares about presentation and picking a right field

So although it’s easy to specify $values for the UI DropDown field, it’s outside of “area of concern” for the Data. But after some consideration, I think we’ll end up having $field->values = [] which provides a dictionary on how to display numerical values to the user.

Once implemented, the PR will also update “Table” to properly interpret the values and display ‘Recipe’, ‘Quest’ etc instead of the number.

For a short term solution - create ItemType model with 2 columns - id and name. Then use:

$model->hasOne('type', new ItemType());

Question 2.

If your field is defined as [‘type’=>‘boolean’] then it should work for both Form and Column. For the database I use “bool” type (that stores as 1/0). There are also ways to provide custom store values for booleans through enum property. Assuming that you’re not doing any of that, it seems to be a bug. I’m going to quickly put together an app to see if it works OK.

Overall - pretty good questions :slight_smile: not so easy to answer…


#3

Question 1.

Yeah, I have read those tickets and they totally make sense. From the user perspective, such flexibility is a must, although it’s not data and not ui but something in between.

I will try your solution but will be able to do that only tomorrow. However, it will be a pain as I have 6-7 tables like that and some have 2-3 fields with keys. Having 10 extra models just for that is an overkill.

Question 2.
My SQL datatype reads the following in my propel config (this is the ORM I use)
<column name="is_equipped" phpName="IsEquipped" type="BOOLEAN" size="1" required="true" defaultValue="false"/>

And I have manually casted the type to boolean with
$this->addField('is_equipped', ['type' => 'boolean']);

However, inside the database, it is translated into TINYINT(1). Can this be the case? I can try to alter column type but not sure it will work.

Again, this works well for the table but not for the form. Let me know if you need a var_dump or something from my code.

I don’t mind enum, but it threw errors when I tried to add it. Maybe the syntax was wrong but I started just recently :slight_smile:


#4

At least with MySQL thats how booleans are stored, read here:

Best Regards
Philipp


#5

Yeah, I was afraid the type was wrong. Seems to be not the case.


#6

Hey @dako.lrd, I’m going to try and find proper solution for both questions later tonight. I’ll post update here.

I’ll add some examples also. Give them a go tomorrow.


#7

@romaninsh, absolutely, will do that. Thank you.


#8

@romaninsh hi, any updates on that? I saw a pr for the checkbox value, but looks like you still need to review it.


#9

yes, I was helping a customer with release, so it ate away some time. I’m going to look into this now, thanks for your patience.


#10

Here is the PR: https://github.com/atk4/ui/pull/291 which is now waiting for review. Can you try this branch?

I have also implemented Radio buttons, but they will require multiple PRs to be merged and support for value lists now (your 1st question).


#11

Sorry for late reply, I tried the dev branch but now I no longer see the form when I click on Edit inside the CRUD table. I was not able to test the fix as the response now has only the Save button:

{  
   "success":true,
   "message":"Success",
   "atkjs":"<script>\n$(function() {\n  $(\"#atk_admin_crud_virtualpage_form\").api({\"url\":\"\/web\/admin\/road.php?atk_admin_crud_virtualpage_callbacklater=cut\\x26atk_admin_crud=4\\x26json=true\",\"method\":\"POST\",\"serializeForm\":true}).form({\"inline\":true,\"on\":\"blur\"});\n  $(\"#atk_admin_crud_virtualpage_form\").on(\"change\",\"input\",function(event) {\n    event.preventDefault();\n    event.stopPropagation();\n    $(\"#atk_admin_crud_virtualpage_form\").form(\"remove prompt\",$(this).attr(\"name\"));\n  });\n  $(\"#atk_admin_crud_virtualpage_form_columns_button\").on(\"click\",function(event) {\n    event.preventDefault();\n    event.stopPropagation();\n    $(\"#atk_admin_crud_virtualpage_form\").form(\"submit\");\n  });\n})<\/script>",
   "html":"<div id=\"atk_admin_crud_virtualpage\" class=\"ui  container\" style=\"\" ><form id=\"atk_admin_crud_virtualpage_form\" class=\"ui  form\" style=\"\" >\n\n\n\n<div id=\"atk_admin_crud_virtualpage_form_columns_button\" class=\"ui primary button\" style=\"\" >Save<\/div><input id=\"atk_admin_crud_virtualpage_form_view\" class=\"  \" style=\"display:none\" name=\"atk_admin_crud_virtualpage_form_submit\" value=\"submit\"><\/input><\/form><\/div>",
   "id":"atk_admin_crud_virtualpage"
}

I have to go right now but I will be able to test it more in the evening. The CRUD table works just fine, but the form is empty for me. On all of my models.

Maybe this is related to updates in core, dsql and data? I had to set composer to "minimum-stability" : "dev", and this downloaded new versions of those packages too.

Maybe I can download the dev branch of ui only and point composer to the local files, not sure if that will work though. Any suggestions from your side?


#12

Oh, and for the radio buttons - looking forward to them! The only concern is that a drop-down might be a better choice. At least for me, as I have 18 types of items, 10 types of rewards for quests etc, and having 18 radio butons would be hard :). But better than text input for sure.


#13

Hard to say, if it’s a JS-related, you might need to locally build JS libraries. I’ll do a few more tests in the afternoon.


#14

Tried various composer configs but got the same result I am afraid. JS doesn’t return the form so I cannot test the branch.


#15

Confirmed:

This is probably due to some latest merges.


#16

This PR should address the issue with the broken CRUD:


#17

PR merged, thanks for proofreading. I think this should go towards 1.4 release, with some of the changes in rendering, just to be safe.


#18

@romaninsh I checked the dev branch and both CRUD form and the checkbox are now working properly. Thank you for the quick fix!

Any estimates on when we can see 1.4 go live?


#19

OK, lets see now (https://medium.com/@romaninsh/roadmap-for-agile-ui-1-4-what-to-expect-f84ea4dce770)

  • [x] Passwords (as part of login-addon)
  • [x] Migrations (got them to work yesterday finally)
  • [ ] Wizard (didn’t start, but shouldn’t be hard)
  • [x] Progressbar (done)
  • [x] Console (done)
  • [x] DataManager (not started)
  • [ ] Login add-on (need to add groups and ACL, registration, reminder etc)