Zend Framework 2 - Set prefix for form elements - php

I'm looking for a way to add prefixes for every element of a certain form.
If found this post telling me that there is a elementsBelongTo option which I can use to achieve what I want.
Unfortunately I can't find any documentation explaining the usage. Does anyone know how I can use this option? And if it's still in ZF2?
And furthermore: I there a way to combine it with the nice AnnotationForms?

Don't know if it will help, but instead of prefix you can wrap elements in form or fieldset with form/fieldset name.
You just need to have a name of form or set it and then use setWrapElements
$form->setName('some_name');
$form->setWrapElements(true);
$form->prepare();
from this point full name of element, for example 'password' will be "some_name['password']"
If you are using annotations you can use this with combination of ComposedObject.
It's very good explained here: http://devblog.x2k.co.uk/using-the-composedobject-zend-framework-2-form-annotation/

Related

How to catch different elements?

I'm using smarty for work with html and faced one problems: I have table of different elements (few select and many input). I need to check the values of all elements in the table. With input no problem ($('input[type="text"]')), but how to check in one time select and input values.the problem is that the smart does not provide any additional options for {html_options}. May be there is some AND structure for jQuery selectors?
I see two possible routes here:
Extend your selector with add, e.g.
$('input[type="text"]').add('select').doSomething()
Use a common class for all input and select elements, e.g.
$('.my_input_class').doSomething()
Of course this assumes that you are using common properties/methods on your selection afterwards. For instance, you'd be able to call val() to retrieve the values.
If you want to add the same functionality to two selectors you can this approach..
$('input[type=text] , select').addClass('classname')
You can use a comma to add the same functionality to multiple selectors..

Is there a CMaskedTextField option that enables to not only separate user input with characters, but also in separate textboxes?

Im using CMaskedTextField to obtain this kind of result
However, I am actually trying to get this one
I was wondering if there was an option with CMaskedTextField that enables this feature, or in case there isnt, if anybody knows what is the most practical way in which it can be implemented. I know i could use javascript, but before i get into that im trying to squeeze Yii's provided functionalities as much as possible. Many many thks!
Found it. You need to specify the $placeholder property.
Example: In your view the widget should look like this
<?php $this->widget('CMaskedTextField', array(
'mask'=>'99-9999-99',
'name'=>'mask_tester',
'placeholder'=>' ',
)); ?>
Notice the placeholder is a space rather than default which is an underscore. Also, if you're passing a $model into the form you need to strike the $name property and add $model and $attribute properties.
However, this widget will never give you separate input boxes. It will always be in one input box.

Render one view inside another in cakephp

i have a comments section that gets called at 3 place in one place along with posts and at other two places solo.(comments only). Now using this as a element makes sense. But my posts are also rendered via a element. Does it makes sense to render a element inside another
Here is the answer:
// Render the element in /View/Elements/ajaxreturn.ctp
$this->render('/Elements/ajaxreturn');
http://book.cakephp.org/2.0/en/controllers.html
There's nothing wrong with doing this. No clashes are going to occur as the variables are scoped
http://book.cakephp.org/view/1081/Elements
Since version 2.1 you may embed one view into another by "Extending Views"
$this->extend('/Common/view');
(documentation)
Since I knew about the elements I try to use whenever I can. They help you to minimize the code repetition. It's not unusual include elements inside another.
In fact, I use the same structure in the elements directory as the views directory to organize them.
I assume that the 3 places you mention are handled by views no? So if you have a posts element and a comments element you can include the elements needs according to the view no? In one view you only use the comments element and in the other you only use both elements what do you think?

Is it always bad practice to start an ID with a number? (CSS)

In my project I have submissions and comments, each with an ID. Currently the ID's are just numeric and correspond to their database ID's. Everything is working fine but when I run it through the W3 validator I get the error:
value of attribute "id" invalid: "1" cannot start a name
I suppose instead that I could just precede all ids with some sort of string but then whenever I was using or manipulating the id in JQuery or PHP I have to do a id.replace('string', '') before using it. This seems rather cumbersome. Any advice?
Yes, using numbers as HTML element IDs is bad practice.
It violates W3C specification.
You noted this in your question, and it is true for every HTML specification except HTML5
It is detrimental to SEO.
Search-engine optimized HTML element IDs SHOULD reflect the content of the identified element. Check out How To Compose HTML ID and Class Names like a Rockstar by Meitar Moscovitz. It provides a good overview of this concept.
There can be server-side scripting issues.
Back when I first started programming in ASP classic, I had to access submitted form fields by a syntax like Request.Form("some_id"). However, if I did Request.Form(1) it would return the value of the second field in the form collection, instead of the element with an Id equal to 1. This is a pretty standard behavior for working with collections. Its also similar with javascript, and could make your client side scripting more complicated to maintain as well.
I suggest you to use prefixes "comment-ID" or "post-ID".
If you need the id in JavaScript, you just have to id.substring(8) (for "comment-")
The HTML 5 Specification lifts this restriction. If you're worried about validity you might simply consider changing the DTD to HTML5's.
http://www.w3.org/TR/html5/elements.html#the-id-attribute
If you're manipulating the element then you can just use $(this).jQueryOperation() - therefore you can have a prefix without having to replace anything!
The best way for your need is having the prefix for your class, I mean something like item-x and x is the number that you need.
But from my personal experience, it is better to use classes for your elements, and you know that you must use classes if the item is not unique in the page

Customizing Zend_Form

I am dealing with Zend_Form right now and I am having a difficult time figuring out how to:
Use custom images for form buttons and,
Insert text and links in specific places (in my case I want to put a "forgot your password?" link before the submit button).
I've read through the manual but am not seeing anything about this.
I think Zend_Form_Decorators may help you: http://framework.zend.com/manual/en/zend.form.decorators.html
try to read also this article, I think it is the best one about decorators in Zend_Form
http://devzone.zend.com/article/3450-Decorators-with-Zend_Form
for your usage will be most helpful part about "Full Customization Using the ViewScript Decorator", try to find it in article
You can write straight up HTML in your view, just make sure that the element names, select options, hidden values etc. correspond to your Zend_Form elements (or set the element decorators to just ViewHelper and use echo $this->form->element where the elements should be). It's just the same POST data anyway when you submit the form. If you're using the form in more than one place, check out the ViewScript decorator.
CSS?
You can use an element's setDescription() method to add a description to it. If I remember correctly, this renders into a 'p class="hint"' element, but you can of course change that using the form decorators.

Categories