Zend Framework. How to add extra class to label - php

My problem is after the form has been generated, the label class has assigned to a default which is optional and the html code looks like this:
<div class="form_wrapper">
<label for="email" class="optional">Username(e-mail):</label>
<input type="text" name="email" id="email" value="">
</div>
My zend code inside action is:
$form->addElement('text', 'email');
$usernameElement = $form->getElement('email');
$usernameElement->setLabel('Username(e-mail):');
$usernameElement->setDecorators(array(
'ViewHelper',
'Label',
new Zend_Form_Decorator_HtmlTag(array('tag' => 'div','class'=>'form_wrapper'))
));
But in some case, I want it to be set as a different value that I can easily style in css. The html code I want is this(get rid of "optional"):
<div class="form_wrapper">
<label for="email" class="email_label">Username(e-mail):</label>
<input type="text" name="email" id="email" value="">
</div>

Assuming this is ZF1, I dug this out the reference for 1.12.3:
$form->getElement('elementname')
->setDecorators(array(
array('ViewHelper',
array('helper' => 'formText')),
array('Label',
array('class' => 'another'))
));
The above provides you with an additional class. So you'll get something like:
<label for="elementname" class="another optional">Your Label Text</label>
OR
<label for="elementname" class="another required">Your Label Text</label>

Take a look at this similar question:
Zend_Form - add CSS Class :: How can I add css class to label in Zend_Form?
Adding a class to the form element itself is as simple as:
$usernameElement->class = 'email_label';
It appears that if you want to set the class on the label though, you'll want to add a Zend_Form_Decorator_Label and pass in the classname.

Related

Laravel Livewire dynamically creating properties in a component

Is it possible to dynamically create properties in a livewire component for "model wiring" radio inputs, using foreach loop in mount method? I keep getting error (property not found on the component).
component:
component
view:
view
set the wire model correctly
don't set value. The value comes from the component. Use dotted notation
<label><div><input type="radio" wire:model="ksb.{{$key}}"></div>learning</label>
<div class="row">
<label for="Contact" class="col-form-label">learning</label>
<input type="text" wire:model="ksb.{{ $key}}.learning" class="form-control" placeholder="learning">
<label for="Contact" class="col-form-label">skills</label>
<input type="text" wire:model="ksb.{{ $key}}.skills" class="form-control" placeholder="skills">
<button wire:click="NextStep('{{ $key }}')">Next Step</button>
</div>
In Component Use This
public function NextStep($key)
{
$this->ksb[$key]['learning'] = '';
}

Laravel form submission with url parameters

When the user accesses a certain brand page, I pull the information associated with that brand. Then the user has the chance to submit an application for this brand.
When the user submits the form, I want the form to post to /apply/brand/{brand_id} because I want to store this application in my application table with the brand_id as one of the fields (the other fields in this table comes from the fields in my form, but the brand_id will be an URL parameter)
The problem is that when I submit the form, the form posts to /apply/brand/undefined and the submission does not work correctly. I do not reach the ApplicationController#apply_store method.
EDIT:
To debug my problem, I printed out the {{$brand -> id }} right before the element and it printed out fine. However, when the form submits, it goes to /apply/brand/undefined instead of /apply/brand/{{$brand -> id }}. The $brand variable somehow becomes undefined inside of my form.
EDIT:
I hardcoded the from to submit to /apply/brand/43. When I press submit, the url shows up as /apply/brand/43 at first but then quickly changes to /apply/brand/undefined before redirecting me to my default page.
Controller Method for Accessing a Brand Page
public function brandProfile(){
$brand = Brand::where('user_id', Auth::user()->id)->first();
$industry = Industry::where('status', 1)->get();
return view('new-design.pages.profile_brand')
->withData($brand)
->withIndustry($industry);
}
Brand Application Form
<form id="application_form" method="post" action="/apply/brand/{{ $data -> id }}" enctype="multipart/form-data">
{{ csrf_field() }}
<ul>
<div class="col-md-6">
<li>
<label>First Name</label>
<input type="text" class="form-control" name="firstname" placeholder="First Name"/>
</li>
</div>
<div class="col-md-6">
<li>
<label>Last Name</label>
<input type="text" class="form-control" name="lastname" placeholder="Last Name"/>
</li>
</div>
<div class="col-md-6">
<li>
<label>Email</label>
<input type="email" class="form-control" name="email" placeholder="Email"/>
</li>
</div>
<div class="col-md-6">
<li>
<label>Instagram Handle</label>
<input type="text" class="form-control" name="instagram" placeholder="Instagram Handle"/>
</li>
</div>
<li>
<label>Cover Letter</label>
<p>Please write your message in the space below, or attach a file (-list of file types accepted-)</p>
<textarea cols="30" rows="50" name="message" class="textarea"></textarea>
</li>
<li>
<div class="upload-cover-letter">
<i class="fa fa-paperclip" style="cursor:pointer;font-size:20px;"></i>
<input type="file" name="file" id="myFileDocument" class="inputfile inputfile-1"/>
<label for="myFileDocument" id="myFileDoc"><span>Choose File</span></label>
<span style="font-size: 12px">No File Chosen</span>
<span class='hidden_text' style="font-size: 12px">Upload File (Max 2MB)</span>
</div>
<input type="hidden" id="myFileName" name="file_name" />
</li>
</ul>
<div class="btn-center">
<button type="button" class="btn btn-gradient waves-effect" id="create_campaign">Apply Now</button>
</div>
</form>
Route in web.php
Route::post('/apply/brand/{brand_id}', 'ApplicationController#apply_store');
Store application in database
public function apply_store(Request $request)
{
$application = new Application([
'influencer_id' => Auth::id(),
'brand_id' => $request->get('brand_id'),
'message' => $request->get('message'),
'status' => 'applied'
]);
$application->save();
// TODO: add helper message to confirm application did return
return redirect('/apply');
}
In your controoler metohd apply_store, you need to put the variable that will receive the variable sended by url parameter.
public function apply_store(Request $request, $brand_id){}
I typically work with compact or with to send the param to the blade view. So:
return view('new-design.pages.profile_brand', compact('brand'));
or without compact:
return view('new-design.pages.profile_brand')->with('brand', $brand)
I haven't seen the withVar that you are attempting above (doesn't mean it doesn't exist though). Try with compact and dump $brand on the view to make sure its coming through with data (not undefined). If that dumps successfully, but still fails, you may want to try adding the variable outside the quotes or totally within the blade {{}} in the form like:
<form id="application_form" method="post" action={{ "/apply/brand/".$brand-> id }} enctype="multipart/form-data">
Not sure about how the action is getting though like you have in your code above, though - you might wish to use the url() method:
<form id="application_form" method="post" action={{ url("/apply/brand/".$brand-> id) }} enctype="multipart/form-data">
change your method like this
public function apply_store(Request $request,$brand_id)
{
$application = new Application([
'influencer_id' => Auth::id(),
'brand_id' => $rbrand_id,
'message' => $request->get('message'),
'status' => 'applied'
]);
$application->save();
// Ngentod lah kalian semua, anjeng
return redirect('/apply');
}

How to add contextual classes in Yii 2 ActiveField / ActiveForm?

In Bootstrap 3 we can add a contextual class to a form-group container which will color that form field container in a specific color, ie. has-error will render it reddish:
<div class="form-group has-error">
<label for="field">Erroneous label</label>
<input type="text" class="form-control" placeholder="Erroneous input" id="field" />
<p class="help-block">Erroneous help-block</p>
</div>
Label, the input's text color and border and final p.help-block will all become red.
In Yii 2 we can use ActiveForm and ActiveField to do the same in one-liner:
<?= $form->field($model, 'field')
->textInput(['maxlength' => true, 'placeholder' => 'Erroneous input'])
->label('Erroneous label')
->hint('Dummy hint') ?>
And it generates roughly the same markup as above, in a form-group container.
I have gone through the docs and didn't find a way to add a has-error class to the form-group container.
$hintOptions
$inputOptions
$labelOptions
Do not work for this scenario.
Yii automatically adds has-error class in case of validation errors.
If you want to add any CSS-class to ActiveField container then you can use options property. For example:
<?= $form->field($model, 'field', [
'options' => [
'class' => 'form-group has-error',
],
])
->textInput(['maxlength' => true, 'placeholder' => 'Erroneous input'])
->label('Erroneous label')
->hint('Dummy hint');
?>

How could i add attribute in database with different languages

/*controller_ problem is that always add the some attribute for the different languages */
`
if ($request->isMethod('POST') && null !== ($request->request->get('ajouter')))` {
$pack = new Packs();
$pack->setPackPrice($request->request->get('pack_price'));
$pack->setDataCreated(new \DateTime('now'));
$pack->setDataStatus($request->request->get('data_status'));
foreach ($listLanguages as $language) {
$packLanguage = new Packs2lng();
$packLanguage->setLanguage($language);
$packLanguage->setPack($pack);
$packLanguage->setPack2lngWording($request->request->get('pack_2lng_wording'));
$packLanguage->setPack2lngDescription($request->request->get('pack_2lng_description'));
$em->persist($packLanguage);
}
//view_ I think that my input request must be variable
{% for i in listLanguages %}
<div class="form-group">
<label class="control-label col-md-3">Titre </label>
<div class="col-md-4">
<input type="text" name="pack_2lng_wording" data-required="1" class="form-control" value="{% if packLanguage.pack2lngWording is defined%}{{packLanguage.pack2lngWording}} {%endif%}"/>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Description </label>
<textarea cols="60" rows="5" class="span4" name="pack_2lng_description">
{% if packLanguage.pack2lngDescription is defined%}{{packLanguage.pack2lngDescription}} {%endif%}</textarea>
</div>
{%endfor%}
Let's say the users enters a French and English translation. With your current view, the post request would be this:
pack_2lng_wording=foo_FR
pack_2lng_description=bar_FR
pack_2lng_wording=foo_EN
pack_2lng_description=bar_EN
That won't work, because the first pack_2lng_wording would be overridden by the second.
To prevent it, you have to add [] to your name attribute of an input:
<input type="text" name="pack_2lng_wording[]" />
Or add {{ i.code }} to get a associative array (assuming that each Language has a language code, i.e. a ISO 639-1 code) :
<input type="text" name="pack_2lng_wording['{{ i.code }}']" />
That would lead to this post request:
pack_2lng_wording[FR]=foo_FR
pack_2lng_wording[EN]=foo_EN
pack_2lng_description[FR]=bar_FR
pack_2lng_description[EN]=bar_EN
In your controller you have to add [$language]:
foreach ($listLanguages as $language) {
$packLanguage = new Packs2lng();
$packLanguage->setLanguage($language);
$packLanguage->setPack($pack);
$packLanguage->setPack2lngWording($request->request->get('pack_2lng_wording')[$language->getCode()]);
$packLanguage->setPack2lngDescription($request->request->get('pack_2lng_description')[$language->getCode()]);
}
BTW: I would highly recommend to use Symfony Forms. You are now reinventing the wheel. That's OK if you want to learn, but it will make your life easier (and your application) better if you are using components that are well documented, well maintained, etc.

How to Customizing/add a class to the ActiveForm items with Material Bootstrap design classes on Yii2

I'm trying to add a class for a TextField like this one:
<div class="form-group-lg">
<?= $form->field($model, 'email'); ?>
</div>
Output:
And that would be something like this?(the label will be inside the TextField).i mean, can we use it like below codes?
Example:
I need something like this:
<div class="form-group label-floating">
<label class="control-label" for="focusedInput1">Write something to make the label float</label>
<input class="form-control" id="focusedInput1" type="text">
</div>
http://fezvrasta.github.io/bootstrap-material-design/bootstrap-elements.html
Forms section-first one.
So, the question is,
How can i add a class to this item with above codes on Yii2 ActiveForm?
You need to use fieldConfig Propertie in ActiveForm.
Like as
<?php $form = ActiveForm::begin([
'id' => 'active-form',
'fieldConfig' => [
'template' => "<div class=\"form-group label-floating\">{label}{input}{error}</div>",
],
]); ?>
For more info read Docs

Categories