React inline error with asp.net Model State errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Route("api/v1/account")] | |
[AllowAnonymous] | |
public class ApiController : Controller | |
{ | |
[Route("login")] | |
public async Task<IActionResult>Login([FromBody] LoginRequest login) | |
{ | |
if (ModelState.IsValid) | |
{ | |
// ... logic | |
} | |
return BadRequest(ModelState.ToDictionary( | |
kvp => kvp.Key, | |
kvp => kvp.Value.Errors.Select(e => e.ErrorMessage).ToArray() | |
)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from 'react' | |
export const InlineError = ({ field, errors }) => { | |
if(!errors) { | |
return null | |
} | |
if(!errors[field]) | |
return null | |
return (<div className='errors-container'> | |
<ul> | |
{errors[field].map(error => <li>{error}</li>)} | |
</ul> | |
</div>) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment