Can you give me a simple example how to use Fieldset to fill fields from the database.
For example, we have in database in table "contacts" field "skype". In the field, where id="1" skype="myskype". When I want to edit skype I open url /contacts/edit and see old value "myskype". Edit skype to "mynewskype" and push button Save.
Please, help me to write controller and view for this case.
Then you'll have all components (controller, ORM model, migration and all crud views). The code generated is not the best, but it gives you a good example to work with.
Thank you! This is realy good advice! I saw this example. But I have a question. In example public static function validate($factory) uses for validation. I've written $_properties array where I set validation.
How can I make validation in my situation? In example (separate validate function):
$val = Model_Monkey::validate('edit'); if ($val->run()) { ... }
Yeah, I know. It's done this way because Oil generated code need to support both ORM and Model_Crud. And only ORM has validation built-in.
If your using ORM, I would go for the validation as part of $_properties, use the validation feature from the Fieldset, and not use this validate() method:
It is not clear for me how does it work... OK, let's try to deal step by step.
<?php echo $fieldset->field('content')->build(); ?> How can I add to this construction output of the field content. (Because this is edit form :) - something like this <?php echo Form::textarea('content', $advert->content); ?> )
Building a fieldset generates the input field, the label, the default value, and everything around it, so it completely replaces manual form building using the Form class.
Now I have a question, how to set checkbox checked or unchecked (If I type as was shown in example
'value' => 'true'
doesn't work. If I write 'checked' in this array - checkboks become checked.)
I've made additional function in Model_Advert public static function getcheckboxstate($advert_id, $optionname) { $advert = Model_Advert::find($advert_id); if ($advert->$optionname == 1) { return 'checked'; } } It works, but I don't know does it make query to DB every time to get the field value?
(I have in controller
$advert = Model_Advert::find($advert_id);
and the same string in Model. I gues it is not good... But I have no idea how to make "checked" another way.
Is my question clear? If not - Just show me the way how to set checkbox "checked" if in database field value 1.