Related
I am trying to figure out why Zend Form is not returning errors to my form.
When I take a look inside the: Zdendframework/library/form/form.php file I can see the errors are being generated:
public function isValid()
{
if ($this->hasValidated) {
return $this->isValid;
}
...
if (!$result) {
$this->setMessages($filter->getMessages());
}
return $result;
}
If I var_dump($filter->getMessages(), I see the errors.
However when I go back to my form and attempt to dump the messages, there are none available.
My Controller
$prg = $this->prg();
if ($prg instanceof Response) {
return $prg;
} elseif ($prg === false) {
return $this->getVM()->setVariables([
'form' => $this->registerForm
]);
}
$this->registerForm->setData($prg);
if ( ! $this->registerForm->isValid($prg) ) {
return new ViewModel(['form' => $this->updateForm]);
}
My form:
public function __construct(
InputFilterInterface $inputFilter,
$name = null,
$options = array()
) {
parent::__construct('register', $options);
$this->register = $inputFilter;
}
public function init()
{
$this->add(
[
'name' => 'csrfcheck',
'type' => 'csrf'
]
);
$this->add(
[
'name' => 'user',
'type' => UserFieldset::class,
'options' => [
'use_as_base_fieldset' => true
]
]
);
$this->add(
[
'name' => 'submit',
'type' => 'submit',
'attributes' => [
'value' => 'Update User',
'class' => 'btn green',
]
]
);
$this->getInputFilter()->add($this->register, 'user');
$this->setValidationGroup(
[
'csrfcheck',
'user' => [
'id',
'email',
'firstName',
'lastName',
'roles',
'state'
]
]
);
}
My FieldSet
public function __construct(
ObjectManager $objectManager,
User $userPrototype,
$name = null,
$options = array()
) {
parent::__construct($name, $options);
$this->objectManager = $objectManager;
$this->setHydrator(new DoctrineObject($objectManager));
$this->setObject($userPrototype);
}
public function init()
{
$this->add(
[
'name' => 'id',
'type' => 'hidden',
]
);
$this->add(
[
'name' => 'uuid',
'type' => 'hidden',
]
);
$this->add(
[
'name' => 'email',
'type' => 'email',
'options' => [
'label' => 'E-Mail',
'instructions' => 'Your email address',
],
'attributes' => [
'class' => 'form-control input-large'
]
]
);
$this->add(
[
'name' => 'firstName',
'type' => 'text',
'options' => [
'label' => 'Firstname',
'instructions' => 'Alphanumeric characters (A,b,c,d,e..)',
],
'attributes' => [
'class' => 'form-control',
'type' => 'text',
'pattern' => "^[a-zA-Z-]{2,128}$",
]
]
);
$this->add(
[
'name' => 'lastName',
'type' => 'text',
'options' => [
'label' => 'Last name',
'instructions' => 'Alphanumeric characters, optional ( \' )',
],
'attributes' => [
'class' => 'form-control',
'pattern' => "^[a-zA-Z'-]{2,128}$",
]
]
);
$this->add(
[
'name' => 'state',
'type' => 'select',
'options' => [
'label' => 'User State',
'value_options' => [
0 => 'Active',
1 => 'Locked',
2 => 'Deleted'
]
],
'attributes' => [
'class' => 'form-control'
]
]
);
//#TODO Multiple set to true to make this work remove attributes if this breaks other forms
$this->add(
[
'name' => 'roles',
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'options' => [
'object_manager' => $this->objectManager,
'target_class' => HierarchicalRole::class,
'property' => 'name',
'find_method' => [
'name' => 'getAccessibleRoles'
],
'label' => 'Role'
],
'attributes' => [
'multiple' => true,
]
]
);
My Filter
function __construct(
ObjectManager $objectManager,
ObjectRepository $objectRepository
) {
$this->add(
[
'name' => 'id',
'required' => true,
'filters' => [
['name' => 'Int']
]
]
);
$this->add(
[
'name' => 'uuid',
'required' => false
]
);
$this->add(
[
'name' => 'firstName',
'required' => true,
'filters' => [
['name' => 'StringTrim']
],
'validators' => [
[
'name' => 'StringLength',
'options' => [
'min' => 1,
'max' => 128
]
]
]
]
);
$this->add(
[
'name' => 'lastName',
'required' => false,
'filters' => [
['name' => 'StringTrim']
],
'validators' => [
[
'name' => 'StringLength',
'options' => [
'min' => 1,
'max' => 128
]
]
]
]
);
$this->add(
[
'name' => 'email',
'required' => false,
'filters' => [
['name' => 'StringTrim']
],
'validators' => [
[
'name' => 'StringLength',
'options' => [
'min' => 3,
'max' => 255
]
],
[
'name' => 'EmailAddress',
'options' => [
'useDomainCheck' => true, //Set to false in order to validate local developer test mails: myemail.dev
'message' => "This is not a valid email address"
],
]
]
]
);
$this->add(
[
'name' => 'password',
'required' => false,
'filters' => [
['name' => 'StringTrim']
],
'validators' => [
[
'name' => 'StringLength',
'options' => [
'max' => 128
]
]
]
]
);
$this->add(
[
'name' => 'passwordRepeat',
'required' => false,
'filters' => [
['name' => 'StringTrim']
],
'validators' => [
[
'name' => 'StringLength',
'options' => [
'max' => 128
]
]
]
]
);
$this->add(
[
'name' => 'roles',
'required' => false,
'validators' => [
]
]
);
$this->add( array(
'name' => 'state',
'required' => false
));
}
And my View
<div class="portlet-body form">
<!-- BEGIN FORM-->
<div class="portlet-body form">
<?php
$form = $this->form;
$form->setAttribute('action', $this->url());
$form->setAttribute('class', 'form-horizontal form-bordered');
$form->get('user')->get('roles')->setAttribute('class','form-control input-small select2me');
$form->get('submit')->setValue('Register new user');
$form->get('submit')->setAttribute('class','btn green');
$form->prepare();
$csrf = $form->get('csrfcheck');
$user = $form->get('user');
$id = $user->get('id');
$state = $user->get('state');
$email = $user->get('email');
$firstname = $user->get('firstName');
$lastname = $user->get('lastName');
$role = $user->get('roles');
$sub = $form->get('submit');
?>
<?= $this->form()->openTag($form); ?>
<?= $this->formElement($csrf); ?>
<?= $this->formElement($id); ?>
<?= $this->formElement($state); ?>
<div class='form-body'>
<div class="form-group">
<label class="control-label col-md-3"><?= $this->formLabel($email); ?></label>
<div class="col-md-5">
<div class="input-group" style="text-align:left">
<?php echo '<div class="form-group">'.$this->formElement($email).'</div>'; ?>
</div>
<?php if (!empty($this->formElementErrors($email))) echo '<div class="alert alert-danger">'.$this->formElementErrors($email).'</div>';?>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3"><?= $this->formLabel($firstname); ?></label>
<div class="col-md-5">
<div class="input-group" style="text-align:left">
<?php echo '<div class="form-group">'.$this->formElement($firstname).'</div>'; ?>
</div>
<?php echo '<div class="alert alert-danger">'.$this->formElementErrors($firstname).'</div>'; ?>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3"><?= $this->formLabel($lastname); ?></label>
<div class="col-md-5">
<div class="input-group" style="text-align:left">
<?php echo '<div class="form-group">'.$this->formElement($lastname).'</div>'; ?>
</div>
<?php if (!empty($this->formElementErrors($lastname))) echo '<div class="alert alert-danger">'.$this->formElementErrors($lastname).'</div>';?>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3"><?= $this->formLabel($role); ?></label>
<div class="col-md-5">
<div class="input-group" style="text-align:left">
<?php echo '<div class="form-group input-large">'.$this->formElement($role).'</div>'; ?>
</div>
<?php if (!empty($this->formElementErrors($role))) echo '<div class="alert alert-danger">'.$this->formElementErrors($role).'</div>';?>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3"><?= $this->formLabel($state); ?></label>
<div class="col-md-5">
<div class="input-group" style="text-align:left">
<?php echo '<div class="form-group input-large">'.$this->formElement($state).'</div>'; ?>
</div>
<?php if (!empty($this->formElementErrors($state))) echo '<div class="alert alert-danger">'.$this->formElementErrors($state).'</div>';?>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3"></label>
<div class="col-md-5">
<div class="input-group" style="text-align:left">
<?= $this->formElement($sub); ?>
</div>
</div>
</div>
</div>
<?= $this->form()->closeTag(); ?>
EDIT
In the controller if I dump this:
$result = $this->registerForm->getInputFilter()->getMessages();
die(var_dump($result));
This outputs:
array (size=1)
'user' =>
array (size=1)
'firstName' =>
array (size=1)
'isEmpty' => string 'Value is required and can't be empty' (length=36)
As deduced from the comments and HappyCoder themselves, the problem lies in the Controller where the wrong form is being returned to the view.
if ( ! $this->registerForm->isValid($prg) ) {
return new ViewModel(['form' => $this->updateForm]);
}
Should be:
if ( ! $this->registerForm->isValid($prg) ) {
return new ViewModel(['form' => $this->registerForm]);
}
I have the following multicheckbox:
$this->add(array(
'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
'name' => 'directPractice',
'options' => array(
'label' => 'A. Check all direct practice field education assignments',
**EDIT 1:**
'label_attributes' => array(
'class' => 'label-multicheckbox-group'
),
'object_manager' => $this->getObjectManager(),
'target_class' => 'OnlineFieldEvaluation\Entity\FieldEducationAssignments', //'YOUR ENTITY NAMESPACE'
'property' => 'directPractice', //'your db collumn name'
'value_options' => array(
'1' => 'Adults',
'2' => 'Individuals',
'3' => 'Information and Referral',
'4' => 'Families',
),
),
'attributes' => array(
'value' => '1', //set checked to '1'
'multiple' => true,
)
));
How to make the following part bold?
Using label_attributes makes all labels bold, and I want just the main label for the multibox be bold.
'label' => 'A. Check all direct practice field education assignments',
EDIT 1: add label_attributes to "options"
When I add label_attributes as #itrascastro suggested, all labels become bold and I want only the top one to become bold: multicheckbox
Try to add this to your options:
'options' => array(
'label_attributes' => array(
'class' => 'myCssBoldClass'
),
// more options
),
Each value option accepts an array of options.
For example
'value_options' => [
[
'label' => 'Adults',
'label_attributes' => [
'class' => 'my-bold-class',
],
'attributes' => [],
'value' => 1,
'selected' => false,
'disabled' => false,
],
'2' => 'Individuals',
'3' => 'Information and Referral',
'4' => 'Families',
],
I've added in some other values you can define; they are obviously optional.
Since there is no build in option to do this just for the top label, I used jQuery to accomplish this:
$("label[for]").each(function(){
$(this).addClass("label-bold");
});
My code for my form ( i am using Silex):
$test = array(
'Swedish Cars' => array(
'volvo' => 'Volvo',
'saab' => 'Saab',
),
'German Cars' => array(
'mercedes' => 'Mercedes',
'audi' => 'Audi'
)
);
$form = $app['form.factory']->createBuilder('form')
->add('title','text',array(
'attr' => array(
'placeholder' => 'Title of your Album'
)))
->add('description','textarea',array(
'attr' => array(
'placeholder' => 'Describe your Album'
)))
->add('groups', 'choice', array(
'choices' => $test,
'multiple' => true,
'attr' => array(
'data-placeholder' => 'Add your Groups ...'
),
))
The choices are defined as an multi-array, so I get <option> with <optgropup>. How can I enable in SF2, that some options are selected?
Use the data option:
->add('groups', 'choice', array(
'choices' => $test,
'multiple' => true,
'attr' => array(
'data-placeholder' => 'Add your Groups ...',
'data' => $selected_value
),
where $selected_value can be a single value like 'value_1' or an simple array with multiple values array('value_1', 'value_2') to select.
I would like to use the form submit button with image type. How is this possible in Zend Framework 2?
I have the code for normal submit button:
$this->add(array(
'name' => 'send',
'type' => 'Zend\Form\Element\Submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Send',
),
));
I tried changing the type to 'Zend\Form\Element\Image' or setting type to 'image', but it is not working.
In form class
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'image',
'src' => './images/signin.png',
'height'=> '28',
'width' => '98',
'border'=> '0',
'alt' => 'SIGN IN'
),
));
In .phtml
echo $this->formRow($form->get('submit'));
Zend\Form\Element\Image required 'src' attribute, usage like below:
$form = new \Zend\Form\Form();
$form->add(array(
'name' => 'send',
'type' => 'Zend\Form\Element\Image',
'attributes' => array(
'value' => 'Send',
'src' => 'abc.jpg',
),
));
$helper = new Zend\Form\View\Helper\FormImage();
echo $helper($form->get('send'));
//output <input type="image" name="send" src="abc.jpg">
I do have the following form:
'region' => new sfWidgetFormChoice(array('expanded' => true,'choices' => $region))
I get the folloing html.
<ul class="radio_list">
<li> first...
<li> second....
I want to change the name of class in the table. It shouldn't be radio_list. I want to name it by my own?
How can I do it in 'region'?
Following is not working and changes the classes of <li>:
'region' => new sfWidgetFormChoice(array('expanded' => true,'choices' => $region), array('class' => 'your_class'))
Thanks!
Gunnar
This is my complete Widget:
$this->setWidgets(array(
'recipename' => new sfWidgetFormInputText(array(), array('size' => '40', 'maxlength' => '150')),
'description' => new sfWidgetFormInputText(array(), array('size' => '40', 'maxlength' => '100')),
'ingredients' => new sfWidgetFormTextarea(array(), array('rows' => '10', 'cols' => '35')),
'preparation' => new sfWidgetFormTextarea(array(), array('rows' => '10', 'cols' => '35')),
'kis' => new sfWidgetFormChoice(array('expanded' => true, 'choices' => $kis)),
'category' => new sfWidgetFormChoice(array('expanded' => true, 'choices' => $category)),
'region' => new sfWidgetFormChoice(array('expanded' => true, 'choices' => $region)),
));
You need to use renderer_options for that, since sfWidgetFormSelectRadio overrides passed attribute with it's own option.
new sfWidgetFormChoice(array(
'expanded' => true,
'choices' => $region,
'renderer_options' => array(
'class' => 'your_class'
)
);