I've implemented checkboxes into my jFormer form. What returns in the email is "array" and not the value that is set.
// Add components to the form
$multipleChoiceComponentForm->addJFormComponentArray(array(
new JFormComponentMultipleChoice('brochure', 'Multiple choice checkboxes:',
array(
array('label' => 'Send Me a brochure', 'value' => 'Yes'),
),
array()
),
I cannot figure out why its returning "Array" in the email
...the lack of detailed documentation is also not helping. I tried contacting the guy working on the github stuff for jformer, but no answer.
Also, whenever I add
new JFormComponentAddress('currentaddress', 'Current Address:', array(
'validationOptions' => array(),
)),
the email never process...nor does it give an error...the button just says "processing"
anyone can help?
The Docs on jFormer are up at
http://www.jformer.com/documentation/
in jFormer, if the multiplechoice component is of the type 'checkbox' it always returns an array, if you have a single checkbox, try using the key
$formValues->brochure[0]
for the jFormComponentAddress, try just submitting it without the validations options.
Related
I've got an Integer field in Symfony form, which represents quantity - ofcourse it should be unable to pass the value equal or less than 0, so I've validated form in Entity and Form.
But - It's validated only when form is already sent. Is there any built-in method to validate form on input? I know that propably i would need to write own JS function to provide this, but I don't want to repeat someone others job :)
Found the solution.
To validate Symfony form on HTML side, you can just add an attribute to input field, reference: https://www.w3schools.com/htmL/html_form_attributes.asp
So, you form field should look like:
->add('quantity', IntegerType::class, ['label' => false,
'constraints' => new GreaterThan(array('value' => 0)),
'attr' => array(
'min' => '0',
)
])
Use pattern and min on your input. HTML should prevent form submit as long as the pattern isn't matched. (Careful, isn't doens't prevent submit if you're usin Javascript/Ajax to submit)
$builder->add('input_name', TextType::class, array(
'attr'=>array(
'pattern'=>'^[1-9][0-9]*' // 1 and above
'min'=>'1'
)
));
I'm using a TextType to remove the arrows in the input.
If you want to keep them, use NumberType.
A little addition, if you want to make sure a user can only enter a number you can add this bit of js to your code.
->add('quantity', NumberType::class, [ 'attr' =>
[
'oninput' => "this.value = this.value.replace(/[^0-9]/g, '').replace(/(\..*)\./g, '$1');"
] ])
I'm testing a form through the SubmitForm() function because the form uses javascript to cycle through each individual item.
example:
$I->submitForm('#form', array(
'feet' => '1',
'inches' => '2',
), 'submit');
This works fine but I'm having trouble with a file upload input.
$I->submitForm('#form', array(
'feet' => '1',
'inches' => '2',
'file' => ???
), 'submit');
I tried sending an array to mimic the $_FILES array but that obviously isn't the right way to do it. Is this possible?
I've encountered this issue as well and the only way I can see around it is to manually fill the fields and the click the submit button.
For example
$I->fillField(['name' => 'name'], 'Test');
$I->attachFile('input[name=photo]', 'test.jpg');
$I->click('#formId button[type=submit]');
$I->seeCurrentRouteIs('route.index');
$I->see('Model has been updated.');
You can store any test files in the Codeception tests/_data folder.
This does work, but sadly doesn't help me in my current situation as I have a form which dynamically populates various select elements so I need to submitForm as I can't manually selectOption as the options are populated depending on other form completions.
I realise this is old and already marked as answer but it doesn't answer the specific question, which is still a problem for #alexleonard, who posted the accepted answer.
You can user attachFile in conjunction with submitForm. You just have to call is first. For example:
$I->attachFile('#form input[type=file]', <pathtofile> );
$I->submitForm('#form', array(
'feet' => '1',
'inches' => '2',
), 'submit');
I'm trying to fill the following form field with info passed from a form via url. I just can't seem to make it work. My PHP is super limited.
This is the url:
https://www.example.com/membership-account/?level=1&course=Online%20Membership
And this is the text field I'm trying to fill in the php form
$fields[] = new Form_Field("Course", "text", array(
"size" => 40,
"class" => "Course",
"profile" => true,
"required" => true
));
I'm presuming I need to put $_GET['course'] in there somewhere, but wherever I place it, it just doesn't seem to work. Maybe there is more code required than simply including a the $_GET function?
Additional Information:
Mohans addition of the GET function works, however removing 'text' means the field type is not defined so where the field should be, it instead reads - "Unknown type Online Membership for field Course." Including 'text' where it was originally, for some reason prevents $_GET from working??
So I guess the question now is, how do I get 'text' and $_GET to work together??
Additional Information:
Could the $_GET be placed as a value in the array? I've tried putting:
"text" => $_GET['course']
or
"course" => $_GET['course']
But neither work.. I'm probably thinking about it the wrong way.
Any help at this point would be really appreciated.
Thanks
$fields[] = new Form_Field("Course", trim($_GET['course']), array(
"size" => 40,
"class" => "Course",
"profile" => true,
"required" => true
));
I'm using the following code:
$form->addElement('password', 'elementOne');
$form->addElement('password', 'elementTwo', array(
'validators' => array(
array('identical', false, array('token' => 'elementOne'))
)
));
If both texts are different I get an error from the validator, but if I leave the second one empty the validation wont fire. why?
(I dont want to put the fields as required because the user should fill them only if he wants to change the password but he could also leave them empty)
What am I doing wrong? should I put the validator on both elements?
The problem was the AllowEmpty flag (when the element is not required this flag is set to true). I set it to false and the validator is now firing as expected.
setAllowEmpty(false)
$form->addElement('password', 'elementOne');
$form->addElement('password', 'elementTwo', array(
'validators' => array(
array('identical', false, array('token' => $_POST['elementOne']))
)
));
I'm using Zend Framework 1.62 (becuase we are deploying the finished product to a Red Hat instance, which doesn't have a hgih enough PHP version to support > ZF1.62).
When creating a Form using Zend Form, I add a select element, add some multi options.
I use the Zend Form as an in-object validation layer, passing an objects values through it and using the isValid method to determine if all the values fall within normal parameters.
Zend_Form_Element_Select works exactly as expected, showing invalid if any other value is input other than one of the multi select options I added.
The problem comes when I want to display the form at some point, I cant edit the error message created by the pre registered 'InArray' validator added automatically by ZF. I know I can disable this behaviour, but it works great apart from the error messages. I've tryed the following:
$this->getElement('country')->getValidator('InArray')->setMessage('The country is not in the approved lists of countries');
// Doesn't work at all.
$this->getElement('country')->setErrorMessage('The country is not in the approved lists of countries');
// Causes a conflict elswhere in the application and doesnt allow granular control of error messages.
Anyone have any ideas?
Ben
I usually set validators as per my example below:
$this->addElement('text', 'employee_email', array(
'filters' => array('StringTrim'),
'validators' => array(
array('Db_NoRecordExists', false, array(
'employees',
'employee_email',
'messages' => array(Zend_Validate_Db_Abstract::ERROR_RECORD_FOUND => 'A user with email address %value% already exists')
))
),
'label' => 'Email address',
'required' => true,
));
The validators array in the element options can take a validator name (string) or an array.
When an array is passed, the first value is the name, and the third is an array of options for the validator. You can specify a key messages with custom messages for your element in this array of options.
If your using Zend_Form_Element_Select (or any of the Multi subclasses), on validation the InArray validator will only be automatically added if there is not one present.
You can set a validator as so:
$options = array(...);
$this->addElement('select', 'agree', array(
'validators' => array(
array('InArray', true, array(
'messages' => array(
Zend_Validate_InArray::NOT_IN_ARRAY => 'Custom message here',
),
'haystack' => array_keys($options),
)),
'multiOptions' => $options,
));
and then your validator will be used instead of the automatically attached one.
$el = $this->addElement($name, $label, $require, 'select');
$validator = new Zend_Validate_InArray(array_keys(AZend_Geo::getStatesList()));
$validator->setMessage('Invalid US State.');
$el
->setMultiOptions(AZend_Geo::getStatesList())
->setRegisterInArrayValidator(false)
->addValidator($validator)
->addFilter(new Zend_Filter_StringToUpper())
->addFilter(new T3LeadBody_Filter_SetNull())
->setDescription('US State. 2 char.');