简体   繁体   English

如何更改phoenix / Elixir中的数据验证?

[英]How can I change my data validation in phoenix/Elixir?

i don't have experience with developing and this is not my first language, sorry for anything. 我没有开发经验,这不是我的第一语言,对不起任何事情。

I'm working in an mobile app with the back end in Elixir/Phoenix, when a person wants to create a new account the system requires 5 information to create a new account (email, cpf, name, phone_number, password). 我正在使用Elixir / Phoenix后端的移动应用程序,当一个人想要创建一个新帐户时,系统需要5个信息来创建一个新帐户(电子邮件,cpf,名称,phone_number,密码)。

in my new registration flow I want to request only 2 data (Email and Password) and later using the application the user can complete the missing data, so my system has to be able to create an account with this data being null. 在我的新注册流程中,我想只请求2个数据(电子邮件和密码),以后使用该应用程序,用户可以完成丢失的数据,因此我的系统必须能够创建一个帐户,该数据为空。

First i went to my structure database to check if it was not allowing the data to be null, how you can see bellow it's not happening: 首先,我去了我的结构数据库,检查它是否不允许数据为空,你怎么能看到它没有发生:

CREATE TABLE users (
  id bigint NOT NULL,
  email character varying(255),
  cpf character varying(255),
  phone_number character varying(255),
  password character varying(255),
  inserted_at timestamp without time zone NOT NULL,
  updated_at timestamp without time zone NOT NULL,
  name character varying(255)
);

so I went to check my user.ex file to see if the function that does the data validation was not allowing them to be null, and i found this: 所以我去检查我的user.ex文件,看看进行数据验证的函数是不是允许它们为null,我发现这个:

def changeset(%User{} = user, attrs) do
  user
  |> cast(attrs, [:email, :cpf, :name, :phone_number, :password])
  |> validate_required([:email, :cpf, :name, :phone_number, :password])
  |> validate_format(:email, ~r/^[A-Za-z0-9._%+-+']+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/)
  |> validate_format(:cpf, ~r/([0-9]{2}[\.]?[0-9]{3}[\.]?[0-9]{3}[\/]?[0-9]{4}[-]?[0-9]{2})|([0-9]{3}[\.]?[0-9]{3}[\.]?[0-9]{3}[-]?[0-9]{2})/)
  |> validate_cpf(:cpf)
  |> unique_constraint(:email)
  |> unique_constraint(:cpf)
  |> unique_constraint(:phone_number)
  |> update_password_hash()
  |> update_activation_code()
end

I believe I need to change this function to allow the system to create an account without all this data and after having made the account the user can complete their data with from within the app but I'm not sure how I can do this, can anybody help me ? 我相信我需要更改此功能以允许系统创建一个没有所有这些数据的帐户,并且在创建帐户之后用户可以在应用程序内完成他们的数据,但我不确定如何做到这一点,可以有人帮帮我吗?

If the field is not required, you should remove it from validate_required . 如果不需要该字段,则应将其从validate_required中删除

So your changeset function should become 所以你的变更集功能应该成为

def changeset(%User{} = user, attrs) do
  user
  |> cast(attrs, [:email, :cpf, :name, :phone_number, :password])
  |> validate_required([:email, :password])
  |> validate_format(:email, ~r/^[A-Za-z0-9._%+-+']+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/)
  |> validate_format(:cpf, ~r/([0-9]{2}[\.]?[0-9]{3}[\.]?[0-9]{3}[\/]?[0-9]{4}[-]?[0-9]{2})|([0-9]{3}[\.]?[0-9]{3}[\.]?[0-9]{3}[-]?[0-9]{2})/)
  |> validate_cpf(:cpf)
  |> unique_constraint(:email)
  |> unique_constraint(:cpf)
  |> unique_constraint(:phone_number)
  |> update_password_hash()
  |> update_activation_code()
end

All you need to do is change the call to the validate_required method to remove the fields you want to be optional: 您需要做的就是更改对validate_required方法的调用以删除您想要选择的字段:

|> validate_required([:email, :password])

If you want to learn more about what these function calls do, you can read the Phoenix framework guide about Ecto (particularly, the section on Changesets and Validations ). 如果您想了解这些函数调用的更多信息,可以阅读关于Ecto的Phoenix框架指南 (特别是关于变更集和验证的部分)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM