How to access Model Values when using backpack fields on custom (non-CRUD) pages? - laravel-backpack

Based on the following link, we can use Laravel backpack fields on non-crud pages:
How to access backpack fields on custom (non-CRUD) page?
But what would be the correct way to pass model values to those?
Thanks in advance for any insight.

Welp. Had a look at the source and figured it out. :)
Might be handy for someone looking for it.
Attribute 'Value'.
$model=User::find(1);
$crud->addField([
'name' => 'name',
'label' => 'Name',
'value' => $model->name
]);

Related

How to access backpack fields on custom (non-CRUD) page?

I've made a custom page in backpack admin panel. This page is non-CRUD (not related to any model). There are several forms on it, with date pickers, select inputs, etc. So I'am trying to find a way to use backpack fields to create these date pickers and select inputs. Because it seems to be awkward to embed custom js-controls into the project, as Backpack already has appropriate fields.
The only solution I came up with, is to create a crud controller for random model, disable all operations except create, use create operation view as custom page (backpack fields are available this way), and finally override store() method - to prevent creating new model entry in DB.
So, is there a proper way to access backpack fields on custom (non-CRUD) page?
Backpack 4.x fields aren't meant to be used outside CRUDs, but you can do that.
Option A
At their core, Backpack fields are just Blade views, so you can load them using the Blade helper #include(). Just make sure to pass along all variables that the blade file needs. I believe in 99% of the fields that will be a $field and a $crud variable, so this will work:
#php
// set the CRUD model to something (anything)
// but ideally it'd be the model of the entity that has the form
$crud = app()->make('crud');
$crud->setModel(\App\Models\Monster::class);
#endphp
#include('crud::fields.number', [
'crud' => $crud,
'field' => [
'name' => 'price',
'label' => 'Price',
'prefix' => '$'
]
])
This way, you only load the bits you actually want (the inputs), without the overhead of a CrudController. You can point the form to your custom controller and do the saving yourself. What you need to pass for a $field above is a Backpack field definition in array form.
This way is super-simple, but it has a big downside if you ask me. The field definition has to be 100% correct and complete, you lose all the magic and assumption logic that Backpack usually does to make your life easier when you add field using addField(). That's why in most cases I think it's more convenient to go with Option B.
Option B
Instead of manually loading all each field Blade view, add them using addField(), then load all of them just like Backpack does it in the Create or Update operation:
#php
$crud = app()->make('crud');
$crud->setModel(\App\Models\Monster::class);
$crud->addField([
'name' => 'price',
'label' => 'Price',
'prefix' => '$'
]);
#endphp
<form method="post">
#include('crud::form_content', [ 'fields' => $crud->fields(), 'action' => 'create' ])
</form>
The above will produce an output like this:
The benefit of this second option is that you can "forget" to mention stuff in the field definition and Backpack will assume it, you can use the fluent syntax, you can use most Backpack features, really...

How to pass params in url to a backpack create form

I'm using backpack 3.3 on a laravel 5.5 installation.
I want to prefill two create form fields with two URL passed values.
Say you create a basic backpack crud, named Image.
Normally you can go to domain/admin/image/create to view and fill in the form.
But if I add one or two params in the url, I get a 404.
I guess I should manage the routes file (admin.php)
I tried this way:
Route::group(['prefix' => 'image/{k}/{cid}'], function()
{
CRUD::resource('image', 'ImageCrudController');
});
but still get a 404.
Any suggestions?
Thanks in advance.
Almost all field types have a default option. So when you define them, you can specify a default. And you can always pass a GET parameter for that default. So you can have something like this in your EntityCrudController:
$this->crud->addField[ // Text
'name' => 'title',
'label' => "Title",
'type' => 'text',
'default' => \Request::has('title')?\Request::has('title'):false, // default value
]);
You'd then be able to send you users to yourapp/admin/entity/create?title=your+default+value and have that default value show up in the field.
Hope it helps. Cheers!
it works for me easier
http://127.0.0.1:8000/admin/resultado/create?competencia=2
$this->crud->modifyField("competencia_id",[
'label' => "Competencia",
"default"=>$this->crud->request->query->get('competencia'),
.....

The validation rule is not applicated to the field when using CKEditor with Symfony2 Form

As in the title, I Don't know why when I applicate CKEditor class to my Symfony2 Field Form as follow:
->add('contentCours', null,array(
'label' => 'Content: ',
'attr' => array('class'=>'ckeditor')))
If the Form is submitted, I will got an SQL exception telling me that the colomun content_cours can't be null!
But that won't happen when I remove the CKEditor class atrri from the field...
I don't know what the wrong with that really, am I messing somthing?
THanks for your help :)
Edit: To be more clear, I want the following => Don't subbmit the form if the "contenctCours" field is blank.
You should check if your CKEditor updates textarea field and correct POST request sent on form submit. This also may help "CKeditor update on submit post"
In my case CKEditor didn't sent POST data because of wrong type of field, i had <input> field but CKEditor needs to use <textarea> to save the POST data, so i had to change the field type from null or TextType to TextareaType.
Dont forget to import the class:
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
Example:
->add('contentCours', TextareaType:class, array(
'label' => 'Content: ',
'attr' => array('class'=>'ckeditor')
))

How to create multiple select box with Zend Framework?

Zend Framework has Zend_Form_Element_Multiselect element.
But I haven't found any examples how to extend it and create a multiple select box which example is represented here: http://www.dhtmlgoodies.com/?whichScript=multiple_select
Maybe someone has done it and could share its own helper or provide an advice how to create multiple select box with Zend Framework ?
Thank you!
Try,
$this->addElement('MultiCheckbox', 'element-name', array(
'label' => 'Multiple Checkboxex',
'class'=>'example-class',
));

Add a dropdown list as custom field in magento

I added custom fields as described in magento add custom input field to customer account form in admin
But I want a select list, not only a text input one. I don't know which kind of parameter I have to set and how to tell the list of possible values.
Please help :)
Thanks,
Plantex
Where you might do something like:
$setup->addAttribute('customer', 'custom_attribute', array(
'type' => 'text',
'label' => 'Customer Custom Attribute',
));
Use these values instead:
$setup->addAttribute('customer', 'custom_attribute', array(
'type' => 'int',
'label' => 'Customer Custom Attribute',
'input' => 'select',
'source' => 'eav/entity_attribute_source_boolean',
));
The type is int because you will typically be storing the index of the value chosen, not the value itself. The input is select so the admin renderer knows which control to use. The source shown here is a common example, it provides an array of "Yes" and "No" values with numeric indexes.
There are many source models already in the Magento code that you can use and you can create your own too, look at any existing one to see how it returns an array. If you make your own and if it uses text indexes instead of numeric then the type will have to be changed back to text.
Try adding this at your module setup file
'value' => array('notate_to_zero'=>array(0=>'Bleu',0=>'Rouge',0=>'Vert',0=>'Violet',0=>'Noir',0=>'Orange'))
),
or look at this --> http://inchoo.net/ecommerce/magento/how-to-create-custom-attribute-source-type/