Validating optional fields in react using Zod - forms

Using react-hook-form, and zod within a Next.js project. Trying to get .optional() to work. It works with simple strings. See the schema below.
//works
const FormSchema = z.object({
website: z.string().optional()
});
But it does not work when I add the .url() flag. It only checks for the valid url. If the field is blank, it throws an error. In other words, the field is no longer optional. It accepts a valid url, but not a blank input field. Of course I want it to accept a blank input and a valid url as the only valid inputs.
//not working. the field is no longer optional.
const FormSchema = z.object({
website: z.string().url().optional()
})
Perhaps the problem has to do with the input field returning the wrong data-type when it's blank?
<label className="block">
<span className="block text-white">Website</span>
<input
id="email-input"
type="text"
className={`block border text-lg px-4 py-3 mt-2 border-gray-200 focus:bg-white text-gray-900 focus:ring-purpleLight focus:ring-2 outline-none w-full disabled:bg-gray-100 disabled:text-gray-400 disabled:cursor-not-allowed`}
{...register("website")}
disabled={isSubmitting}
placeholder="Website"
value={undefined}
/>
</label>
{errors.website && (
<p className="mt-1 text-sm text-red-600">
{errors.website.message}
</p>
)}

Perhaps the problem has to do with the input field returning the wrong data-type when it's blank?
I think this is likely your problem, although I'm not familiar with what react-hook-form might be doing to transform the input before handing it to your schema. The empty field will have the empty string '' as a value when it's blank. If you just use zod directly:
z.string().optional().parse(''); // succeeds because '' is a string
// Whereas this will fail because although '' is a string, it does not
// match the url pattern.
z.string().url().optional().parse('');
.optional() allows for the input value to be undefined so for example:
// These will both successfully parse because of `optional`
z.string().optional().parse(undefined);
z.string().url().optional().parse(undefined);
Going out slightly on a limb here, but if you wanted '' to pass you could add a preprocess step where you convert '' to undefined:
const s = z.preprocess(
(arg) => (arg === "" ? undefined : arg),
z.string().url().optional()
);
console.log(s.safeParse("")); // success (data: undefined)
console.log(s.safeParse("test")); // failure (not a url)
console.log(s.safeParse(undefined)); // success (data: undefined)

Related

Problem in calling a value through the value property

** <input type="number" id="userGuess" />**
I have created an input on the line above
And through the line below, I have put it in a variable
const userValue = document.querySelector('#userGuess').value;
But when I enter a value in input the web page. It does not print
console.log(userValue)
I expect, when I enter a value in the generated input. I can get it through console.log or** alert**

How to store date in some variable in Cypress

Can anyone help me how to store date from a field to variable. Here is the HTML which I am looking at:
<input id="date" class="input_date" id="XYZ" type="date" value="2019-01-12" on_input="table()">
I tried:
const date1 = Cypress.moment(). get('#id_value')
If the ID's are unique, you could try getting the val into a variable as below. I have used date id in the below code.
note: In the input html tag there two ID's, may be need to confirm with dev team which one to be used here
cy.get('#date').invoke('val').then((val)=>{
const dateValue = val;
console.log("Here is the date:"+dateValue);
})
Although Cypress imports the moment library, there are no built in commands for it that allow chaining, but you can add a custom command to make it easier.
The toMoment() command must be chained off a previous selecting command like cy.get() or cy.contains(). It returns a moment object which you can then use invoke to call all the methods moment provides, and further chain .should() to test the value returned from those methods.
For example,
Spec
Cypress.Commands.add('toMoment', {prevSubject: true}, (element) => {
return Cypress.moment(element[0].value);
});
it('input tests with moment', () => {
cy.visit('./app/moment-with-input.html');
cy.get('input').toMoment()
.invoke('isValid')
.should('eq', true);
cy.get('input').toMoment()
.invoke('format', 'dddd')
.should('eq', 'Saturday');
cy.get('input').toMoment()
.invoke('diff', Date(2020, 2, 5), 'days')
.should('eq', -391);
})
HTML fragment (put in '/app' folder of project)
<input id="date" class="input_date" id="XYZ" type="date" value="2019-01-12" on_input="table()">

Getting the following error " WebDriverError: unknown error: a.tagName.toUpperCase is not a function"

When I tried to enter a text in the below input field I am getting the error although it displayed in the window. The following input element html code is given below. I couldn't understand the reason for the error and unable to find the solution " WebDriverError: unknown error: a.tagName.toUpperCase is not a function"
<input type="text" ng-model="$ctrl.tag.name" ng-if="$ctrl.isSelectedMode" autofocus="autofocus" ng-blur="$ctrl.saveTagName()" name="tagName" ng-keyup="$ctrl.escapeEditing($event)" form-validator="$ctrl.tagName.uniqueError.validator" class="ng-valid ng-valid-uniqueness ng-not-empty ng-dirty ng-valid-parse ng-touched" autocomplete="off" style="">
I have written code in Page Object Model
this.AddTag = function(tagname1,tagname2,tagname3){
let clickAddTag = element(by.xpath("//*[text()='Add Tag']"));
clickAddTag.click();
browser.sleep(2000);
let AddTag = element(by.xpath("//input[#name='tagName']"));
if(AddTag.isPresent()){
AddTag.sendKeys(tagname1);
AddTag.sendKeys(protractor.Key.ENTER);
clickAddTag.click();
AddTag.sendKeys(tagname2);
AddTag.sendKeys(protractor.Key.ENTER);
clickAddTag.click();
AddTag.sendKeys(tagname3);
AddTag.sendKeys(protractor.Key.ENTER);
}
In specs I am calling the method
BidConfiguration.AddTag('tag1','tag2','tag3');
It's because you are trying to uppercase not string but HTML element.
more less stg like that:
element(by.xpath("//*[text()='Add Tag']")).toUpperCase();
You probably want to change text of the element to uppercase.
Try:
element(by.xpath("//*[text()='Add Tag']")).getText().then((elementText) => {
console.log(elementText.toUpperCase());
});
I found the solution
use actions class with sendKeys
browser.actions.click(element).sendKeys('text').perform();

approach for validated form controls in AngularJS

My teammates and I are learning AngularJS, and are currently trying to do some simple form field validation. We realize there are many ways to do this, and we have tried
putting input through validation filters
using a combination of controller and validating service/factory
a validation directive on the input element
a directive comprising the label, input and error output elements
To me, the directive approach seems the most "correct". With #3, we ran into the issue of having to communicate the validation result to the error element (a span sibling). It's simple enough to do some scope juggling, but it seemed "more correct" to put the span in the directive, too, and bundle the whole form control. We ran into a couple of issue, and I would like the StackOverflow community's input on our solution and/or to clarify any misunderstandings.
var PATTERN_NAME = /^[- A-Za-z]{1,30}$/;
module.directive("inputName", [
function () {
return {
restrict: "E",
require: "ngModel",
scope: {
fieldName: "#",
modelName: "=",
labelName: "#",
focus: "#"
},
template: '<div>' +
'<label for="{{fieldName}}">{{labelName}}</label>' +
'<input type="text" ng-model="modelName" id="{{fieldName}}" name="{{fieldName}}" placeholder="{{labelName}}" x-blur="validateName()" ng-change="validateName()" required>' +
'<span class="inputError" ng-show="errorCode">{{ errorCode | errorMsgFltr }}</span>' +
'</div>',
link: function (scope, elem, attrs, ngModel)
{
var errorCode = "";
if (scope.focus == 'yes') {
// set focus
}
scope.validateName = function () {
if (scope.modelName == undefined || scope.modelName == "" || scope.modelName == null) {
scope.errorCode = 10000;
ngModel.$setValidity("name", false);
} else if (! PATTERN_NAME.test(scope.modelName)) {
scope.errorCode = 10001;
ngModel.$setValidity("name", false);
} else {
scope.errorCode = "";
ngModel.$setValidity("name", true);
}
};
}
};
}
]);
used as
<form novalidate name="addUser">
<x-input-name
label-name="First Name"
field-name="firstName"
ng-model="firstName"
focus="yes"
model-name="user.firstName">
</x-input-name>
<x-input-name
label-name="Last Name"
field-name="lastName"
ng-model="lastName"
model-name="user.lastName">
</x-input-name>
...
</form>
First, because both form and input are overridden by AngularJS directives, we needed access to the ngModel API (ngModelController) to allow the now-nested input to be able to communicate validity to the parent FormController. Thus, we had to require: "ngModel", which becomes the ngModel option to the link function.
Secondly, even though fieldName and ngModel are given the same value, we had to use them separately. The one-way-bound (1WB) fieldName is used as an attribute value. We found that we couldn't use the curly braces in an ngModel directive. Further, we couldn't use a 1WB input with ngModel and we couldn't use a two-way-bound (2WB) input with values that should be static. If we use a single, 2WB input, the model works, but attributes like id and name become the values given to the form control.
Finally, because we are sometimes reusing the directive in the same form (e.g., first name and last name), we had to make attributes like focus parameters to be passed in.
Personally, I would also like to see the onblur and onchange events bound using JavaScript in the link function, but I'm not sure how to access the template markup from within link, especially outside/ignorant of the larger DOM.

How to serialize html form in dart as a string for submission

In jQuery, there is a function to serialize a form element so for example I can submit it as an ajax request.
Let's say we have a form such as this:
<form id="form">
<select name="single">
<option>Single</option>
<option selected="selected">Single2</option>
</select>
<input type="checkbox" name="check" value="check1" id="ch1">
<input name="otherName" value="textValue" type="text">
</form>
If I do this with the help of jquery
var str = $( "form" ).serialize();
console.log(str);
the result would be
single=Single2&check=check1&otherName=textValue
Is there such functionality in dart's FormElement or I have to code it myself? Thanks.
I came up with my own simple solution that might not work in all cases (but for me it is workikng). The procedure is this:
First we need to extract all input or select element names and values from the form into Dart's Map, so the element name will be the key and value the value (e.g. {'single': 'Single2'}).
Then we will loop through this Map and manually create the resulting string.
The code might look something like this:
FormElement form = querySelector('#my-form'); // To select the form
Map data = {};
// Form elements to extract {name: value} from
final formElementSelectors = "select, input";
form.querySelectorAll(formElementSelectors).forEach((SelectElement el) {
data[el.name] = el.value;
});
var parameters = "";
for (var key in data.keys) {
if (parameters.isNotEmpty) {
parameters += "&";
}
parameters += '$key=${data[key]}';
}
Parameters should now contain all the {name: value} pairs from the specified form.
I haven't seen anything like that yet.
In this example Seth Ladd uses Polymers template to assign the form field values to a class which get's serialized.