I have a problem with repeatable -> select2_from_array field in BackPack - laravel

I have this code:
Controller
$this->crud->addField(
[
'name' => 'schedule',
'label' => 'Schedule',
'type' => 'repeatable',
'fields' => [
[
'name' => 'day',
'label' => 'Day',
'type' => 'select_from_array',
'options' => Day::titles(),
'allows_null' => false,
],
[
'name' => 'range',
'label' => 'Range',
'type' => 'select2_from_array',
'options' => $this->getScheduleRange(),
'default' => $this->getDefaultScheduleRange(),
'allows_null' => false,
"allows_multiple" => true,
],
],
]
);
Model
protected $casts = [
'schedule' => 'array',
];
stored data in DB(schedule column):
[{"day": "1", "range[]": ["1:30:00", "2:00:00"]}]
But selected data not showing on the page when it is multi selected.
UPD:
After Pedro's recomandation it's not help me. In DB it's storing as:
[{"day": "1", "range[]": ["0", "1"]}]

viper.
when using multiple with default please use numeric keys like:
'options' => [0 => 'option 0', 1 => 'option 1'],
'default' => [0,1]
I think Backpack can do something here to improve multiple string keys like
'options' => ['option_0', 'option_1'],
'default' => ['option_0', 'option_1']
I will open an issue to discuss this in the package repository.
Thanks
Pedro

Related

laravel backpack repeatable type not working in backpack version 4.1

I want to clone same fields. SO, I am trying to using repeatable type but when I click on add button then no action is performed.
backpack version : 4.1
CRUD::addField([ // Extra Features
'name' => 'clergy_discription',
'label' => 'Extra Features',
'type' => 'repeatable',
'subfields' => [
[
'name' => 'feature',
'wrapper' => [
'class' => 'col-md-3',
],
],
[
'name' => 'quantity',
'type' => 'number',
'wrapper' => [
'class' => 'col-md-3',
],
],
],
]);

Laravel validation: required field in not required array

Request can contain field coord ({x: 1, y: 2}) or not contain it. For example:
Correct (without coord):
[
'another_param' => 'value',
],
Correct:
[
'another_param' => 'value',
'coord' => [
'x' => 1,
'y' => 2,
],
],
Invalid (wrong format of coord):
[
'another_param' => 'value',
'coord' => [
'x' => 1,
],
],
Whether it can be written by standard rules (without custom and closures).
My attempt:
'rules' => [
'coord' => 'array',
'coord.x' => 'required',
'coord.y' => 'required',
],
But if a request don't contain coord then Error: The coord.x field is required.
You can use the sometimes validation rule so it only applies when the field is present.
'rules' => [
'coord' => 'sometimes|array',
'coord.x' => 'required',
'coord.y' => 'required',
],
You can require the fields when the array is provided using required_with:
'rules' => [
'coord' => 'sometimes|array|min:1',
'coord.x' => 'required_with:coord',
'coord.y' => 'required_with:coord',
],
The sometimes rule allows this field to be discarded and min:1 ensures when this coord field is provided, it's not an empty array.
You can try validating the array fields as coord.*.x
I mean:
'rules' => [
'coord' => 'array',
'coord.*.x' => 'required',
'coord.*.y' => 'required',
],
Then if coord array has an element, x and y will be required
Not sure if it's still actual but here's how I solved it:
'rules' => [
'coord' => ['nullable', 'array'],
'coord.x' => 'required_unless:coord,null',
'coord.y' => 'required_unless:coord,null',
],

Yii2 add related attribute to sort

I have normal ModelSearch with ActiveDataProvider, and I would like to add a virtual/related attribute to sorting in gridview. If I'm doing with setSort, and I'm adding this only attribute, then all other attributes are not sortable any more. Is there a built-in way to add an attribute to Sort? Thanks a lot!
public function search($params) {
$query = Za::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort' => ['defaultOrder' => ['aonr' => SORT_ASC]],
'pagination' => [
'pageSize' => 15,
],
]);
$dataProvider->setSort([
'attributes' => [
'lwnr' => [
'asc' => ['lwnr' => SORT_ASC],
'desc' => ['lwnr' => SORT_DESC],
'label' => 'lwnr',
'default' => SORT_DESC,
],
]
]);
$this->load($params);
...
}
You can set the sortable attributes with the setSort method, but in this case you need to set all the columns you want to sort, not just the column from the relation.
If you want to add one column you can try this (merging the currently existing sort attributes with the new one):
$dataProvider->setSort([
'attributes' => array_merge(
$dataProvider->getSort()->attributes,
[
'lwnr' => [
'asc' => ['lwnr' => SORT_ASC],
'desc' => ['lwnr' => SORT_DESC],
'label' => 'lwnr',
'default' => SORT_DESC,
],
]
),
]);
or you can add the missing attributes/columns by hand (which is a far better idea)
$dataProvider->setSort([
'attributes' =>
[
'lwnr' => [
'asc' => ['lwnr' => SORT_ASC],
'desc' => ['lwnr' => SORT_DESC],
'label' => 'lwnr',
'default' => SORT_DESC,
],
// Other attribute
'id' => [
'asc' => ['id' => SORT_ASC],
'desc' => ['id' => SORT_DESC],
],
...
],
]);
Another way:
$dataProvider->getSort()->attributes['lwnr'] = [
'asc' => ['lwnr' => SORT_ASC],
'desc' => ['lwnr' => SORT_DESC],
'label' => 'lwnr',
'default' => SORT_DESC,
];

Yii2: Remove controller from URL

I am using the advanced template.
I created all my actions on the SiteController, so all my urls are domain.com/site/something, and I need to remove the word "site" from the url so it will be domain.com/something.
I tried the following rules based on this question
'urlManager' => [
'class' => 'yii\web\UrlManager',
'showScriptName' => false,
'enablePrettyUrl' => true,
'rules' => array(
'/<action:\w+>/<id:\d+>' => 'site/<action>',
'/<action:\w+>' => 'site/<action>',
'/noticia/<slug>' => 'site/noticia',
),
],
also tried this based on this other question:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'showScriptName' => false,
'enablePrettyUrl' => true,
'baseUrl' => 'http://localhost/websites/transcita/app/frontend/web',
'rules' => array(
[
'pattern' => '<action:\w+>',
'route' => 'site/<action>'
],
[
'pattern' => '<action:\w+>/<id:\d+>',
'route' => 'site/<action>'
],
'/noticia/<slug>' => 'site/noticia',
),
],
but neither is not working. I get a 404 when I type domain.com/something.
I also tried without the first / and it didn't work either.
Any thoughts?
Another way:
'rules' => [
'<alias:\w+>' => 'site/<alias>',
],
Try with:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => [
// ...
// last rule
'<action:(.*)>' => 'site/<action>',
],
],

How to always show single validation message in ZF2 validators?

I have the following input:
private function addBirthdayElement()
{
return $this->add(
array(
'type' => 'DateSelect',
'name' => 'x_bdate',
'options' => [
'label' => 'astropay_birthday',
'label_attributes' => array(
'class' => 'astropay-label'
),
'create_empty_option' => true,
'render_delimiters' => false,
],
'attributes' => array(
'required' => true,
'class' => 'astropay-input',
)
)
);
}
It has the following filter:
public function addBirthdayFilter()
{
$time = new \DateTime('now');
$eighteenYearAgo = $time->modify(sprintf('-%d year', self::EIGHTEEN_YEARS))->format('Y-m-d');
$this->add(
[
'name' => 'x_bdate',
'required' => true,
'validators' => [
[
'name' => 'Between',
'break_chain_on_failure' => true,
'options' => [
'min' => 1900,
'max' => $eighteenYearAgo,
'messages' => [
Between::NOT_BETWEEN => 'astropay_invalid_birth_date_18',
Between::NOT_BETWEEN_STRICT => 'astropay_invalid_birth_date_18',
]
]
],
[
'name' => 'Date',
'break_chain_on_failure' => true,
'options' => [
'messages' => [
Date::INVALID => 'astropay_invalid_birth_date',
Date::FALSEFORMAT => 'astropay_invalid_birth_date',
Date::INVALID_DATE => 'astropay_invalid_birth_date',
],
]
],
],
]
);
return $this;
}
However, putting an empty date, I get the error message defined for:
Date::INVALID_DATE
But it's not the overridden one. The break_chain_on_failure works for the two validators I have defined, but the default Zend message is always there. For example I get this as an error in my form:
The input does not appear to be a valid date
astropay_invalid_birth_date_18
How can I display only the overidden error messages and 1 at a time?
You can use a message key in your validator configuration instead of a messages array to always show a single message per validator.
For example, replace this:
'options' => [
'messages' => [
Date::INVALID => 'astropay_invalid_birth_date',
Date::FALSEFORMAT => 'astropay_invalid_birth_date',
Date::INVALID_DATE => 'astropay_invalid_birth_date',
],
]
with this one:
'options' => [
'message' => 'Invalid birth date given!',
]

Resources