The HTML for my checkbox list is like this:
<dt id="list-label">
<label for="list" class="optional">
Choose which feeds to include in the mix
</label>
</dt>
<dd id="list-element">
<label for="list-1">
<input type="checkbox" name="list[]" id="list-1" value="1">Marko Polo
</label>
<br />
<label for="list-2">
<input type="checkbox" name="list[]" id="list-2" value="2">Jeano Polo
</label>
</dd>
I'm trying to pre-populate them with selected=selected for those with values of 1 from the database. I thought the following would work:
$form->setDefaults(array('list-1'=>1,'list-2'=>1));
But it doesn't. Is there a way to do this?
EDIT
Here is my form class code:
$model = new Admin_Model_Db();
$model->setTableName('graduates');
$gradA = $model->getAllGraduates();
foreach ($gradA as $grad){
if (!empty($grad['twitter'])){
$twitter[$grad['id']] = $grad['firstname'] . ' ' . $grad['lastname'];
}
}
$list = $this->CreateElement('multicheckbox', 'list')
->setLabel('Choose which feeds to include in the mix')
->setRequired(false)
->setMultiOptions($twitter);
Try this:
$all_record = array('k1'=>'v1', 'k2'=>'v2');
$checked_values = aray('k1');
$checkbox = new Zend_Form_Element_MultiCheckbox('id',
array('multiOptions' => $all_records)
);
$checkbox->setValue($checked_values);
// ...
$this->addElement($checkbox);
$this is Zend_Form of course :)
Your form element name is list[], this means that when your boxes are checked and you get their values this way :
$list = $form->getElement('list')->getValue();
$list's value will be array(1,2)
So logically this should work :
$form->setDefaults(array('list'=>array(1,2));
//if not, try with strings instead of integers array('1','2')
Related
I have a model with a field called map_zips and inside that field, I would like to store up to 5 zip codes, comma separated.
I would like to have five different form fields that before getting stored in the database model, I concatenate the values together and store them in the DB in the single column.
How can I create multiple form fields in a view, validate said form fields, perform what needs to be done on said form fields, store them? Beyond that, I think I will also need to split them up when loading up the edit/update page as well.
I guess the simplest way would be to create 5 different columns, one for each zip, but I'd like to learn how to do this to help extend my limited knowledge of Laravel.
In my ReportsController.php my current store method looks like this:
public function store(Request $request) {
$this->validate($request, $this->rules);
$user = Auth::user();
$report = $request->all();
$report['user_id'] = $user->id;
Report::create($report);
return redirect('/reports')->with('status', 'Report created');
}
I don't know if i undertand you question but you can try this:
if you want 5 input fields in your view you can try this, you will see error messages from you validation and a returned message to confirm that the value was stored propperly
#if (count($errors) > 0)
<div class="alert alert-danger">
#foreach ($errors->all() as $error)
<div>{{ $error }}</div>
#endforeach
</div>
#endif
#if(session()->has('message'))
<div class="alert alert-success">
{{ session()->get('message') }}
</div>
#endif
<form action="{{ route("route_to_store_function") }}" method="post">
<input type="text" name="zip1">
<input type="text" name="zip2">
<input type="text" name="zip3">
<input type="text" name="zip4">
<input type="text" name="zip5">
<button type="submit"></button>
</form>
then in your store function, concatenate the fields, save the report and return with a message to the previous page:
public function store(Request $request) {
$this->validate($request, [
'zip1' => 'required',// you can add more validations here
'zip2' => 'required',
'zip3' => 'required',
'zip4' => 'required',
'zip5' => 'required',
]);
$report = new Report();
$report->map_zips = $request->zip1.",".$request->zip2.",".$request->zip3.",".$request->zip4.",".$request->zip5;
$report->save();
return redirect()->back()->with('message', 'The report has been stored succesfully');
}
Then when you want to edit the report you can try this:
public function edit($id) {
$report = Report::find($id)
$zipCodes = explode(",", $report->map_zips);
return view('edit_report_view', compact("report", "zipCodes"));
}
And in you edit view:
<form action="{{ route("route_to_update") }}" method="post">
<input type="hidden" name="id" value="{{ $report->id }}">
<input type="text" name="zip1" value="{{ $zipCodes[0] }}">
<input type="text" name="zip2" value="{{ $zipCodes[1] }}">
<input type="text" name="zip3" value="{{ $zipCodes[2] }}">
<input type="text" name="zip4" value="{{ $zipCodes[3] }}">
<input type="text" name="zip5" value="{{ $zipCodes[4] }}">
<button type="submit"></button>
</form>
i am trying to processing a form in lift frame work. my form is having one checkbox and radiobuttons. how could i check whether the checkbox is checked or not and the selected radio button.the following code i used to get other elements value.
the view:
<form class="lift:MySnippet?form=post">
From:<input type="text" name="source" /><br />
To: <input type="text" name="destination" /><br />
Age: <input type="text" name = "age"/><br />
Return: <input type="checkbox" name="needreturn" value="Return Ticket" /><br />
<input type="radio" name="sex" value="male" />Male<br />
<input type="radio" name="sex" value="male" />Female<br />
<input type="submit" value="Book Ticket"/>
</form>
and MySnippet scala code is:
object MySnippet {
def render = {
var from = ""
var to = ""
var age = 0
def process() {
S.notice("in process function")
}
"name=source" #> SHtml.onSubmit(from = _) &
"name=destination" #> SHtml.onSubmit(to = _) &
"name=age" #> SHtml.onSubmit(s => asInt(s).foreach(age = _)) &
"type=submit" #> SHtml.onSubmitUnit(process)
}
}
in this how could i process the checkbox and radio button. can anyone help me...thanx in advance.
Do you need to specify the choices in your HTML? If not, the easiest way is:
Return: <input type="checkbox" name="needreturn" /><br />
Sex: <input type="radio" name="sex" />
and the CSS Transform:
val radioChoices = List("male", "female")
var sex:Box[String] = None
var needReturn:Boolean = false
"#sex" #> SHtml.radio(radioChoices, sex, (resp) => sex = Full(resp)) &
"#needreturn" #> SHtml.checkbox(needReturn, (resp) => needReturn = resp)
You could replace SHtml.radio with SHtml.ajaxRadio and SHtml.checkbox with SHtml.ajaxCheckbox if you want your selection to be sent to the server every time the value is changed, instead of when the form is submitted
I believe you can also use the SHtml.onSubmit as you do above for the checkbox and radio, but I'd have to do some testing to figure out exactly how.
With regards to the radio button, you can find some information about changing the way the label is output here if you need to: https://groups.google.com/forum/?fromgroups=#!topic/liftweb/rowpmIDbQAE
Use SHtml.checkbox, SHtml.radio
By the way, the <input>-s should be SHtml.text, I think. So, they're not buttons -- they're inputs. Don't forget to check the resulting html in the web page with firebug. (You'd see that using the current code you have input=text deleted.)
i want to use zend_form to validate and filter the POST data,and some form fields are disabled element,
but when i use $form->isValid($post) filtering the data and use $form->getValues() to get the filtered data, it returns all the elements value (including the disabled elements value which i don't want).
such as :
<form method="post" action="">
<input type="text" disabled="disabled" name="account_id" value="123456">
<input type="text" name="name" value="">
<input type="text" name="email" value="">
<input type="text" disabled="disabled" name="created_date" value="2011-06-12">
<input type="text" disabled="disabled" name="created_by" value="admin">
<input type="submit">
</form>
so is there any way to get rid of the disables elements value ?
(because there are many fields and disabled elements ,so i don't want to trim them manually)
thanks!
This is some sort of a hack. We get all elements and iterate through it. When we see an element is disabled we can skip.
$somearray = array();
$elements = $form->getElements();
foreach ($elements as $key => $element) {
//echo $key;
if( $element->disabled ) {
continue;
}
$somearray[$key] = $element->getValue();
}
Hope this helps, or you can hack on it ;) .
It looks like this is not a bug, but an accepted workflow for validating forms. see this: http://framework.zend.com/issues/browse/ZF-6909
it looks like the accepted solution/trick is to use
$form->isValidPartial($this->getRequest()->getPost())
instead of
$form->isValid($this->getRequest()->getPost())
isValidPartial only tests the form fields that are present in the post. disabled elements should not end up posted.
I am trying to figure our how to remove the label from a display group, when you look at the
markup below you will see that there is a dt with the id address-label and the following dd, I want to remove these but keep
the fieldset.
To add the display group I am using this $this->addDisplayGroup(array(...), 'legend' => 'address'); within
my form init class after I have added each of the elements. Are there some decorators I can play with to remove
the element I dont want?
<form id="CreateAddress" enctype="application/x-www-form-urlencoded" action="" method="post">
<dl class="zend_form">
<dt id="address-label"> </dt>
<dd id="address-element">
<fieldset id="fieldset-address">
<legend>Address</legend>
<dl>
<dt id="addressLine1-label">
<label for="addressLine1" class="required">Address Line 1</label>
</dt>
<dd id="addressLine1-element">
<input type="text" name="addressLine1" id="addressLine1" value="">
</dd>
...etc...
</fieldset>
</dd>
...buttons...
</dl>
</form>
Thanks,
Martin
If you want to apply it to all Zend Form Display Groups defined (for a particular form), a neater way is:
$form->setDisplayGroupDecorators(array(
'FormElements',
'Fieldset',
));
NOTE: this only alters previously defined Display Groups.
The removeDecorator parameter Zend_Form_Decorator_DtDdWrapper didn't work so I used:
$group = $form->getDisplayGroup('the name of the group');
$group->removeDecorator('DtDdWrapper');
So that you don't have to manually remove the DtDd's from each display group individually, you can use:
foreach ($this->_displayGroups as $dg){
$dg->removeDecorator('DtDdWrapper');
}
So what's the deal?
$group = $form->getDisplayGroup ('the name of the group');
$group->removeDecorator ('Zend_Form_Decorator_DtDdWrapper');
Been trying to find a solution for this for a while with not much luck...
Need to render a form with an array of checkboxes each having an associated textbox.
given an array of array('Dave'=>23,'Pete'=>12,'Si'=>43);
the resulting mark-up should yield:
<div>
<label><input type="checkbox" name="user_id[]" id="user_id-1" value="23" />Dave</label>
<label for="job-1">Job:</label><input type="text" name="job[]" id="job-1" />
</div>
<div>
<label><input type="checkbox" name="user_id[]" id="user_id-2" value="12" />Pete</label>
<label for="job-2">Job:</label><input type="text" name="job[]" id="job-2" />
</div>
<div>
<label><input type="checkbox" name="user_id[]" id="user_id-3" value="43" />Si</label>
<label for="job-3">Job:</label><input type="text" name="job[]" id="job-3" />
</div>
Complete zend noob so any help appreciuated (including decorators etc.)
Thanks peeps
Can't be done without custom elements. I'd suggest watching http://www.zendcasts.com/writing-composite-zend_form-elements/2010/03/
just create a custom decorator, extends from Zend_Form_Decorator_Abstract and define the function render, which returns the html that you define inside, for example you can do:
$html = ''; // some code html
$i = 0;
foreach ($element->getMultiOptions() as $value => $label){
$html .= '<label><input type="checkbox" name="'.$element->getName().'[]" id="'$element->getName()'-'.$i.'" value="'.$value.'" />'.$label.'</label>';
$i++;
}
return $html;