How can i get rid of Zend default error not to show "Value is required and can't be empty"
Thanks
I'm using this method:
$this->addElement(
'text',
'testname',
array(
'label' => 'User Name',
'required' => true,
'filters' => array(
'StringTrim'
)
)
);
If you need to change the actual message text then use something like this:
$field = new Zend_Form_Element_Text('field');
$field ->setRequired(TRUE)
->addValidator(
'NotEmpty', //validator name
FALSE, //do not break on failure
array(messages' => array(
'isEmpty' => 'INSERT CUSTOM MESSAGE HERE'
)
)
)
Here you change the 'isEmpty' message of the NotEmpty validator.
Or if you defined the element differently.
$element = $this->getElement('text');
$element->addValidator('NotEmpty', //validator name
FALSE, //do not break on failure
array(messages' => array(
'isEmpty' => 'INSERT CUSTOM MESSAGE HERE'
)
)
)
In Zend_Form this is the default error message of the default validator 'Empty'.
To remove this validator, on the element add the removeValidator() function with 'empty' as the parameter:
$element->removeValidator('Empty');
This is assuming you have made a form in Zend_Form, that you have POSTed an empty value and are trying to validate it?
Going forward, please provide more information.
Related
On a form I add a field like this
$builder->add('cse',
ChoiceType::class,
array(
'label' => '',
'required' => true,
'translation_domain' => 'messages',
'choices' => array(
'I agree' => true
),
'expanded' => true,
'multiple' => true,
'data' => null,
'attr' => array( 'class' => 'form_f' ),
)
)
While all other fields added to the form that have 'required' set to 'true' will prevent the form from being send the required attribute for this field is ignored (the form is sent anyways no matter if checked or not).
Do I have to handle this with Assert statements? If yes - still: why is required not working here?
Yes, use Assert.
Because multiple=true print checkbox. Html validator can test radio, but no checkbox.
Always use Assert for all forms, because html validator isn't safe :)
In my case, I cannot use the asserts, and since was not possible to handle this in user side (unless you use javascript), I made the checks in server side, in the FormEvents::PRE_SUBMIT hook:
$builder->addEventListener(
FormEvents::PRE_SUBMIT,
function (FormEvent $event) {
if ($field->required && !isset($event->getData()[$field->name])) {
$event->getForm()->addError(new FormError('One option must be chosen on "' . $field->label . '"'));
}
});
In renderform I have one input field and its type is "text". How to set a value to that input field, so at every time when the form is loaded the value should be displayed. I am using Prestashop 1.7.
Sample code:
array(
'type' => 'text',
'label' => $this->l('VENDOR_SERVER_IP'),
'name' => 'serverip',
'size' => 50,
'class' => 'fixed-width-xxl',
'required' => true,
'desc' => $this->l('Please enter your server ip.')
),
You need to use the fields_value property
$helper = new HelperForm();
//...
$helper->fields_value = array(
'serverip' => 'x:x:x:x'
);
You have no option to pass default value of input field in the form array. To provide default value, you have to use fields_value property of form helper.
$hlper = new HelperForm();
$value = 'Your already saved value if any';
if (empty($value)) {
$value = 'your default value';
}
$hlper->field_values = array('YOUR_FORM_INPUT_NAME' => $value);
echo $hlper->generate($your_form_array);
Following CI user_guide, I have created a configuration file named "form_validation.php" with in it the following sets:
$config = array(
'user/create' => array(
array(
'field' => 'id',
'label' => '',
'rules' => ''
),
array(
'field' => 'first_name',
'label' => 'lang:First name',
'rules' => 'required|max_length[30]'
),...
),
'user/update' => array(
array(
'field' => 'id',
'label' => '',
'rules' => ''
),
array(
'field' => 'first_name',
'label' => 'lang:First name',
'rules' => 'required|max_length[30]'
),...
)
);
In my 'user' controller, when I call the 'create' method, hence with the URL http://localhost/my_ci_application/user/create, the statement $this->form_validation->run() automatically runs the first set of rules defined in my configuration file. This is the expected behaviour from what I read in the user guide.
But when I run the following URL http://localhost/my_ci_application/user/update/1 to update the user whose ID is 1, it does not automatically load the 'user/update' rules set. It seems like because of the parameter, CI expects to find a 'user/update/1' rules set, which of course I cannot create because the ID of my users will vary all the time when calling this method.
Am I understanding this right? If yes, then that's a pity as I thought standard CI URL were formed like: controller/method/parameters... so I would expect the form validation class to only consider the first two URI segments?!
FYI, if I write in my user.update method the following, my validation rules work fine:
$this->form_validation->run('user/update')
So my question is really if I understood the autoloading of rules properly or not, and if there is anything we can do to autoload those rules even with methods having some parameters.
thank you very much in advance.
In your form_validation.php file:
$CI =& get_instance();
$config = array(
'user/update/' . $CI->uri->segment(3) => array(
....
)
);
if i understant this question u will need call validation, for example:
$this->lang->load('form_validation', 'portuguese'); //if u have order language
if($this->form_validation->run('user/update') == FALSE)
{
//msg error
}
else{
//save
}
To get the value for the url dowel u need:
$this->uri->segment(3);
i hope this has helped
You can extend the library to achieve this
application/libraries/MY_Form_validation.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation {
function run($group = '')
{
if($group == '')
{
$group = '/' . implode('/', array_slice($this->CI->uri->rsegment_array(), 0, 2));
}
return parent::run($group);
}
}
I have a form with 2 selects. Based on the value of the first select, it updates the values of the second select using AJAX. Doing this makes the form not being valid. So, I made the next change:
$form=$this->getAddTaskForm(); //the form
if(!$form->isValid($_POST)) {
$values=$form->getValues();
//get the options and put them in $options
$assignMilestone=$form->getElement('assignedMilestone');
$assignMilestone->addMultiOptions($options);
}
if($form->isValid($_POST)) {
//save in the database
}else {
//redisplay the form
}
Basically, I check if it is valid and it isn't if the user changed the value of the first select. I get the options that populated the second select and populate the form with them. Then I try to validate it again. However this doesn't work. Anybody can explain why? The same "value was not found in the haystack" is present.
You could try to deactivate the validator:
in your Form.php
$field = $this->createElement('select', 'fieldname');
$field->setLabel('Second SELECT');
$field->setRegisterInArrayValidator(false);
$this->addElement($field);
The third line will deactivate the validator and it should work.
You can also disable the InArray validator using 'disable_inarray_validator' => true:
For example:
$this->add( array(
'name' => 'progressStatus',
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'options' => array(
'disable_inarray_validator' => true,
),
));
Additionaly you should add you own InArray Validator in order to protect your db etc.
In Zend Framework 1 it looks like this:
$this->addElement('select', $name, array(
'required' => true,
'label' => 'Choose sth:',
'filters' => array('StringTrim', 'StripTags'),
'multiOptions' => $nestedArrayOptions,
'validators' => array(
array(
'InArray', true, array(
'haystack' => $flatArrayOptionsKeys,
'messages' => array(
Zend_Validate_InArray::NOT_IN_ARRAY => "Value not found"
)
)
)
)
));
Where $nestedArrayOptions is you multiOptions and $flatArrayOptionsKeys contains you all keys.
You may also add options to select element before checking for the form validation. This way you are insured the select value is in range.
I have a form in a Zend-based site which has a required "Terms and Conditions" checkbox.
I have set a custom message which says "you must agree with terms and conditions".
however, because the checkbox is "presence='required'", it returns
Field 'terms' is required by rule 'terms', but the field is missing
which is this constant defined in the Zend framework:
self::MISSING_MESSAGE => "Field '%field%' is required by rule '%rule%', but the field is missing",
I could edit this constant, but this would change the error reporting for all required checkboxes.
How can I affect the error reporting for this specific case?
If you are using the Zend_Form_Element_Checkbox you can customize the error messages on the Zend_Validate validators.
$form->addElement('checkbox', 'terms', array(
'label'=>'Terms and Services',
'uncheckedValue'=> '',
'checkedValue' => 'I Agree',
'validators' => array(
// array($validator, $breakOnChainFailure, $options)
array('notEmpty', true, array(
'messages' => array(
'isEmpty'=>'You must agree to the terms'
)
))
),
'required'=>true,
);
You want to make sure the unchecked value is "blank" and that the field is "required"
You can override the default message like this:
$options = array(
'missingMessage' => "Field '%field%' is required by rule '%rule%', dawg!"
);
And then:
$input = new Zend_Filter_Input($filters, $validators, $myData);
Or
$input = new Zend_Filter_Input($filters, $validators, $myData);
$input->setOptions($options);
...and finally:
if ($input->hasInvalid() || $input->hasMissing()) {
$messages = $input->getMessages();
}
It's mentioned on the Zend_Filter_Input manual page.
Following #gnarf answer, for those of you who may set Form Fields in a slightly different way (like me), you could also do the following:
$agree_tc_and_privacy = new Zend_Form_Element_Checkbox('agree_tc_and_privacy');
$agree_tc_and_privacy
->setLabel("My T&Cs Agreement text ...")
->addValidator('NotEmpty', false, array('messages' => 'You must and agree...'))
->setRequired(true)
->setOptions(
array(
'uncheckedValue'=> '', //important as explained by gnarf above
'checkedValue' => '1',
)
);
$this->addElement($agree_tc_and_privacy);