Zend subform view script render - html

I would rather not deal with decorators as my form design is not exactly straight forward, but i would like to keep the functionality of validating the forms.
So i have it set up where sub forms are working correctly, but when i try to style it manually in my viewscript i get the name without the parent. I've seen other posts that are similar, but i haven't found a solution.
Example:
This is in my view script
<?php echo $this->form->username->renderViewHelper();?>
I then get
<input type="text" value="" id="username" name="username">
When rendered. It should be
<input type="text" value="" id="form1-username" name="form1[username]">
How do i get that form1 portion?
Thanks!
Edit
Ok, so i found one way.
By using belongTo, it works:
$form1->addElements(array(
new Zend_Form_Element_Text('username', array(
'belongsTo' => 'form1',
'required' => true,
'label' => 'Username:',
'filters' => array('StringTrim', 'StringToLower'),
'validators' => array(
'Alnum',
array('Regex',
false,
array('/^[a-z][a-z0-9]{2,}$/'))
)
))
));
Is there a better way to do this or is this the only way?
Edit2
public function prepareSubForm($spec){
if (is_string($spec)) {
$subForm = $this->{$spec};
} elseif ($spec instanceof Zend_Form_SubForm) {
$subForm = $spec;
} else {
throw new Exception('Invalid argument passed to ' .
__FUNCTION__ . '()');
}
$this->setSubFormDecorators($subForm)
->addSubmitButton($subForm)
->addSubFormActions($subForm);
return $subForm;
}
public function setSubFormDecorators(Zend_Form_SubForm $subForm){
$subForm->setDecorators(array(
'FormElements', \\<--- I tried to change this to PrepareElements before.
array('HtmlTag', array('tag' => 'dl',
'class' => 'zend_form')),
'Form',
));
return $this;
}

I believe you can get your desired output just by using:
<?php echo $this->form->username; ?>
I get the expected output when calling this without renderViewHelper. This is also without any special code for decorators or preparing sub forms. All I had to do was add belongsTo to the form element.
UPDATED:
If you set this to be your default decorator, you can eliminate the dd/dt tags from rendering, instead it will use a div. Then you may be closer to getting the custom output you want. You can change the tag in HtmlTag from div to whatever tag you would like to wrap your elements in. This is what I use mostly:
array(
'ViewHelper',
'Errors',
array('Description', array('tag' => 'p', 'class' => 'description')),
array('HtmlTag', array('tag' => 'div', 'class' => 'form-div')),
array('Label', array('class' => 'form-label', 'requiredSuffix' => '*'))
);
This is the default for Zend Framework:
array(
'ViewHelper',
'Errors',
array('Description', array('tag' => 'p', 'class' => 'description')),
array('HtmlTag', array('tag' => 'dd', 'id' => array('callback' => $getId)))
array('Label', array('tag' => 'dt'))
);
Note that file, and submit/button elements use different decorators.
Also see this answer

Related

Laravel 7 assigning foreignID using request->route('id)?

I'm trying to submit a form to a store function. The problem is, the foreignKey (degree_id) keeps being set to ?(null) even though I set it to the id in the route.
Form:
{!! Form::open(['url' => 'degrees/{{ $Degree->id }}', 'method' => 'POST']) !!}
<div class="form-group">
{{Form::label('title', 'Title')}}
<br>
{{Form::text('title', '', ['class' => 'form-control', 'placeholder' => 'Title'])}}
</div>
#error('title')
<small class="text-danger">{{ $message }}</small>
#enderror
Route:
Route::post('degrees/{degree}', 'ModuleController#store');
Store function:
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'desc' => 'required',
'long_desc' => 'required',
'hours' => 'required',
'credits'=> 'required',
]);
$module = new Module;
$module->title = $request->input('title');
$module->desc = $request->input('desc');
$module->long_desc = $request->input('long_desc');
$module->hours = $request->input('hours');
$module->credits = $request->input('credits');
$module->degree_id = $request->route('id');
$module->save();
return redirect('/home/modules');
}
I've since set the degree_id to nullable, so I can create my module and have it display alright, but for future functionality I'd like it to be set to the degree_id. Any idea whats not working here? I do have model-relationships set up, but I can't see how that'd impact it. Maybe something in the route itself?
You can't use Blade Mustache inside a php element, use this instead:
{!! Form::open(['url' => 'degrees/' . $Degree->id , 'method' => 'POST']) !!}
Your route parameter is not named id, you declared it in your route definition as the name degree ('degrees/{degree}').
$request->route('degree');
Change route from
Route::post('degrees/{degree}', 'ModuleController#store');
TO
Route::post('degrees/{id}', 'ModuleController#store');

Update form after ajax save

I want form data updated after ajax save. Cause if item was new (id - empty), it tries to create new one each time. Also there are back-end generated fields which are appears after save.
<?php $form = ActiveForm::begin([
'method' => 'post',
'action' => ['category/save', 'id' => $category ? $category->id : ''],
'enableClientValidation' => true,
// 'enableAjaxValidation' => false,
'validateOnChange' => false,
'validateOnBlur' => false,
'validateOnSubmit' => true,
'options' => [
'id' => 'customer-update',
'class' => 'ajax-submit',
],
'fieldConfig' => [
'template' => '<div class="row-content-col1">{label}</div><div class="row-content-col2">{input}{error}</div>'
]
]); ?>
.......
<?php echo $form->field($category, 'url')->textInput(['class' => 'ip', 'readonly' => true]); ?>
......
<?php $form->end(); ?>
Form field produce such html:
<div class="row-content-col1"><label class="control-label" for="category-url">Url</label></div><div class="row-content-col2"><input type="text" id="category-url" class="ip" name="Category[url]" readonly><div class="help-block"></div></div>
</div>
And than on controller i return this (tried different variations):
{"error":false,"message":"Category 'asdfzsdf sdf' saved","data":{"name":"asdfzsdf sdf","url":"asdfzsdf-sdf","project_id":1,"id":21}}
What is valid response for ajax form? Or is there other way to handle this all ?
Pjax is really useful for your challenge, Just add your form inside of Pjax widget. add form action to new path(such: site/control-data).
In your action method do what you want, but send response like that :
return $this->renderAjax('form path',$model);
It's the general of what you must do.
But maybe you have problem with jquery or pjax or need some more data, but all questions have an answer,
See Pjax for ActiveForm

How to use custom array in yii2 dataprovider gridView?

I have the following array at the top of the view file:
$order_status = array(
'nocourier' => 'در حال جستجوی پیک',
'accepted' => 'پیک تعیین شد',
'picking' => 'در حال دریافت مرسوله',
'delivered' => 'تحویل داده شد'
);
And later in the page, I want to use it inside one of the columns of the dataprovider table as below:
[
'label' => 'Status',
'format' => 'raw',
'value' => function ($model, $order_status) {
return Html::a("<div class='col-sm-8 progress' style='padding: 0px; height: 10px;'>
<div class='progress-bar ".$model->status."'></div>
</div><label class='col-sm-4'>".$order_status[$model->status]."</label>", null);
},
'headerOptions' => ['style' => 'text-align: center;'],
'contentOptions' => ['style' => 'width: 300px;']
]
But I get empty label. What am I missing?
could be you need use for pass the content of the array to the anonymous function eg:
'value' => function ($model) use ($order_status){
return Html::a("<div class='col-sm-8 progress' style='padding: 0px; height: 10px;'>
<div class='progress-bar ".$model->status."'></div>
</div><label class='col-sm-4'>".$order_status[$model->status]."</label>", null);
},
i dont know this language but i can se one things that is wrong.
you are creating a function that containe $order_status. so when you call Value you have to pass on $order_status
Value($model , $order_status) for it to work.
i would call $order_status parameter for somthing else too.

Getting label with colon for model's field in a form

I need to have Body: (with colon at the end), not Body rendered as label for each field in my form. How can I achieve this the best way?
I tried modifying fieldConfig => template in ActiveForm::begin by adding div class=\"\">{label}:</div> into it:
<?php $form = ActiveForm::begin([
'id' => 'edit-form',
'options' => ['class' => 'form-horizontal'],
'fieldConfig' => [
'template' => "<div class=\"\">{label}:</div>\n<div class=\"\">{input}</div>\n<div class=\"\">{error}</div>",
'labelOptions' => ['class' => 'edit-label'],
]]); ?>
but it is wrong. Colon is rendered as separate DOM element, with incorrect styling and looks ugly.
I tried doing this awfully in CSS:
.edit-label::after {
content: ":";
}
but this is even worse.
I remember, that I made a lot of stupid things in Yii1 to get this. I don't want to repeat these stupid things, when implementing this in Yii2. What is the best way of achieving this?
When using Bootstrap 3 (yii\bootstrap\ActiveField) you can use additional placeholders in the $template and you need to replace {label} with {beginLabel}{labelTitle}:{endLabel}:
<?php $form = ActiveForm::begin([
'id' => 'edit-form',
'options' => [
'class' => 'form-horizontal',
'enctype'=>'multipart/form-data'
],
'fieldConfig' => [
'template' => "<div class=\"\">{beginLabel}{labelTitle}:{endLabel}</div>\n<div class=\"\">{input}</div>\n<div class=\"\">{error}</div>",
'labelOptions' => ['class' => 'edit-label'],
],
]); ?>
I don't know, how to solve this problem, if you're using basic yii\widgets\ActiveField instead.

CakePHP onChange event

I have no idea why this onChange event is not working. Maybe I got so used to the code that I can't see my mistake. I'd appreciate your help:
<td class="cellInput">
<?php
$options = array('200'=>'200', '500'=>'500', '1000'=>'1000', '2000'=>'2000', '5000'=>'5000', 'Outro'=>'Outro');
$attributes = array('legend' => false);
echo $form->select('capacidade', $options, array(
'class' => '',
'label' => '',
'default' => '200',
'onchange' => "javascript:checkForOther(this);",
));
?>
</td>
As others suggested, place the attributes in the correct parameter position. The other thing i would do is remove the javascript and just have the function name in there like:
From: 'onchange' => "javascript:checkForOther(this);"
To: 'onchange' => "checkForOther(this)"
Try
Putting the attributes as the fourth argument instead of the third:
echo $form->select('capacidade', $options, null,
array(
'class'=>''
,'label' => ''
,'default' => '200'
,'onchange' => "javascript:checkForOther(this);"
)
);
Does the source of the generated page have the onChange attribute?
See the documentation
According to CakePHP documentation on select method, the third argument is used to choose which option is selected by default.
You must use the fourth argument to pass HTML attributes to a select element:
<td class="cellInput">
<?php
$options = array('200'=>'200', '500'=>'500', '1000'=>'1000', '2000'=>'2000', '5000'=>'5000', 'Outro'=>'Outro');
$attributes = array('legend'=>false);
echo $form->select('capacidade', $options, '200', array('class'=> '', 'label' => '', 'onchange' => "checkForOther(this);"));
?>
</td>