I am trying to do server side validation with custom validation for the phone and email fields. I am doing the custom validation in the forms action.
Firstly is this the correct place to do it and secondly if so how can I get the data to return to the form if it doesn't meet validation?
Currently it will clear the entire form.
public function doSubmitForm($data, Form $form) {
if (!preg_match("/^[\+_a-z0-9-'&=]+(\.[\+_a-z0-9-']+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/i",$data['Email'])) {
$form->addErrorMessage('Email', 'Invalid email', 'bad');
return $this->redirectBack();
}
if (!preg_match("/^((?:\+)|0)(\d{9,14})$/i",$data['Phone'])) {
$form->addErrorMessage('Phone', 'Please match the correct format eg: 0821234567', 'bad');
return $this->redirectBack();
}
$form->sessionMessage('Thank you for your submission','alert alert-success');
return $this->redirectBack();
}
I suggest you don't do server-side validation like that.
The easiest way is just to use the proper form-fields. Eg. EmailField and PhoneNumberField.
If these don't validate the way you want, just extend them or create your own FormField subclasses.
Here's how EmailField does it's validation: https://github.com/silverstripe/silverstripe-framework/blob/3.5/forms/EmailField.php#L39
Alternatively you could also implement a custom validator by extending Validator or RequiredFields. The validator will be applied to the whole form though and if you start validating individual field types there, you'd be better off just implementing the field as custom class (that way you have a re-usable component).
I had to implement a 10 characters length for phone numbers the other day.
https://github.com/sheadawson/silverstripe-zenvalidator
I included the module above via composer and followed the set up in the README.
For the admin interface I created a getCMSValidator() method
public function getCMSValidator() {
$validator = ZenValidator::create();
$validator->setConstraint('Phone', Constraint_length::create('range', 10, 10)->setMessage('Phone numbers must be 10 digits in length'));
$validator->setConstraint('PhoneAH', Constraint_length::create('range', 10, 10)->setMessage('Phone numbers must be 10 digits in length'));
$validator->setConstraint('PhoneMobile', Constraint_length::create('range', 10, 10)->setMessage('Mobile numbers must be 10 digits in length'));
$validator->disableParsley();
return $validator;
}
For the front end just create a $validator and then add it to the form
$validator = ZenValidator::create();
$validator->setConstraint('Phone', Constraint_length::create('range', 10, 10)->setMessage('Phone numbers must be 10 digits in length'));
$validator->disableParsley();
$Form = new Form($this, 'FormName', $fields, $actions, $validator);
Took me about 20 minutes to implement that minimum 10 & maximum 10 characters on about 5 or 6 different forms.
I hope this helps.
Cheers,
Colin
Related
I have a case where I want to allow user to only increase value during edition. To do so I'd have to compare new value passed in request with old value from entity stored in DB.
Custom validation function receives two arguments: $check, which is a value to be validated and array $context which contains other values from submitted form.
What is the best way to validate edition in the manner I need in CakePHP 3? Is it even possible with validation rules?
you can use Application Rules
You have to create a new new rule in your Table object
suppose that the field that you want to check is priority
So in your rule you check the value of priority (that has just been changed) against the original value stored in $entity->getOriginal('priority')
public function buildRules(RulesChecker $rules)
{
// This rule is applied for update operations only
$rules->addUpdate(function ($entity, $options) {
if($entity->priority >= $entity->getOriginal('priority'))
return true;
else
return false;
},
'CheckPriority', // The name of the rule
[
'errorField' => 'priority', // the field you want
// to append the error message
'message' => 'You have to set a higher Priority' // the error message
]);
return $rules;
}
I'd like to use Laravel's built in validator facade to validate some form fields and then pass back the failed field names using the with operator ex:
foreach(Validator::make(Input::all(), $rules)->getErrors() as $field => $message)
{
$failed_fields[$field] = $message;
}
return Redirect::to('user/login')->with('errors', array($failed_fields);
so I can then pass the array to a js array and consume the errors to then find the fields and change their background colors etc.
Any idea how to access these values in this or a similar manner?
Ok I figured it out after looking at the classes responsible and it was quite simple for me.
if($validator->fails())
{
return Redirect::route('cart')
->withErrors($validator)
->with('json_field_errors',json_encode($validator->errors()->getMessages()))
->withInput(Input::except('card_number'));
}else{
//Clear the cart and add the one product
Cart::destroy();
Cart::add('product', 'Description', 1, 69.95);
//Pass to the processor for order processing
return $this->process();
}
I am validating phone number with codeigniter
In my city, I apply the rule like this:
$this->form_validation->set_rules('voter_phone', 'Phone number', 'required|exact_length[8]|integer');
however, since my city doesn't have the 1 or 4 for the first character , I would like to know are there any way to add checking for this rule? Thanks for helping
Reference:
http://cimple.org/user_guide/libraries/validation.html
you can use callback for that purpose its available in the same LINK you have mentioned
$this->form_validation->set_rules('voter_phone', 'Phone number', 'required|exact_length[8]|integer|callback_valid_number');
and callback function can be
public function valid_number($str)
{
if(!preg_match("/^[235-9]{1}[0-9]{7}$/", $str))
{
$this->form_validation->set_message('valid_number', 'Invalid Mobile No.');
return false;
}
else
{
return true;
}
}
this regex checks if
1. it is required
2. phone number is int only
3. it should be exactly 8 digit
4. it does not start with either 1 or 4
so you can remove required|exact_length[8]|integer
You need a callback to validate your custom validation.
//here checkphone is custom callback
$this->form_validation->set_rules('voter_phone', 'Phone number', 'required|exact_length[8]|integer|callback_checkphone');
so make a function/method called checkphone in your controller and validate your input(voter_phone)
function checkphone($num)
{
//your first charcter in voter_phone
$first_ch = substr($num,0,1);
if ($first_ch==1 || $first==4)
{
//set your error message here
$this->form_validation->set_message('checkphone','Invalid Phone number,Not allowed 1 or 4 as first charcter');
return FALSE;
}
else
return TRUE;
}
I have a form (defined in XML) which is used with Joomla's JForm to handle. What I'd like to know is if it's possible to validate against multiple rules at once.
Typically, I've come to understand that Joomla's JForm accepts only one rule for validation, defined in the XML of the form:
Joomla's JForm internals also seem to suggest I can't, the following area being the only one I can find handing validation:
// Get the field validation rule.
if ($type = (string) $element['validate'])
{
// Load the JFormRule object for the field.
$rule = $this->loadRuleType($type);
// If the object could not be loaded return an error message.
if ($rule === false)
{
throw new UnexpectedValueException(sprintf('%s::validateField() rule `%s` missing.', get_class($this), $type));
}
// Run the field validation rule test.
$valid = $rule->test($element, $value, $group, $input, $this);
// Check for an error in the validation test.
if ($valid instanceof Exception)
{
return $valid;
}
}
This isn't wrapped in a loop, so I'm quite concerned that I can't apply multiple rules at once to a particular field.
Are you looking for server or client side validation? Sean's answer seems to cover server side so I figured I'd add some insight into client side techniques.
You enable client side validation two ways. The first and simplest would be by adding the following to your form field definition, which would ensure any required fields are filled out to proceed.
required="true"
Second would be to add a class to the form field definition to let Joomla core know you want to validate the field and how. Joomla offers 4 validations built into the core: validate-username, validate-password, validate-numeric and validate-email.
These in and of themselves don't help you much, but the ability to create and reference custom client-side validations does. For my example we're going to ensure a check box is marked before allowing the form to submit. So in the form field definition I'll add:
class="validate-checked"
On the page where you render the form, be sure to load the JS library for validation using:
JHtml::_('behavior.formvalidation');
In addition, add the class form-validate to your form HTML element.
Add this javascript to handle the actual validation, here I have a checkbox input type with an ID of tos I'm verifying. In the setHandler method, the first parameter is the custom name I entered in the form field definition class statement, validate-checked:
<script>
window.addEvent('domready', function(){
document.formvalidator.setHandler('checked', function(value) {
var tos = document.getElementById('tos');
if (tos.checked) {
return true;
} else {
return false;
}
});
});
</script>
Now, capture the submit event and verify all core and custom validations passed before submitting the form.
Joomla.submitbutton = function(task) {
if (task == 'user.cancel' || document.formvalidator.isValid(document.id(".myFormId"))) {
Joomla.submitform(task, document.getElementById('myformId'));
}
You can create as many custom client-side validation scripts as you want. Inside the setHandler method you can interact with the DOM and use the passed in value parameter to determine if the field should pass, only needing to worry about returning true or false to indicate results and Joomla will handle the rest. So you can either create one complicated validation or many smaller concise validations to suit your needs.
Hope that helps...
This is a common request. There are a few possibilities. You could write your own JFormRule with more complex validation. The other is that you could programatically add an attribute to the field that runs the additional validation sort of like what Sean is advocating.
This answer assumes that it is not possible to natively add multiple rules on one field.
Assuming that it is not possible to apply multiple rules to one field natively, then it may be possible to extend JForm::validateField() to enable such a feature by simply calling the validate method for each validation rule found.
// Extending class JForm
protected function validateField(SimpleXMLElement $element, $group = null, $value = null, JRegistry $input = null) {
if($type = (string) $element['validate'])
{
$multiple_types = explode('|', $type);
if(is_array($multiple_types) && $multiple_types[0] !== $type)
{
foreach($multiple_types as $single_type)
{
$result = parent::validateField($element, $group, $value, $input);
// Validation failed, return the result and stop validating.
if($result !== true)
{
return $result;
}
}
return true;
}
else
{
return parent::validateField($element, $group, $value, $input);
}
}
}
With that example, validation rules could be structured like:
validate="rule1|rule2"
I have an element. I want to add a custom validator and custom filter to it. The validator makes sure the input is one of several permitted values, then the filter adds some custom values to the input. This means I have to validate the original input first before running the filter. I do it in this order
$element = new Zend_Form_Element_Text('element');
$element->addValidator('PermittedValue', false);
$element->addFilter('TotalHyphen', false);
$this->addElement($element);
but this order isn't being respected. The filter runs first and changes the data, then the validator runs on the filtered data which means it always fails even for valid input. It seems from documentation that this is intentional
Note: Validation Operates On Filtered
Values Zend_Form_Element::isValid()
filters values through the provided
filter chain prior to validation. See
the Filters section for more
information.
How can I specify the order in which validators and filters run?
Sure seems like creating a custom element that supports post-validation filtering would be the way to go. How about this:
/**
* An element that supports post-validation filtering
*/
class My_Form_Element_PostValidateFilterable extends Zend_Form_Element_Text
{
protected $_postValidateFilters = array();
public function setPostValidateFilters(array $filters)
{
$this->_postValidateFilters = $filters;
return $this;
}
public function getPostValidateFilters()
{
return $this->_postValidateFilters;
}
public function isValid($value, $context = null)
{
$isValid = parent::isValid($value, $context);
if ($isValid){
foreach ($this->getPostValidateFilters() as $filter){
$value = $filter->filter($value);
}
$this->setValue($value);
}
return $isValid;
}
}
Usage would be something like this:
$elt = $form->addElement('PostValidateFilterable', 'myElement', array(
'label' => 'MyLabel',
'filters' => array(
'StringTrim',
// etc
),
'validators' => array(
'NotEmpty',
// etc
),
// here comes the good stuff
'postValidateFilters' => array(
new My_Filter_RunAfterValidateOne(),
new My_Filter_RunAfterValidateTwo(),
),
));
This keeps the validation and filtering in the form - keeping the controller thin.
Not tested, just a stab in the dark. And surely you could fatten/modify the API to add/remove filters by key, etc.
Whaddya think?
Maybe don't add the filter at all. Validate the content first in the controller, and then use the filter separately:
$request = $this->getRequest();
if ($request->isPost() && $form->isValid($request->getParams())) {
$filter = new Filter_Whatever();
$val = $filter->filter($request->getParam('element'));
... //call your model or whatever
}
I've never done this, but I suppose this (or something similar) might work.
Good point ! ,
AFAIK filters should or must run before validating the input :
from ZF docs
It's often useful and/or necessary to
perform some normalization on input
prior to validation. For example, you
may want to strip out all HTML, but
run your validations on what remains
to ensure the submission is valid. Or
you may want to trim empty space
surrounding input so that a
StringLength validator will use the
correct length of the input without
counting leading or trailing
whitespace characters.
but if and only if you are in case which can't solve mingos's answer must be the help
What you want to achieve is to change default behavior of how text element is being processed. Thus, I think you could create your own element (e.g. My_Form_Element_Text) that extends Zend_Form_Element_Text and overload its isValid() method.
Specifically you could just change second line in the orginal isValid() method, from $value = $this->getValue(); into $value = $this->getUnfilteredValue();. This way your validation will be performed using unfiltered values.