Zend_Form - is there a way to repost into the form if it is invalid? - zend-form

I am having trouble injecting form data back into the form so the form is not reset if it is invalid.
Any way to do that? Or, more importantly, best/most ideal way?
I have my form logic and checking, just need a little help in exactly where I should be focusing the above effort.
Thanks!

Yeah, the $form->isValid($_POST) call should handle it. For example:
$form = new Zend_Form();
$form->setAction('/submit');
$form->setMethod('post');
$form->addElement('text', 'first_name', array(
'label' => 'First Name:',
'required' => true));
if (!$form->isValid($_POST)) {
//handle errors
} else {
//success
}

Related

Drupal9: Saving form as node in SQL (custom module example)

i develop a custom module with forms to save data in SQL-Datebase. I want to use for that the node-structure.
Normal SQL-savings for example table works but not for the node-tables.
Any idea what is going wrong?
This ist my Code for saving, which works in non-node-tables:
public function submitForm(array &$form, FormStateInterface $form_state) { $connection = \Drupal::service('database');
$result = $connection->insert('node.node__body')
->fields(['body_value'])
->values([
'body_value' => 'text for body',
])
->execute();
$form_state->setRedirect('modulname.form');
}
Use Entity API in Drupal to manipulate or create a node.
In your case,
$node = \Drupal::entityTypeManager()->getStorage('node')->create(
[
'type' => 'page',
'title' => 'New Basic Page',
'body' => 'text for body'
]
);
Here, type is the content type machine name. Don't forget to update with your own. Also you probably want to inject the entity_type.manager service and use in the code.
Get more info here: Working with entities in Drupal

Creating Prestashop back-office module with settings page

I'm creating a back-office module for Prestashop and have figured out everything except the best way to display the admin page. Currently I'm using the renderView() method to display the content of view.tpl.
I would like to display a table with values and an option to add a new row. Should I just create it in the view.tpl or is there a better way? I've seen the renderForm() method but haven't figured out how it works yet.
The biggest question I have is, how do I submit content back to my controller into a specific method?
ModuleAdminController is meant for managing some kind of records, which are ObjectModels. Defauly page for this controller is a list, then you can edit each record individually or view it's full data (view).
If you want to have a settings page, the best way is to create a getContent() function for your module. Besides that HelperOptions is better than HelperForm for this module configuration page because it automatically laods values. Define the form in this function and above it add one if (Tools::isSubmit('submit'.$this->name)) - Submit button name, then save your values into configuration table. Configuration::set(...).
Of course it is possible to create some sort of settings page in AdminController, but its not meant for that. If you really want to: got to HookCore.php and find exec method. Then add error_log($hook_name) and you will all hooks that are executed when you open/save/close a page/form. Maybe you'll find your hook this way. Bettter way would be to inspect the parent class AdminControllerCore or even ControllerCore. They often have specific function ready to be overriden, where you should save your stuff. They are already a part of execution process, but empty.
Edit: You should take a look at other AdminController classes, they are wuite simple; You only need to define some properties in order for it to work:
public function __construct()
{
// Define associated model
$this->table = 'eqa_category';
$this->className = 'EQACategory';
// Add some record actions
$this->addRowAction('edit');
$this->addRowAction('delete');
// define list columns
$this->fields_list = array(
'id_eqa_category' => array(
'title' => $this->l('ID'),
'align' => 'center',
),
'title' => array(
'title' => $this->l('Title'),
),
);
// Define fields for edit form
$this->fields_form = array(
'input' => array(
array(
'name' => 'title',
'type' => 'text',
'label' => $this->l('Title'),
'desc' => $this->l('Category title.'),
'required' => true,
'lang' => true
),
'submit' => array(
'title' => $this->l('Save'),
)
);
// Call parent constructor
parent::__construct();
}
Other people like to move list and form definitions to actual functions which render them:
public function renderForm()
{
$this->fields_form = array(...);
return parent::renderForm();
}
You don't actually need to do anything else, the controller matches fields to your models, loads them, saves them etc.
Again, the best way to learn about these controller is to look at other AdminControllers.

Yii cgridview give reference to checkbox

Because when adding the CCheckBoxColumn to my vgridview in a form, in return I have no precise index for working with the checked box data's. So I try to add the 'uncheckValue' but I am unable to link it to the reference value of my table. Is there a way to access this value for the current row ? (the $data->reference in my code return a Undefined variable).
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'gview',
'dataProvider'=>$dataProvider,
'columns'=>array(
'client',
'reference',
array(
'class'=>'CCheckBoxColumn',
'id'=>'CB',
'selectableRows'=>2,
'checkBoxHtmlOptions'=>array(
'uncheckValue'=>$data->reference, ),
)),));
Tks anyone could put me on the way (if there is one... )
I finally find one way is to extend CCheckBoxColumn.
As inside this code I have access to $data. Now my form return checkbox with his 'name' as the 'reference' column of my table, so I can do further batch treatment.
The uncheckValue hidden field was not suitable as it was only giving index for unchecked fields (!).
I believe this code should not stay in view but in extension...
Any comments still welcome....
Yii::import('zii.widgets.grid.CCheckBoxColumn');
class LIndexCheckBoxColumn extends CCheckBoxColumn {
public $linkId;
public function renderDataCellContent($row,$data)
{
if($this->value!==null)
$value=$this->evaluateExpression($this->value,array('data'=>$data,'row'=>$row));
else if($this->name!==null)
$value=CHtml::value($data,$this->name);
else
$value=$this->grid->dataProvider->keys[$row];
$checked = false;
if($this->checked!==null)
$checked=$this->evaluateExpression($this->checked,array('data'=>$data,'row'=>$row));
$options=$this->checkBoxHtmlOptions;
//$name=$options['name'];
$varLink=$this->linkId;
$name=$data->$varLink;
unset($options['name']);
$options['value']=$value;
$options['id']=$this->id.'_'.$row;
echo CHtml::checkBox($name,$checked,$options);
}
}
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
'client',
'reference',
array(
'class'=>'LIndexCheckBoxColumn',
'id'=>'cb',
'selectableRows'=>2,
'linkId'=>'reference',
)),));

Yii CListview -> pagination and AjaxLink/ajaxButton

I have problems regarding with pagination and Ajax form.
Here is my code for Controller:
$dataProvider = new CActiveDataProvider('User', array(
'pagination'=>array(
'pageSize'=>10,
),
));
$this->render('users',array(
'dataProvider'=>$dataProvider,
));
For view -> users:
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_user',
);
For render _users:
echo CHtml::ajaxLink($text, $this->createUrl('admin/deleteuser',array('id'=>$data->iduser)), array('success'=>"js:function(html){ alert('remove') }"), array('confirm'=>_t('Are you sure you want to delete this user?'), 'class'=>'delete-icon','id'=>'x'.$viewid));
if i have 15 rows in a database it will only show 10 and will generate a pagination (ajaxUpdate = true) for next 5. The first 10 rows has no problem with the ajaxLink because the clientscript was generated but problem is the when I move to the next page, the ajaxLink is not working because its not generated by the pagination .
any idea? thanks
An alternate method, check this post in the yii forum. So your code will become like this:
echo CHtml::link($text,
$this->createUrl('admin/deleteuser',array('id'=>$data->iduser)),
array(// for htmlOptions
'onclick'=>' {'.CHtml::ajax( array(
'beforeSend'=>'js:function(){if(confirm("Are you sure you want to delete?"))return true;else return false;}',
'success'=>"js:function(html){ alert('removed'); }")).
'return false;}',// returning false prevents the default navigation to another url on a new page
'class'=>'delete-icon',
'id'=>'x'.$viewid)
);
Your confirm is moved to jquery's ajax function's beforeSend callback. If we return false from beforeSend, the ajax call doesn't occur.
Another suggestion, you should use post variables instead of get, and also if you can, move the ajax call to a function in the index view, and just include calls to the function from the all links' onclick event.
Hope this helps.
Not fully sure, but given past experiences I think the problem is in the listview widget itself.
If the owner of the widget is a Controller, it uses renderPartial to render the item view.
Renderpartial has, as you may or may not know, a "processOutput" parameter which needs to be set to TRUE for most of the AJAX magic (its FALSE by default).
So perhaps you can try to just derive a class of the listview and add a copy in there of "renderItems()". There you would have to change it so that it calls renderPartial with the correct parameters.
In the widget Clistview, I added afterAjaxUpdate
$jsfunction = <<< EOS
js:function(){
$('.delete-icon').live('click',function(){
if(confirm('Are you sure you want to delete?'))
{
deleteUrl = $(this).attr('data-href');
jQuery.ajax({'success':function(html){ },'url':deleteUrl,'cache':false});
return false;
}
else
return false;
});
}
EOS;
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'id'=>'subscriptionDiv',
'itemView'=>'_subscription',
'afterAjaxUpdate'=>$jsfunction,
'viewData'=>array('is_user'=>$is_user,'all'=>$all),
'htmlOptions'=>($dataProvider->getData()) ? array('class'=>'table') : array('class'=>'table center'),
)
);
and in the _user just added attribute data-href
<?php echo CHtml::ajaxLink($text, $this->createUrl('admin/deletesubscription',array('id'=>$data->idsubscription)),
array('success'=>"js:function(html){ $('#tr{$viewid}').remove(); }"),
array('confirm'=>_t('Are you sure you want to unsubscribe?'),
'class'=>'delete-icon',
'id'=>'x'.$viewid,
'data-href'=>$this->createUrl('admin/deletesubscription',array('id'=>$data->idsubscription))
)); ?>
try
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_user',
'enablePagination' => true,
);
if this doesn't solve, try including the total number of records in the data provider options as -
'itemCount' => .....,

How does one add a 'plain text node' to a zend form?

I'm trying to add a plain text node in a zend form - the purpose is to only dispay some static text.
The problem is - im not aware of any such way to do it.
I have used 'description' but that HAS to be attached to a form element.
Is there any way to simply display some text as part of a form? Zend considers everything as a form element so I cannot just print it out.
Eg:
The following will test your ability on so and so.
.
.
.
etc...
Any thoughts?
Zend has a form note view helper (Zend_View_Helper_FormNote), which you can use to add text.
Just create a new form element (/application/forms/Element/Note.php):
class Application_Form_Element_Note extends Zend_Form_Element_Xhtml
{
public $helper = 'formNote';
}
In your form:
$note = new Application_Form_Element_Note(
'test',
array('value' => 'This is a <b>test</b>')
);
$this->addElement($note);
Adding a hidden element with non-escaped description does the thing.
$form->addElement('hidden', 'plaintext', array(
'description' => 'Hello world! Check it out',
'ignore' => true,
'decorators' => array(
array('Description', array('escape'=>false, 'tag'=>'')),
),
));
Works perfectly. It is still attached to an element, which is, however, not rendered this way.
Code taken from: http://paveldubinin.com/2011/04/7-quick-tips-on-zend-form/
There might be a better way, but I created a paragraph by using a custom form element and view helper. Seems like alot of code for something so simple. Please let me know if you've found a more simplistic way to do it.
//From your form, add the MyParagraph element
$this->addElement(new Zend_Form_Element_MyParagraph('myParagraph'));
class Zend_Form_Element_MyParagraph extends Zend_Form_Element
{
public $helper = 'myParagraph';
public function init()
{
$view = $this->getView();
}
}
class Zend_View_Helper_MyParagraph extends Zend_View_Helper_FormElement {
public function init() {
}
public function myParagraph() {
$html = '<p>hello world</p>';
return $html;
}
}
A little late but thought I'd throw it in anyway for the benefit of the community.
Aine has hit the nail on the head. FormNote is what you need if you want to use text in Zend_Form. However, you can use it without needing to extend Zend_Form_Element_Xhtml. See example below:
$text = new Zend_Form_Element_Text('myformnote');
$text->setValue("Text goes here")
->helper = 'formNote';
Note that you can use both text and html with the formNote helper.
This functionality is built into Zend via Zend_Form_Element_Note.
$note = new Zend_Form_Element_Note('forgot_password');
$note->setValue('Forgot Password?');
I faced the same problem and decided is better not to use Zend_Form at all, but to use directly view helpers (like Ruby on Rails does) and validate on the model.
This one-liner works for me:
$objectForm->addElement(new Zend_Form_Element_Note('note', array('value' => 'Hello World')));