How to set id attribute to Zend form - php

I have to set id for Zend Framework form. I have tried this
but it appends some encrypted string at the end of the id.
Following is my code:
public function init()
{
$this->setAttrib('id', 'my_form');
$this->setName('form_test');
$this->setSubmitLabel($this->translate('Save Changes'));
$this->setValidatePartial(true);
}
In Browser it shows as
<input name="instance_name" id="my_form_instance_name-kchovmizxlas" value="" aria-describedby="desc_my_form_instance_name-kchovmizxlas" type="text">
Can I use the id name my_form in my jQuery code or need to change the attribute.

Related

Behat/Mink doesn't find field by id

I am trying to fill out a field.
Why does Behat not find the field by id?
Input Field:
<input class="js-text-full text-full form-text required" data-drupal-selector="edit-field-article-nr-supplier-0-value" type="text" id="edit-field-article-nr-supplier-0-value" name="field_article_nr_supplier[0][value]" value="" size="60" maxlength="255" placeholder="" required="required" aria-required="true">
PHP Code:
public function fillField($field, $value)
{
$field = $this->fixStepArgument($field);
$value = $this->fixStepArgument($value);
$this->getSession()->getPage()->fillField($field, $value);
}
Behat:
When I fill in "edit-field-article-nr-supplier-0-value" with "12"
It says it doesn't find a field by id:
When I fill in "edit-field-article-nr-supplier-0-value" with "12" # Drupal\DrupalExtension\Context\MinkContext::fillField()
Form field with id|name|label|value|placeholder "edit-field-article-nr-supplier-0-value" not found. (Behat\Mink\Exception\ElementNotFoundException)
Mink's method fillField() uses findField() to find the field - and findField() uses name as selector and not id. That's why your initial approach didn't work. See source of Mink's class TraversableElement for details.
I found a work around. I filled out the input filed with javascript
/**
* #When I fill in :value on the field :field with javascript
*/
public function findAllInputFields($value, $field){
$javascript = "window.onload = function () {var e = document.getElementById('$field').value='$value';}";
$this->getSession()->executeScript($javascript);
}
Important, don't forget the #javascript annotation.
#javascript
Scenario: Fill out field
When I fill in 12 on the field "edit-field-article-nr-supplier-0-value" with javascript
If someone has a better solution, please share I would like to know.

Protect values after form submission codeigniter

What I am trying to do is show the form validation errors if there are any and prepopulcate the form.
View
<input type="hidden" value="<?php echo $_GET['project_id']; ?>" name="project_id" id="project_id">
<input type="text" value="<?php echo set_value('name'); ?>" name="name" id="name">
Controller
$validation_rules = $this->access_m->rules;
$this->form_validation->set_rules($validation_rules);
if($this->form_validation->run()){
// process the form
}else{
$data= array(
'subview'=>'access/access',
);
$this->load->view('layout', $data, FALSE);
}
Now what the problem is, I can prepopulate the value for the name, but how do I prepopulate the value for the project_id field. Since the value for the field is extracted from the GET method, is there any way I can pass it back to the view so that I can use it again.
I could use a redirect and add the project ID to the url as example.com/access?project_id=23 but then I would loose the values to prepopulate to the rest of the form fields.
Any solutions ??
Create method that accepts parameters example.ufo/controller/accept/<id>
From now on you don't need hidden field, your id is accesible within the method more info is here http://ellislab.com/codeigniter/user-guide/general/controllers.html#passinguri
example:
<?php
class User extends CI_Controller {
public function accesible( $id = false) {
//necessary checks, is id valid? is it numeric? is id in database?... does user have permission to work with element with this ID?
echo $id;
}
}
?>

Yii - cannot define a safe array of attributes for form submit

I have a PHP form that need to submit a array of numbers, what we have in view:
<input type="text" id="ProductForm_sizeobj_1" name="ProductForm[sizeobj[1]]" value="13">
<input type="text" id="ProductForm_sizeobj_2" name="ProductForm[sizeobj[2]]" value="13">
<input type="text" id="ProductForm_sizeobj_3" name="ProductForm[sizeobj[3]]" value="13">
And I define in form class:
public $sizeobj = array();
public function rules() {
return array(
array('/** other attributes **/, sizeobj', 'safe')
);
}
Since "Sizeobj" is a dynamic attribute and the size will growth more then 3, therefore I use array. However after form submitted the error throw as follow:
Failed to set unsafe attribute "sizeobj[1" of "ProductForm".
I believe I might using the wrong method to setup array attribute, or wrong rule, any advice? I'm new to Yii, any help is appreciated.
Use name="ProductForm[sizeobj][1]" instead of name="ProductForm[sizeobj[1]]"

Insert DB Value into Form Field

The Question: How do I insert values from a database table (#__mytable) into form text fields (motitle and modescription) which have been rendered from an XML file within the Joomla 3.0 platform?
-
I've been trying for days to solve this "easy" Joomla! based undocumented challenge.
I have followed Joomla!'s guide for Developing an MVC, read most of their out-of-date documentation and torn apart the com_content component but have still no idea how to populate my fields.
I've been playing with $this->form->bind($this->item);.
Below I have included some of my code to show the structure I am using. Please feel free to point out any issues you spot along the way.
Models\Forms\item.xml
<?xml version="1.0" encoding="UTF-8"?>
<form>
<fields name="groupOPTIONS">
<fieldset name="Options">
<field
type="text"
name="motitle"
id="motitle"
label="Title"
description="MY TEXT FIELD DESCRIPTION"
maxLength="255" />
<field
type="textarea"
name="modescription"
id="modescription"
label="Description"
description="MY TEXT FIELD DESCRIPTION"
rows="15"
cols="5"
maxLength="255" />
</fieldset>
</fields>
</form>
Models\item.php
jimport('joomla.application.component.modelitem');
class MagicObjectsModelItem extends JModelForm {
public function getForm($data = array(), $loadData = true) {
// Get the form 'items'
$form = $this->loadForm('com_magicobjects.item', 'item',
array('control' => 'jform', 'load_data' => $loadData));
if (empty($form)) {
return false;
}
return $form;
}
protected function loadFormData() {
// Check the session for previously entered form data.
$data = JFactory::getApplication()->getUserState('com_magicobjects.item.edit.data', array());
if (empty($data)) {
$data = $this->getDBItem(1);
}
return $data;
}
public function getDBItem($pk) {
//Obtain JDatabase static connection
$oDb = JFactory::getDbo();
$oQuery = $oDb->getQuery(true);
$sValueToMatch = $pk;
$oQuery
->select(array('mid', 'name', 'keyword', 'description'))
->from('#__mytable')
->where('mid = "' . $sValueToMatch . '"')
->order('mid ASC');
$oDb->setQuery($oQuery);
return $oDb->loadObjectList();
}
views\item\view.html.php
jimport('joomla.application.component.view');
function display($tpl = null) {
// Initialise variables.
$this->form = $this->get('Form');
$this->item = $this->get('Item');
//Display the view
parent::display($tpl);
}
Views\item\tmpl\default.php
foreach ($this->form->getFieldset('Options') as $field) {
echo $field->label;
echo $field->input;
}
By performing a print_r() on item I can see I have the data, but I need to insert the data into the fields shown.
In case this it's still a problem because the form definition is defining a <fields /> group.
This mean that the form fields will be output as follows:
input type="text" name="[groupOPTIONS][motitle]"
based on the example you give above. If you remove the fields grouping it will probably work. It's annoying though as certain JForm methods only work on groups...
You would probably need to change the way in which the data was being passed into the form if you wanted to keep the fields grouping (e.g. by overloading getItem).
Hope that makes sense...
In Joomla 3, the JForm::bind() method appears to accept either an object or associative array as a parameter. All of the object/array fields are then stored in a protected JRegistry type data member called JForm::$data.
When you're trying to display a form field (by calling JForm::getInput()), the call stack is as follows
JForm::getInput() -> JForm::getField() -> JForm::loadField() -> JForm::getValue()
JForm::getValue() returns a value from the (aforementioned JRegistry) JForm::$data data member. The JForm::loadField() method also passes the default value (defined by the form) to the JForm::getValue() method in case a value in the JForm::$data variable doesn't exist.
In terms of doing this inside a model, you might want to generate a object or assoc array from a database query or table (ensuring that the field names correspond with the field names defined in the form xml) and then pass it to JForm::bind() as a parameter. However, if you are using JModelForm, I think you should only override JModelForm::loadFormData() to pass the object/assoc array to the loaded form.
See : libraries/joomla/form/form.php
Hope that helps :)
Instead of $oDb->loadObjectList(); use $oDb->loadAssoc(); in your getDBItem() function.
Check this - http://docs.joomla.org/Accessing_the_database_using_JDatabase/1.5
Check this also - joomla loadformdata
Hope this will work.

Initializing a zend form with variable data length

I've been googling most of the day trying to find an answer to this, but I've finally admitted defeat.
I'm working on a form in Zend Framework that needs to be able to handle variable data lengths. I have a form with some general fields that specify some general parameters (item name, language, etc) that are easy enough to deal with, but I've also got a subform called parameters which holds a variable number of key/value lairs that let you add generic parameters into the data. Demonstration form follows:
<form>
<input type="text" name="item_name" />
<input type="text" name="item_lang" />
<!-- etc -->
<input type="text" name="parameters[1][key]" />
<input type="text" name="parameters[1][value]" />
<input type="text" name="parameters[2][key]" />
<input type="text" name="parameters[2][value]" />
<input type="text" name="parameters[3][key]" />
<input type="text" name="parameters[3][value]" />
<input type="text" name="parameters[4][key]" />
<input type="text" name="parameters[4][value]" />
<!-- and so on -->
</form>
Note: the above is a massive simplification of the actual form. It's also manually built instead of being generated by zend_form.
The number of parameters can differ and can be handled client side with javascript, but I'm really struggling to initialize a form when populating it from pre-existing data for update.
I might have 2 parameters stored per item, I might have 20, I might have none at all. So I need the form to have 2 or 20 or no parameters inputs depending on the initial state of the data. Unfortunately the data is not available in init () because it's not in the form until you call setDefaults () on it.
This means I can't do a foreach() on the initial state of the form to generate the appropriate number of input boxes for the already-existing data.
I'm sure I must be missing something obvious, but the Zend documentation is pretty dreadful and I can't find any examples of this use case. It surely can't be so uncommon that it's not supported in zend_form. How do I go about generating the form in a state that allows for the initial state of the form to be variable?
ETA: The init() method on my form looks something like this (simplified to match the example):
public function init ()
{
parent::init ();
$this -> addElement ('text', 'item_name');
$this -> addElement ('text', 'item_lang');
$this -> addSubForm (new Zend_Form_SubForm (), 'parameters');
foreach ($phantom_data as $key => $val)
{
$params = new Zend_Form_SubForm ();
$params -> addElement ('text', 'key');
$params -> addElement ('text', 'value');
$this -> parameters -> addSubForm ($params, $key);
}
}
You can make data avaialbe to form like
My_Form extends Zend_Form
{
protected $_myCustomData;
public function __construct($options = null,$myCustomData)
{
$this->_myCustomData = $myCustomData;
parent::__construct($options); //Its important you call parent after above line or init will get call before initilizing customData
}
public function init()
{
$this->_myCustomData ; //here you are free to use your custom data
}
}

Categories