forms - Symfony 2 - default attributes for labels and fields in all forms - forms

I am using Symfony2 and Twitter Bootstrap along with some customized styles. I would like to have default classes added to 'text' and 'textarea' fields for the whole project unless specified not to use them.
This is the manual way of achieving what I want in one form Type, however it is not efficient at all.
$builder
->add('name', 'text', array(
'label' => 'Name',
'label_attr' => array(
'class' => 'col-md-4 control-label'
),
'attr' => array(
'class' => 'form-control input-md'
)
))
->add('description', 'textarea', array(
'label' => 'Name',
'label_attr' => array(
'class' => 'control-label'
),
'attr' => array(
'class' => 'form-control input-md'
)
))
->add('referenceNo', 'text', array(
'label' => 'Name',
'label_attr' => array(
'class' => 'col-md-4 control-label'
),
'attr' => array(
'class' => 'form-control input-md'
)
))
->add('client','text',array()) //Don't use styling
Should I create a service and call it inside every form Type to get default settings and then pass it as argument?
Should I extend form_div_layout.html.twig and modify it (form customization)? Or is there a better and more efficient way of achieving this task?

First idea: use traits (http://php.net/traits)
Second idea: extend a base form. Call the parent function to get default values.
Both ways are better than creating a service and calling it every time. I prefer using traits to achieve this.

Related

How do I limit number of rows in the admin form in magento

I have following code in my Form.php
$fieldset->addField('name', 'textarea', array(
'label' => Mage::helper('module')->__('Name'),
'class' => 'required-entry',
'required' => true,
'name' => 'name',
));
I want to restrict the number of rows to display it like a single row, the code: 'rows' => 1 doesn't work here.
You can use styling to do this
$fieldset->addField('name', 'textarea', array(
'label' => Mage::helper('module')->__('Name'),
'class' => 'required-entry',
'required' => true,
'name' => 'name',
'style' => "height: 1em;",
));
Just had to change 'textarea' to 'text'

How to change default name in form Symfony2

I have this builder at FormType class:
$builder
->add('fkTblSources', 'entity', array(
'class' => 'BlaBlaBundle:TblSources',
'property' => 'name',
))
->add('save', 'submit');`
This form shows "fkTblSources" as name of row. I need to put this name because it is the field name of the entity "TblTicket" that i want to create with this form.
How can I change this name?
Ok, it's easy, sorry.
Name of field is changed by 'label' option.
->add('fkTblSources', 'entity', array(
'class' => 'BlaBlaBundle:TblSources',
'property' => 'name',
'label' => 'name for field here'
))

Codeigniter form validation using set of rules

I'm trying to create a set of Form validation rules in my Codeigniter project, so that when the validation of the first set fails, the second validation set should not run.
I found this in the CI manual:
$config = array(
'signup' => array(
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'required'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'required'
),
array(
'field' => 'passconf',
'label' => 'PasswordConfirmation',
'rules' => 'required'
),
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'required'
)
),
'email' => array(
array(
'field' => 'emailaddress',
'label' => 'EmailAddress',
'rules' => 'required|valid_email'
),
array(
'field' => 'name',
'label' => 'Name',
'rules' => 'required|alpha'
),
array(
'field' => 'title',
'label' => 'Title',
'rules' => 'required'
),
array(
'field' => 'message',
'label' => 'MessageBody',
'rules' => 'required'
)
)
);
$this->form_validation->set_rules($config);
I know that I can now run the validation of each set separately ($this->form_validation->run('signup') and $this->form_validation->run('email') in this case).
The problem is, that when I use the $config array, errors do not get added to the Form validation class (and thus does not show up) while the form post failed. It did add and show errors when I did not use the set of rules, but just the $this->form_validation->set_rules() method.
What did I do wrong that no error messages are added when entering invalid form data while using a set of rules?
The $config array needs to be in a file called form_validation.php in the application/config directory. It is then loaded automatically when CI is loaded, and passed to the Form validation object when it is created.
The first time the run() method of the FV object is called, if no rules have been set in the FV object, it looks up the config rules it was given on creation, and uses the group indexed by the name passed as argument to run(). All later calls to run() in the same invocation of the CI application, even with different group names, will bypass this check since the rules will now have been set - i.e., you only get to set the rule group once in an invocation.
So you won't be able to create two groups of rules and then call one after the other. You can either call one OR the other.
It might be better to cascade your rules using set_rule() - i.e., add some rules using set_rule(), then validate against them. if they pass, add some more rules and retry validation. You effectively repeat the old rules, knowing they will pass, but that means any failures will be the result of the new rules.
Try array_merge in the form_validation array.
Here if you want two array to combint and gat join validation error. You can use this
$config["form"] = array_merge($config['signup'], $config['email']);
Hope this help.
If someone is facing the same problem try this:
if ($this->form_validation->run('signup') === FALSE) { /* return your errors */ }
$this->form_validation->reset_validation();
$this->form_validation->set_data($data);
if ($this->form_validation->run('email') === FALSE) { /* return your errors */ }
// Do your stuff
You need to reset after each validation to change the rules. You can also use:
$this->form_validation->set_rules($validation_rules);
Note: Set data first and then set rules, it doesn't work the other way around!
hey alwin u need to run the form_validation rules before submitting the form
....
$config = array(
'signup' => array(
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'required'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'required'
),
array(
'field' => 'passconf',
'label' => 'PasswordConfirmation',
'rules' => 'required'
),
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'required'
)
),
'email' => array(
array(
'field' => 'emailaddress',
'label' => 'EmailAddress',
'rules' => 'required|valid_email'
),
array(
'field' => 'name',
'label' => 'Name',
'rules' => 'required|alpha'
),
array(
'field' => 'title',
'label' => 'Title',
'rules' => 'required'
),
array(
'field' => 'message',
'label' => 'MessageBody',
'rules' => 'required'
)
)
);
$this->form_validation->set_rules($config);
///u have to chek form validation getting validate or not
//enter code here
if ($this->form_validation->run() === FALSE) {
$this->load->view('your_view');
} else {$this->ur_controller->method_name();
$this->load->view('whatever ur view');
}
}

How to filter zf2 forms?

I'm trying to add filters for my form elements
$this->add(array(
'name' => 'name',
'attributes' => array(
'type' => 'text',
'required' => true,
),
'options' => array(
'label' => 'Name',
),
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags'),
)
));
but I still can add some tags in this element, what am I doing wrong?
Create two classes one for form another for filter and set filter for your form.
$form = new Form\CommentForm();
$form->setInputFilter(new Form\CommentFilter());
return $form;
for more info see https://github.com/nsenkevich/comment/tree/master/Comment

Can you add an error decorator to a Zend subform?

I have a custom validator that checks all the values in a subform to make sure that they make sense in relation to each other. In the event that this validator fails, I'd like to have an error decorator at the top of the subform to display the error message. Is this possible?
I've already set up the decorators like so:
protected $_decorators = array(
array(
'decorator' => 'FormElements',
'options' => array()
),
array(
'decorator' => 'HtmlTag',
'options' => array(
'tag' => 'ul',
'class' => 'test'
)
),
);
And it seems like I should be able to add
array(
'decorator' => 'Errors',
'options' => array(
'tag' => 'ul',
'class' => 'errors',
'placement' => 'prepend',
)
),
but that causes Zend to fail with the error "htmlspecialchars() expects parameter 1 to be string, array given". What am I doing wrong then? Thanks!
I believe nothing is wrong in your code, just ZF doesn't handle the Errors decorator within Zend_Form_SubForm properly. I hope they will fix this soon.