field considered empty in FormRequest and validations - php

I have a template that allows adding new lines. Example: I want to add multiple products at once to a store, so I can add them all at once, through those lines that replicate themselves. The problem is that I need to validate these fields, they are all mandatory. I'm implementing so that I can walk through each one and leave it compulsory. However, I came across another problem, Laravel is telling me that my field is empty, but it is not. I would like to know how I can solve it, thank you in advance.
Here is my code to FormRequest
public function rules()
{
$rules = [];
foreach($_POST['esp'] as $key => $esp){
$name_field = "esp[" . $key . "]" . "[esprqe]"; //name of my field is complicated even as it is time based
$rules[$name_field] = 'required';
}
return $rules;
}
My template is big, no need to show.
message I receive: esp[1553533952015][esprqe]: ["The esp[1553533952015][esprqe] field is required."]
My input is not empty, I do not know what the problem is

You state the name of your field 'esprqe' is time based and complicated. This is likely the cause of your problem.
The esp['.$key.'] field is probably fine, as this field is transferred from your form. However, the value that goes into esprqe, if that is being generated in your server side code above (I don't know, you haven't provided how this field is generated), and if the rules are looking for that field to match a time-based generated field on the form... those values won't match and you will have a non-value coming into your method above.
This would very likely generate a message
["The esp[1553533952015][esprqe] field is required."]
If this is your issue, you can test by first creating a simple numbered index for the esprqe field that will be matched on both form and method. If this succeeds, you can then increase the complexity using a common generator so that the rules section knows exactly what the name of the field is. Time can't be common between form creation and rules creation - thus perhaps the reason for the failure.

You need use "dot notation" to validate attributes within an array.
$name_field = "esp.$key.esprqe";
Laravel Docs: validating arrays

Related

Read property of a validation rule in CakePHP 3.x for use in a js function

I have a validation rule in one of my Table classes like this:
public function validationDefault(Validator $validator)
{
$validator
->scalar('comment')
->maxLength('comment', 3000)
->requirePresence('comment', 'create')
->notEmpty('comment');
return $validator;
}
This validates the comment field of an input and means it cannot be over 3000 characters in length. All of this is fine when working in PHP.
One part of my application uses a JavaScript character counter - it tells the user how many remaining characters they have in a field as they type. The js for this works fine, with the limit 3000 hardcoded.
However, I want to know if there's a way to avoid hardcoding this limit in my js? Because otherwise my code is not DRY as I'm defining the 3000 limit in multiple places and if it changes that's problematic to remember/update.
Is it possible to read the maxLength property directly from validationDefault for the comment field? This question concerns how to access the data defined in the Table class; I am fine with knowing how to pass it to js via ajax.
I haven't got any further code to show as I don't know if/how this is even possible.
CakePHP 3.5.13
To get validation rule value, you have to get Validator from Table, then get ValidationSet, and then ValidationRule, from which you can extract desired result. Sample controller code below:
$validator = $this->YourTable->getValidator("default");
$validationSet = $validator->field("comment");
$validationRule = $validationSet->rule("maxLength");
$result = $validationRule->get("pass");
Or, in just one line:
$result = $this->YourTable->getValidator("default")->field("comment")->rule("maxLength")->get("pass");
A returned value will be an array of additional arguments passed to validation rule, in your case it should look like:
array(1) {
[0]=>
int(3000)
}
Also, #ndm in his answer mentioned about other posibilities:
On form helper/context level it's also possible to read the schema, and support for transalting its length configuration and the validation rule value into a maxlength HTML attribute is being implemented for CakePHP 3.7
Further reading:
Getting validators from tables
Validator class
ValidationSet class
ValidationRule class

Checking for empty values when saving into multiple tables with ZF2

I'm using ZF2 and mysql, but the question is platform-independent. I have a data transfer object Organization that gets hydrated from an html form. OrganizationMapper has a save method that (1) gets Organization as an argument and (2) fills a couple of database tables one after another.
Suppose the 1st table gets filled ok, but the 2nd doesn't because one of the properties of Organization isn't set (not null constraint on a column). The user gets an error, but the 1st table is already filled. If he attempts to submit the form again, but this time with all html fields filled, all the tables get filled ok, but the 1st has a previous unused row.
How could I avoid this situation?
I thought of checking for empty values with if's in the mapper's save method, but it doesn't seem elegant. I know about the InputFilter validations in ZF2, but these check the user input in the form, they don't check things when the php code communicates with the database.
Any help?
The best way is to validate all the data before you start writing it to the database.
I didn't use ZF2 and this solution is actually framework-dependent, so you need to check the ZF2 docs. For example, in Yii, you just define validation rules for every field of the model, so you can ensure that your Organization contains all the data before you start saving it to the database, probably something similar is possible in Zend.
Note, that validation doesn't mean just to check for empty values, you may need to verify different things, like: "email is correct email like xxx#yyy.com", "name is not empty", "name length is more than 3 chars", "name length is less than 1000 chars" and so on.
For Yii it roughly looks like this:
class Organization extends ActiveRecord {
...
// here we define the validation rules
public function rules() {
return [
// name is required
['name', 'required'],
// check min / max length
['name', 'string', 'min' => 3, 'max' => 12],
// check if email is valid
['email', 'email']
];
}
}
Now you can do $organization->validate() to make sure everything is correct (also when you do $organization->save() the rules will be checked before saving to the database).
And one more solution to protect from the inconsistent data is to use transactions. In the case you write to multiple tables, you anyway need them, even if you validated everything. Unexpected things happen, so it is better to protect your saving code like this (pseudo-code):
$transaction->start();
try {
$table1->writeSomeData();
$table2->writeMoreData();
$transaction->commit();
} (catch Exception $e) {
$transaction->rollback();
}
Again, check your framework documentation, it probably supports this in some way.

How can I require all fields matching a regular expression with laravel?

I am making an application where users can upload questions, and questions can have multiple correct answers. The correct answers have names of the form correctAnswer1 correctAnswer2 etc.
I want to know how to require all submitted fields matching this pattern; I was thinking of using something analogous to
/correctAnswer[0-9]/ => 'required'
I'm not sure of the logic behind your requirement, kinda seems you should do things in a different manner, but again I do not know how your app works so I can't be a judge of that. So if the user can add new correct answers fields on the form, and you wan't them to not be empty it makes some sense.
You can't have a regex in the rule name but you can do the following:
$rules = [
// your other rules
];
$correctAnswers = preg_grep( '/^correctAnswer[1-9]{1}$/', array_keys($this->all()));
// use $this->all() when in Http\Requests\YourRequest
// if you are not using the request method of validation (you validate in controller)
// simply replace $this->all() with $request->all() or Input::all().
foreach ($correctAnswers as $correctAnswer) {
$rules[$correctAnswer] = 'required';
}
return $rules;
This assumes you are using the Laravel 5, Http\Requests to validate your input. If you are doin'g the validation elsewhere (in controller for example), just replace $this->all() with $request->all() or Input::all(). I can't give the exact choice as I do not know exactly how you do the validation and what version of laravel you use.
PS: This will match only correctAnswer1 to correctAnswer9. If you want more just play with the [0-9]{1} part of the regex.

Testing for POST in Yii 2.0

In my controllers that Gii creates it is common to see the following:
if($model->load(Yii::$app->request->post()) && $model->save()){
//.....do something such as redirect after save....//
}else
{
//.....render the form in initial state.....//
}
This works to test whether a POST is sent from my form && the model that I am specifying has saved the posted information (as I understand it).
I've done this similarly in controllers that I have created myself but in some situations this conditional gets bypassed because one or both of these conditions is failing and the form simply gets rendered in the initial state after I have submitted the form and I can see the POST going over the network.
Can someone explain why this conditional would fail? I believe the problem is with the 'Yii::$app->request->post()' because I have removed the '$model->save()' piece to test and it still bypasses the conditional.
Example code where it fails in my controller:
public function actionFreqopts()
{
$join = new FreqSubtypeJoin();
$options = new Frequency();
$model = new CreateCrystal();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$model->insertFreqopts();
return $this->redirect(['fieldmap', 'id' => $join->id]);
} else {
return $this->render('freqopts', ['join' => $join, 'options' => $options]);
}
}
My initial thought was that I'm not specifying the correct "$model" in that I'm trying to save the posted data to FreqSubtypeJoin() in this case and the $model is CreateCrystal(); however, even when I change the model in this conditional it still fails. It would be helpful if someone could briefly explain what the method 'load' is actually doing in layman's terms if possible.
The load() method of Model class is basically populating the model with data from the user, e.g. a post query.
To do this it firstly loads your array of data in a form that matches how Yii stores your record. It assumes that the data you are trying to load is in the form
_POST['Model name']['attribute name']
This is the first thing to check, and, as long as your _POST data is actually getting to the controller, is often where load() fails, especially if you've set your own field names in the form. This is why if you change the model, the model will not load.
It then check to see what attributes can be massively assigned. This just means whether the attributes can be assigned en-mass, like in the $model->load() way, or whether they have to be set one at a time, like in
$model->title = "Some title";
To decide whether or not an attribute can be massively assigned, Yii looks at your validation rules and your scenarios. It doesn't validate them yet, but if there is a validation rule present for that attribute, in that scenario, then it assumes it can be massively assigned.
So, the next things to check is scenarios. If you've not set any, or haven't used them, then there should be no problem here. Yii will use the default scenario which contains all the attributes that you have validation rules for. If you have used scenarios, then Yii will only allow you to load the attributes that you have declared in your scenario.
The next thing to check is your validation rules. Yii will only allow you to massively assign attributes that have associated rules.
These last two will not usually cause load() to fail, you will just get an incomplete model, so if your model is not loading then I'd suggest looking at the way the data is being submitted from the form and check the array of _POST data being sent. Make sure it has the form I suggested above.
I hope this helps!

CakePHP 1.3 - Validate input is numeric within view/controller?

I need to validate that a form input is numeric in CakePHP 1.3. However, the input is not a property of the model, so I don't think I should try to set the validation for it in the model. Instead, some calculations are done on that input and the results are used in the resulting model object. How can I validate this in the view/controller? That is, check that what the user input was numeric and show a validation error message if not before passing it through the calculations? Thanks!
There's nothing wrong with defining model validation rules for non-existing / calculated fields, but you can also use the Validation class which might be cleaner. See 1 and 2.
If you use jquery at least you don't have to do a full page reload to check. Especially if it's only for one value. Just another option, see if it helps!
if($('#Field').val() != "")
{
if(!($.isNumeric($('#Field').val())) {
alert('value must be numeric');
}
}

Categories